« The Training "Ombudsman"What Can You Really Teach with eLearning? »

Casting Root : ActionScript 3.0 : Finding Your Movie Clips

04/04/09

One of the most frustrating things for me to learn in AS 3.0 was being able to find my movie clips in the code. I usually work on a single Flash timeline with lots of nested movie clips that make up my elearning: Frame 1 has an intro movie clip, Frame 2 introduces the course, etc.

My frustration with AS3.0 is that I couldn't figure out how to control my main movie from within my nested movie symbol. For example, in Frame 1, my intro movie plays, and then ends on a screen that explains the learning objectives. There is a big "Start the Training" button, and when the user clicks on it, the ROOT of the Flash movie advances to frame 2, where a new movie symbol plays.

Not that hard right? Old ActionScript code:

_root.play();

Not any more. I'm stubborn...I want AS 3.0 to work like AS 2.0. Suck it up buttercup, it won't. The reality is AS 3.0 doesn't see the "stage" or even the "timeline", relative to the objects. Think of it this way...in AS 1.0 and 2.0, objects added to your movie existed on the timeline, and Flash knew where they were at based on where on the timeline you placed them. They were "happyBall instance on frame 1 of the main timeline(root)."

In AS 3.0, movie clips just "are". Very zen like, I know, but in AS 3.0, they are defined as themselves. It doesn't matter where they are on the timeline - their identity is not connected to where they are placed. So, trying to add "root" to an AS 3.0 object is like trying to tell a book to go back to the printing press where it was created. Can't do it on its own - it has no awareness of its location.

After searching around the web, and finding a ton of different solutions (some of which seemed overly complicated, especially the ones that said to replace

_root

with just

root

...doesn't work...nice try), the kind developers at Yahoo found a working solution. It goes back to the day when you were able to identify anything based on its location, relative to the root. The code looks like this:

MovieClip(this.root)

So, in our example, in the movie clip, we have a button that advances to the next frame of the main timeline. There is a movie with some text and a button (named clickMe) in it. In the Movie clip, you would add this code:

clickMe.addEventListener(MouseEvent.CLICK, goClickMe);

function goClickMe(event:MouseEvent):void {
	MovieClip(this.root).play();
}

You can control the main timeline from within movie clips using that bit of code.

I hope this helps you as much as it helped me. Maybe its no longer a best practice to embed ActionScript in movies that control other things besides that movie symbol, but for a guy like me that wants to move from AS 2.0 to 3.0 quickly, I'm going to hang my hat on these little tricks to help me rapidly develop my projects.

Oh...and if you want to download a sample to see this in action click here. Its a Flash CS4/AS 3.0 file that shows you the controls I'm talking about.

Trackback address for this post

This is a captcha-picture. It is used to prevent mass-access by robots.
Please enter the characters from the image above. (case insensitive)

42 comments

Comment from: Luís Miguel [Visitor]
*****
Very useful, just what I needed! Thank you =]
04/18/09 @ 15:06
Comment from: Tabita [Visitor]
***--
Hi, i want to ask you, how it's the code if i want to go back to home in timeline (and not to advance to the next frame of the main timeline). With this code i go to next frame, but i wanr to go back home(three frames). Thank you very much!
05/08/09 @ 15:46
Comment from: Juan C Ballesteros [Visitor] · http://jcballesteros.com
****-
Thanks, just what i was looking for!

To Tabita, if you labeled your frame set use MovieClip(this.root).gotoAndStop('home') or MovieClip(this.root).gotoAndPlay if that's the case;
06/11/09 @ 14:25
Comment from: Rachel [Visitor]
*****
Thank you SO much!
06/11/09 @ 15:07
Comment from: LionHeart [Visitor]
Itis good,very useful
Thank's
06/23/09 @ 15:49
Comment from: Steve [Visitor] · http://www.thepetersonproject.com
*****
Dude, you just made me so happy. Thank You!
07/11/09 @ 21:41
Comment from: ralph [Visitor]
*****
im proud of you... ive been looking for this,,, but i have 1 more problem... i have many movie clip inside the movie clip...

(in AS2.0 heres the code)
_root.clickme.clickmeinside.another.loader_mc.loader_btn.loader_txt.loadMovie("index.swf")

how can i conver this to AS3.0
08/10/09 @ 15:37
Comment from: Thomas Toth [Member] Email
Replace _root. with:

MovieClip(this.root).

and it should path correctly down to your very buried movie clip! :)


