PDA

View Full Version : Catching Events from Child Objects


jimmyboo
01-13-2008, 01:03 AM
Hi,

I am trying to figure out how to throw a custom event from an object that is the child of another object such that the parent object can catch it. Basically, I have two classes, Gallery and Image. Each instance of the Gallery contains an array of Image objects. The Image class contains a method to load a jpg using the Loader class, to which I have assigned an event listener for the Event.COMPLETE event (added to the contentLoaderInfo property of Loader class).

What I want to do is, when the COMPLETE event is thrown by any instance of the Image class, I want the parent Gallery class to catch it. Does anyone have any idea of how to do this? I gather that I have to create a custom Event class, but I'm not quite show how to assign a listener in the Gallery class to events thrown by the child Image objects. Thanks in advance!

J

5566
01-13-2008, 02:27 AM
Image extends EventDispatcher

in you COMPLETE event handler dispatch a custom event

dispatchEvent(new Event("imageLoadComplete"));

in your Gallery Class, when creating an instance of Image, add an event listener to it.

var img:Image=new Image();
img.addEventListener("imageLoadComplete",hImageLoadComplete);

that's it.

jimmyboo
01-13-2008, 02:34 AM
Excellent, I've just been playing around with it and came up with the same solution. Many thanks!