PDA

View Full Version : Child calling parent function



sgpeters
March 17th, 2009, 09:31 PM
I have setup a simple video interface. The video player lives in the parent swf. The video list chooser is loaded as a child swf. In this child swf, I have some video buttons nested within two movieclips in order to facilitate the scrolling feature. I am trying to get the movie buttons to load flvs into the component flv player in the parent swf. I have declared the functions in the parent and I am trying to call them in the child:

Code in the Parent:

function vid1(event:MouseEvent): void{
pubPlayer.source = 'vid1.flv';
}

function vid2(event:MouseEvent): void{
pubPlayer.source = 'vid2.flv';
}

------
Code in the child:

vid1_btn.addEventListener(MouseEvent.CLICK, parent.vid1);
vid2_btn.addEventListener(MouseEvent.CLICK, parent.vid2);


Obviously the compiler sees that there is no 'vid1' and 'vid2' functions in the child swf and it gives an error. I've tried preceding the call function with 'parent' but no luck. Any idea?

Any help is appreciated.

kevin416
March 17th, 2009, 09:46 PM
Pass a reference in through the child's constructor. Have an variable of the appropriate type inside each of the children that can store that parent object.

If you pass in the reference through a var called parent_mc, then you can just call parent_mc.vid1().

sgpeters
March 18th, 2009, 02:26 PM
Would it be like this:

var myParent:MovieClip = new MovieClip();

vid1_btn.addEventListener(MouseEvent.CLICK, myParent.vid1());
vid2_btn.addEventListener(MouseEvent.CLICK, myParent.vid2());

At some point do I need to call the name of the parent swf "videopage.swf"?

kevin416
March 19th, 2009, 08:47 PM
Ok, after reading the original post again, it would be easier if the code controlling the movie in the parent was actually in the parent. If you arranged your hierarchy such that the buttons controlling the movie are direct children of the parent, you should be able to put this code in the parent and have everything work:



vid1_btn.addEventListener(MouseEvent.CLICK, vid1);
vid2_btn.addEventListener(MouseEvent.CLICK, vid2);


Its hard to keep track of where everything is without seeing it. Post the .fla if you need more help.

To keep the hierarchy the way you have it, it might be necessary to extend the MovieClip class so that you can pass a reference when you instantiate it (this is what I was talking about before):


var childMC:MyMC = new MyMC( parent )

where MyMC extends MovieClip. Then you can call methods from within MyMC that exist in the parent:


vid1_btn.addEventListener(MouseEvent.CLICK, parent.vid1);

sgpeters
March 20th, 2009, 02:29 AM
I would greatly appreciate it if you took a quick look:

I posted the two relevant flas in a zip file here (http://http://thepetersonproject.com/newsite/fla.zip)

The video buttons in the child swf (videopagelist.swf) are nested within two movieclips that facilitate the custom scroll feature. The user will simply scroll through the list and click a thumbnail to control the flv component on the parent swf (videopage.swf)

Any ideas?

Thank you for you help

kevin416
March 20th, 2009, 05:19 PM
dude your link's broken

sgpeters
March 20th, 2009, 06:39 PM
Crap sorry, take two:

http://www.thepetersonproject.com/newsite/fla.zip

sekasi
March 21st, 2009, 10:21 AM
I'd strongly suggest you throw that suggestion out the window mate. It utterly breaks encapsulation and makes for extremely poor quality project architecture.

Dispatch an event that the parent class is listening for instead. Then deal with the method calls in the parent class in the event listener.

kevin416
March 21st, 2009, 02:07 PM
Sekasi, I'd be interested in how to actually do that too. Could you elaborate?

Anogar
March 21st, 2009, 02:11 PM
I'd strongly suggest you throw that suggestion out the window mate. It utterly breaks encapsulation and makes for extremely poor quality project architecture.

Dispatch an event that the parent class is listening for instead. Then deal with the method calls in the parent class in the event listener.

Sekasi is absolutely correct. Try something like this:



//in parent
myChild.addEventListener("customEvent", parentMethod);

//in child
dispatchEvent(new Event("customEvent"));

You could also go ahead and make a new custom event class if you want to be a little cleaner about the whole thing, or if you want to pass custom data.

:)