08/10/09 @ 15:48
Comment from: AS3 IS A WHORE [Visitor] · http://visitorQ.slingshot.com
*****
THANK YOU SOOO MUCH FOR THIS, you cannot believe how F** Frustrating trying to play a movie clip turned out to be with stupid AS3. (i'm switching from AS2)
09/08/09 @ 20:15
Comment from: Sven Lennartz [Visitor]
*****
OMFG - This is so freaking great!
Wasted like 3 hours with google and stuff...

And now, here's the solution...

THANKS!!!
09/19/09 @ 08:22
Comment from: scripter [Visitor] · http://degrotejongens.nl
*****
Thnx!!
10/08/09 @ 10:17
Comment from: leigh [Visitor]
*****
finally! Thanks!
10/12/09 @ 11:08
Comment from: Robin Huber [Visitor]
*****
I LOVE YOU!
10/21/09 @ 20:09
Comment from: EricC [Visitor]
****-
I have tried this and it seems like every variation of root and parent and this is the error I always get:

TypeError: Error #1009: Cannot access a property or method of a null object reference.
at master2_fla::MainTimeline/frame2()

Can anyone help?!
10/28/09 @ 20:04
Comment from: Thomas Toth [Member] Email
I'd have to see your code...it looks like you may have this code on the main timeline instead of buried in a movie clip. You don't need MovieClip(this.root). to access a clip on the main timeline. This is used when you need to control the main timeline from an embedded movie symbol.
10/29/09 @ 19:19
Comment from: Franco Li [Visitor]
*****
Thank you! I've been looking for this answer for long time!
11/06/09 @ 11:18
Comment from: firewolf [Visitor] Email
*****
Hi! Thanks for the great info. How does this apply to externally loaded swfs? I am trying to target a button rollover on my main index swf (which contains nav and UILoader) from a button within the external swf (being loaded through UILoader)
I've tried what you've explained above and other variants, but get a 1010 error. Any ideas would be greatly appreciated! :)
11/10/09 @ 22:12
Comment from: Bert Sagi [Visitor] · http://www.bertsagi.com
*****
YOU ARE THE MAN!!!!!!!!!!!!!!!!!!!!
12/06/09 @ 13:04
Comment from: Hwhiskey [Visitor] · http://1stdip.com
*****
hey hey, good stuff man

life saving
12/31/09 @ 08:52
Comment from: Keith Scott [Visitor] · http://www.scottpartnership.com
*****
Being a novice in both Flash and Actionscript 2 (where my buttons worked perfectly ok), I was in danger of losing hair rebuilding my website and trying to fathom this rather glossed-over subject regarding buttons inside movie clips in Actionscript 3 - required for the 3D effect I'm also tampering with. But after a great deal of surfing I came across your wonderfully simple solution which works perfectly and I am extremely grateful to you for sharing it.
Thanks
Keith
01/17/10 @ 14:24
Comment from: Kathleen [Visitor]
*****
Thank you. I have been trying to figure this out for hours, nothing worked until I found your post. Thank you.
01/19/10 @ 11:00
Comment from: himangshu maity [Visitor] Email
*****
hello all !!

how to access movie clip if it is located in another frame on root frame.

i have an animation in frame no.1 in root frame inside a movie clip (himu_mc).

and another movie clip have an animation on frame no.5 inside a movie clip(himu2_mc)

is it possible to access himu2_mc from himu_mc.

please help me if anyone have this solution.

thanks
01/30/10 @ 02:34
Comment from: Karl [Visitor]
*****
Finally, Thank you.
02/01/10 @ 13:41
Comment from: Me [Visitor]
for: himangshu maity

