dcsimg
A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: What does _root mean?

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    3

    What does _root mean?

    I have been having a problem with linking between scenes with buttons in AS3.

    Although it seems to be a very hot topic, I haven't actually found a thread where people's problems have actually been solved, or answered. There has just been a lot of angry people posting solutions that involve adding _root. to the beginning of gotoAndPlay... however whenever I do this, it tells me that that property is undefined.

    I see people saying just add _root to the main timeline or to the path... what does that mean? and how do i do that?


    here is my code right now:


    button_right.addEventListener(MouseEvent.CLICK, button_righthandle);

    function button_righthandle(evt:MouseEvent):void {
    _root.gotoAndStop('good');
    }

    button_left.addEventListener(MouseEvent.CLICK, button_lefthandle);

    function button_lefthandle(evt:MouseEvent):void {
    _root.gotoAndStop('danger');
    }

    Note:
    'good' and 'danger' are frame lables for other scenes, but when I put in simply:

    gotoAndStop('danger', "Scene3"); --> danger being the lable and scene3 being the scene that it is in, it says my scene doesnt exist. Hense why I am assuming I need that root thing, and hense my issue...


    I would really appreciate the help, thank you!

  2. #2
    Amazed and Amused Mazoonist's Avatar
    Join Date
    Mar 2006
    Location
    Northern California
    Posts
    201
    Your gotoAndStop command would have worked if you had used the exact name that Flash uses. You see, Flash inserts a space, so it's "Scene 3" and not "Scene3" so the correct command would be:

    gotoAndStop("danger", "Scene 3");

    It's also case sensitive, so "scene 3" won't work either, because the S must be a capital letter. However, you can name your scenes whatever you want--you don't have to accept Flash's default. But using the exact string, capitalization and all, still applies to your own names as well.

    _root was an object in Actionscript 1 & 2. In Actionscript 3, it's just root (note the lack of an underscore). So if you are going to program in Actionscript 3, don't ever use the underscore anymore. And I'll see if I can explain the difference between the old _root and the new root:

    In Actionscript 2, _root was the place where every path began, because the only way to get MovieClips to display other MovieClips was to nest them "Nesting" means to place MovieClips inside other MovieClips, and so _root represented the main timeline and was the source of a whole lot of complicated pathing, like:

    _root.myMovieClip.myOtherMovieClip.myMenu.home_btn

    In Actionscript 3, root still represents the main timeline, but now AS3 has this cool thing called the display list. Display list programming eliminates the need for all that nesting, as display objects can just be added and removed from the "display list" of the higher level display objects. These objects that are added to a display list, though, need not be nested inside the higher level objects. The root is the highest level display object aside from the stage. You don't want to deal with the stage directly anyway, though, and the root has automatically been added to the stage for you already, so you really only deal with the root and everything beneath it when you manage the display list.

    Instead of referring to objects using a complicated path, you can just refer to them directly, even though they've been added to the display list of another object. For example:

    myClip.addChild(myOtherClip);

    After the above command has been written, further references to myOtherClip read like this:

    myOtherClip

    NOT this:

    myClip.myOtherClip

    Here's a pretty good video introduction to the power of the display list: http://flashgameu.com/Understanding_...21-102550.html

    Sorry you haven't gotten the help you needed in the past. And I know all you asked was the definition of "root," but it's hard to explain without going into at least some discussion of the display list.

    The new correct syntax for referring to the root is like this:

    MovieClip(root).gotoAndStop("danger", "Scene 3");

    The above command is absolute and works from anywhere (that is, for example, it would work if it were issued from inside another movie clip's frame code).

    But code is relative to where it's at, so if you are already on the main timeline, you can just say:

    this.gotoAndStop("danger", "Scene 3");

    or even: gotoAndStop("danger", "Scene 3"); // the "this" part is understood

    Hope this helps somewhat, as that simple word "root" is kind of a deep subject.
    Last edited by Mazoonist; 11-03-2011 at 02:45 AM.

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    3

    Still having issues haha

    Thank you so much for the awesome reply! Being pretty new to actionscript that was very interesting, and I have no idea why I have not joined a forum like this before.

    Unfortunately I'm still having the issue and I am wondering if its possible that my problem is something else all together. The scene names, making sure they are the right spelling and such is a good tip, however that is just what I named my scenes. I went back and forth and when I renamed them after a few times, i just landed on naming them that, so I was calling on the right scene name...

    but my code is still saying that my scenes don't exist when I click on my buttons.

    I had another question... When you say MovieClip, is that actually what I should be writing? or am I supposed to be putting in the name of my button? or... am I supposed to be using movieclips instead of buttons for that to work? I have heard that movie clips are really what I should be using, but I have a think for buttons :P

    If you are unsure of what my issue is now, no matter, but I really appreciate the suggestions and the little lesson about lists.

  4. #4
    Amazed and Amused Mazoonist's Avatar
    Join Date
    Mar 2006
    Location
    Northern California
    Posts
    201
    To tell you the truth, I had not visited this forum for a long time, and your post just kind of caught my eye for some reason.

    You will find that scenes are mostly useful for animators making huge animations, too large to easily manage on the main timeline. However, when the swf is compiled, it all becomes one huge timeline anyway. All Flash does is connect them end to end. And there are serious issues with writing code on a timeline, and using that timeline for program navigation. I wrote about the problem (and a good solution) here: http://theflashconnection.com/conten...ng-as3-classes.

    You might want to stop using scenes unless you have a compelling reason for doing so. They are really kind of a legacy from the early days of Flash. I am sure they still have their usefulness, but I seriously haven't seen too many cases where people have used them properly and put them to good use, and I have seen a lot of cases where people just create a huge mess for themselves. I would say if you do huge animations and very little code, they might be useful. The more you code interactivity and navigation, though, the more difficult it becomes to manage it on a timeline.

    There's nothing wrong with Buttons, as long as you don't expect more out of one than just basic button functionality (up, over, down, hit). They don't make good containers. You can't address any object inside a button with actionscript. But you can do all of these things with MovieClips, and MovieClips can do anything buttons can do. So bottom line, if you know for a fact that all you need is a button, use a Button symbol to save time. Otherwise, use MovieClips. They take a little longer to craft, but I also wrote a tutorial on this subject, & you might find it interesting: http://theflashconnection.com/conten...p-buttons-mcbs

    If you want to send me your file, I would be glad to take a look at why it is not navigating to your scenes the way you expect. Send to mazoonist at gmail dot com.

  5. #5
    Junior Member
    Join Date
    Nov 2011
    Posts
    3

    Lightbulb Problem Solved!

    Thanks again very much for the additional info, it helped to clarify a lot of things for me...

    Turns out that my problem however was something completely ridiculous as these things usually are. I am embarrassed to say it almost, but my problem was that when I was testing it, I wasn't loading the whole file since my animation is so long, I was just previewing a scene at a time, so.. since it hadn't loaded all of the other scenes they technically didn't exist. Once I tried testing the whole video it suddenly worked of course.

    Thanks so much for your help!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center