A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: Problems with hierarchy (nested functions inside movie clip)

  1. #1
    Junior Member
    Join Date
    Sep 2009
    Posts
    21

    resolved Problems with hierarchy (nested functions inside movie clip)

    Hi guys, I'm having problems with the scope of nested functions.
    I have a movie clip: "MC_clip"
    inside that movie clip there is a button: "inside_button".

    when users press "inside_button", I want the movie will jump to play frame 5 (main time line). how do i refer the stage from inside a movie clip??

    another question is about the other way: how can I call to a function that is declared inside a movie clip?? is that movie clip have to be an instance on the stage??
    Last edited by red-T; 08-23-2010 at 12:53 PM.

  2. #2
    Junior Member
    Join Date
    Aug 2010
    Posts
    8
    Quote Originally Posted by red-T View Post
    Hi guys, I'm having problems with the scope of nested functions.
    I have a movie clip: "MC_clip"
    inside that movie clip there is a button: "inside_button".

    when users press "inside_button", I want the movie will jump to play frame 5 (main time line). how do i refer the stage from inside a movie clip??

    another question is about the other way: how can I call to a function that is declared inside a movie clip?? is that movie clip have to be an instance on the stage??
    You need to refer to the MainTimeline, not to the stage. You have access to the MainTimline with:

    Actionscript Code:
    trace(MovieClip(this.root));

    To call a function inside of a Movieclip, which must be added to your stage, just do this:

    Actionscript Code:
    mc_name.functionName();

  3. #3
    Junior Member
    Join Date
    Sep 2009
    Posts
    21
    Actionscript Code:
    trace(MovieClip(this.root));

    I get an erroer about that one:
    TypeError: Error #1034: Type Coercion failed: cannot convert flash.display::Stage@10e8e2e1 to flash.display.MovieClip.
    at missionCompleted/bla_pressed()

    also I tried some variations (without the trace, without the MovieClip casting) and it also didn't work.

  4. #4
    Junior Member
    Join Date
    Sep 2009
    Posts
    21
    it appears that this kind of casting is wrong
    Actionscript Code:
    (MovieClip(this.root)).gotoAndPlay(1);
    since the stage is not a movie clip, the error will be thrown during run time (#1034: Type Coercion failed)

    casting (this.root) as a DisplayObject or a DisplayObjectContainer throws a compilation error (1061: Call to a possibly undefined method stage through a reference with static type Class)

    does any one has a clue how to reach the main time line from a function inside a movie clip?
    Last edited by red-T; 08-22-2010 at 05:14 AM.

  5. #5
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Code:
    MovieClip(root)
    Should have worked, if you did things in the conventional way. It seems you have put something directly on the stage, rather than as a child of the main timeline. Don't do that unless you really need something to be outside the root for some reason.

    Look for "stage.addChild" in your code, and change it to either just "addChild" or "root.addChild".

  6. #6
    Junior Member
    Join Date
    Sep 2009
    Posts
    21
    5TonsOfFlax you are a genius!
    I did added the movie clip as a child to the "stage" and not to the "root".
    Guess I did it all wrong. does any one has some good tutorial about how to write a code from a movie clip that effects the root???

    another question, it that the way things are done?, I mean, is it the conventional way to write a code inside a movie clip that effects the root, or this is not how things are done??

  7. #7
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    The display hierarchy starts at the stage. There is only one stage, and it has no parents. It usually has a single child, the root. The root is the main timeline or document class instance. Usually, you will want to keep everything that your swf does inside the root, though there are legitimate reasons to attach something to the stage.

    When you have a child directly manipulate a display ancestor, you create a strong coupling between that child and where it is in the display. This makes your code more fragile, and you will have to change more stuff if you change how things are arranged. What you can do instead is have the child dispatch an event, and have the ancestor that needs to react to that event listen for it. Make sure your event bubbles if you want to rely on the display list, or you can attach the listener directly to the child.

    It's pretty simple. Just like life, children shouldn't boss their parents around. They can ask nicely for things, and the parents can acknowledge that or not.

    The overall guiding principle is to think about making your code robust to changes. Everything should be need-to-know basis, and you should keep things as self-contained as you can. This lets you put various self-contained functionality together with some simple hooks.

  8. #8
    Junior Member
    Join Date
    Sep 2009
    Posts
    21
    5TonsOfFlax that exactly the piece I didn't understand. I worked under the wrong assumption that one can control the root from inside movie clips with no problem.

    Thank you very very much for the great explanation. I'm going to frame your post above (:
    case solved!
    Last edited by red-T; 08-23-2010 at 12:55 PM.

  9. #9
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You actually can have the children control the main timeline, but generally you shouldn't.

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