It is possible (if I've understand your question)...

Just after the MovieClip(this.root) put dot and the name of your movie clip...

For you example it should be:

MovieClip(this.root).himu2_mc.gotoAndStop(1);

Ofcourse, if your goal is gotoAndStop on frame 1...

Enjoy!


02/05/10 @ 00:14
Comment from: Bauritto [Visitor] Email
*****
Maaaaaaaaaaan !!! You are awesome .... seriously
02/19/10 @ 21:49
Comment from: MrT [Visitor]
*****
What about targeting a function that is on frame 1 of the main timeline from a nested button. I got used to putting all of my functions in one place and then calling them from any nested level using _root.functionName

I have a listener that is three levels in with this code
this.addEventListener(MouseEvent.CLICK, (parent as MovieClip).btnClick);

but the function btnClick is on frame 1 of the main timeline.

I get the following error when the clip with that listener loads
TypeError: Error #2007: Parameter listener must be non-null.
at flash.events::EventDispatcher/addEventListener()
at website_fla::Heirloom_btn_7/frame1()

How do I reference the function on the main timeline from the listener?

Many Thanks
03/12/10 @ 08:30
Comment from: Tony M. [Visitor]
hi, I have a button inside a movie clip, which is inside a movie clip that is located in the scene.
Scene1 > movieclip1 > movieclip2 > button

I want the main timeline to gotoAndStop (5);
but unfortunately the MovieClip(this.root) command doesn't work for me.
Any help would be appreciated, thanks!
04/13/10 @ 18:29
Comment from: Randall M. [Visitor]
*****
I am essentially having the same problem as Tony M.

I have a button in a movie clip that I want to, when clicked, go to frame 3 of the main timeline. Thanks for the help!

current code for going to frame 2...

btn1_btn.addEventListener(MouseEvent.CLICK, buttonClick1);
function buttonClick1(event:MouseEvent):void{
MovieClip(this.root).play();
};
04/30/10 @ 12:20
Comment from: nicole [Visitor]
*****
It took me a whole day of work to figure this out. this is a god-send.

thank you soooo much! I think I'm going to cry I'm so happy
04/30/10 @ 16:21
Comment from: Margaret [Visitor] Email · http://www.coloramerica.com
*****
thank you! thank you! thank you! I can finish this application I been building. PS. Your now on my bookmarks of top sites.
05/21/10 @ 16:03
Comment from: Imaginer [Visitor]
*****
Fantastic! You solved my problem in 2 minutes! I couldn't understand other explanations, because of higher level.
Your's is as simple, and as good as it can be. Thank You!! :)
06/30/10 @ 01:44
Comment from: Tim [Visitor] Email
*****
Hi there,
Just wondered if anyone knew how to do this in reverse?? I have a parent layer with a button, that when clicked I which to reset a child movie called Intro_movie that is inside another movie home_bg inside the main root. I need to get the button click Home_BTN to reset Intro_movie to frame 1 and stop. Any suggestions please?
11/04/10 @ 18:43
Comment from: Anando [Visitor]
thank you kind sir, your help has saved me a lot of time, most of the tutorials out there are shitty as hell, yours was the only easy one.
12/03/10 @ 11:19
Comment from: Julia [Visitor]
*****
Thank you very much! You saved my day!

One thing I figured out, which might help other people. You have to change the code to MovieClip(parent.root) if you are calling something into the root from inside a loaded movie clip.

Thank you!
01/16/11 @ 08:27
Comment from: benny [Visitor] Email
*****
Tahnks
03/09/11 @ 01:21
Comment from: Chaukar [Visitor] · http://www.animeonsen.com
*****
Thank you very much. I was really struggling how to change variables in the parent class from the child class for a school project. Your article really helped me.
03/23/11 @ 15:13
Comment from: Camila [Visitor] · http://paschini.wordpress.com
*****
man i love you marry me and have my kids xD
06/27/11 @ 13:34
Comment from: Camila [Visitor] · http://paschini.wordpress.com
*****
hey now, seriously speaking.

ive started out in Flash from ActioScript 3, and have found that over time, people dont intend to make the move into 3, specially cuz they find it harder to code in 3.

The difference between 1-2 and 3 is that everything was relative to objects, and all objects related to the maintimeline somehow. the way things go in 3 is that everything relates to status (event status), which move flash from a object-oriented platform into a state-driven (as in the finite state machine) architeture.

which means instances are created and destroyed as needed, and also things dont have a root. they have states. so you have on frame 1 a certain state, which means certain things exist. at any given moment, you can control everything that exist in that state. so you can control child objects from their parent objects, and can control parents from children.

they only thicky part for me was what was exactly the code to bring up the anscestors, once i knew they all existed at some point. and you solved it!! and people dont care about finding it out, they only care about keeping everything in AS1-2. which is a pain, for being OO.

a state-driven architeture is always better cuz then you dont need to know everything that has to be known about all your objects at any time. you can store that info in the objects themselves and prompt they to answer when needed, as long as you remember their state at any point.

think about that using a lamp as example: you dont need to know the shape of the lamp, the color of its light or its size everyday. but you might care to know it is off, on, or broken. then when you decide to replace it, then you can check on the lamp for its size, color, shape.
06/27/11 @ 13:50
Comment from: Brad Harris [Visitor]
*****
*thumbsUp*
08/18/11 @ 09:32
Comment from: Jose Albert [Visitor] · http://www.i2sys.com
*****
Wow!
This is something I am being scratch my head for sometime now.
Just Great thanks this is just beautiful.
Thank you.
10/26/11 @ 01:05
Comment from: sharon [Visitor]
*****
Thank you!!!
11/25/11 @ 02:47
Comment from: Edward [Visitor] Email · http://www.edscorner.com
I've been trying to get this solution as well...but I need to have the code execute via a function name and NOT by clicking a button...what event listener would I use for that?

Thanks.
12/15/11 @ 17:44

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
PoorExcellent
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)
This is a captcha-picture. It is used to prevent mass-access by robots.
Please enter the characters from the image above. (case insensitive)

Very few people are creating technology exclusively for the online learning developer, so this site attempts to fill that gap. Whether you want ideas on how to use web technologies in your eLearning, or have questions about the what's and how's, this site is for you.

February 2012
Sun Mon Tue Wed Thu Fri Sat
 << <   > >>
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29      

Search

XML Feeds

powered by b2evolution free blog software