Flash Actionscript 3 Tip: Calling a function from inside a movieclip

Just spotted this quandary on the forums regarding calling an Actionscript 3 function on the main timeline of Flash from inside of another movieclip.  This is one of my favorite type questions to answer because I had about a 50/50 chance of actually remembering how it was done without looking in some old .fla to find the last place I’d done this. And today I got it right…

Paste this onto a frame in the main timeline of your .fla…

function mainTimeline()
{
trace (“yo, I’m a function on the main timeline. If you are reading this, I was called by script in the logo movieclip”);
}

function mainTimeline() {

trace (“yo, I’m a function on the main timeline. If you are reading this, I was called by script inside a movieclip”);

}

Now use any movieclip you have onstage, and double click into it’s timeline and paste the following on a frame…

MovieClip(parent).mainTimeline();

Thats it. Totally done. If you publish, that trace statement will get called and you’ll see it in the Output window.

Alright what if you wanted to call this function from 2 mc’s deep. Like one movieclip inside of another. The code is…

MovieClip(parent.parent).mainTimeline();
Make sense? Also remember, you can use similar code for finding movieclips on the main timeline. So to find an instance call “cartoonsmart” on the root and change its x location and make it play, you’d use…
MovieClip(parent).cartoonsmart.x = 400;
MovieClip(parent).cartoonsmart.gotoAndPlay(2);
Yoink! Done! Pretty easy again, right. I’ll be here all week. Drive carefully.
Advertisements

15 thoughts on “Flash Actionscript 3 Tip: Calling a function from inside a movieclip

  1. wootreview says:

    Wow, my hours of scouring the web are over!!! Thanks for this, it fixed a problem I was having when I was creating a menu!!!

  2. Oliver says:

    Thanks for this excelent tip Justin. Been looking all over the web to find a solution to several problems I’ve had lately and your tip solved all of them. CHEERS!

  3. Vinicius Hacebe says:

    Ok… what if i need to trigger the method from a loaded MC… I mean i loaded an MC using LOADER and i just cant call the method the container has…

  4. Great article! Thanks. I thought I had tried exactly this earlier today and it didn’t work. Now it’s working fine. Must have been a bit off, thanks for the great explanation.

  5. Farzan mc says:

    Hello
    MovieClip(parent).functionName(); workd good if i added my movieclip manualy on stage.

    But if I add it dynamically , like :
    var mc:MovieClip = new mcName();
    stage.addChild(mc);

    then in mc , MovieClip(parent).functionName(); is no use.
    What should I do ?

  6. Farzan mc says:

    I found a way. But it’s not oop and I don’t like it.
    Send an instance of the timeline to movieclip after creating it :

    var mc:MovieClip = new MovieClipName();
    mc.timeline = this;
    ….

    in mc :
    Object(this).timeline.MainTimelineFunctionName();

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s