#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    3
    Rep Power
    0

    Accessing child of another MovieClip


    Hello,

    Thanks in advance for everyone's help. I have recently started developing in ActionScript and my experience with Adobe Flash is now about 1 week.

    The issue I am having is that I have no idea how to access the Children of another MovieClip. I have 2 MovieClip's, storeFront, at frame 25, and reviewPage, at frame 60. storeFront contains a vector, cart, and I need to access it from reviewPage.

    I have tried...

    var appRoot:MovieClip = parent as MovieClip;
    buff = appRoot.getChildAt(30);
    review = buff.cart;

    and...

    buff = MovieClip(root).getChildAt(30);
    review = buff.cart;

    ... to no avail.

    Here is a tree visual of what's going on:

    -------------------------------Scene 1
    -------------------------------/------\
    ------------------------------/--------\
    -----------------------storeFront-----reviewPage
    --------------------------/---------------\
    ------------------------cart------------review

    I need review to get the values of cart.
  2. #2
  3. No Profile Picture
    Gotta get to the next screen..
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2003
    Location
    Legion of Dynamic Discord
    Posts
    6,678
    Rep Power
    3164
    Hi, both need to exist on the timeline at the same point for you to access the children of both of them. So say that at the moment, storeFront has no frames after number 25, you will be unable to access it when you reach frame 60 as it will no longer exist. In that situation the fix is simply to extend the frames until they reach 60 but create a keyframe on #26 which has some code to set the visible property to false:

    storeFront.visible = false;

    You might need to put the opposite of that on #25 incase you go backwards in your navigations, so the same as above except "= true;".

    Using getChildAt is ok but it can prove tricky if you add or remove any elements from the clip in question. It is far safer to give the child clips instance names. It looks like you have done that already based on your diagram but I thought it was safer to mention it than assume.

    So now that both child clips have instance names you can use getChildByName like so:

    var buff:MovieClip = storeFront.getChildByName("cart") as MovieClip;

    That of course assumes you have given the parent clip the instance name of storeFront.

    Just to be clear on what I mean by instance names. These are not the names that you give the clips in the library. If you place an instance of a library object on the stage and then open the properties panel, one of the fields on there is the Instance Name field. That is the one I mean. It's blank as default.
    Quis custodiet ipsos custodes?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    3
    Rep Power
    0
    Originally Posted by Tann San
    Hi, both need to exist on the timeline at the same point for you to access the children of both of them. So say that at the moment, storeFront has no frames after number 25, you will be unable to access it when you reach frame 60 as it will no longer exist. In that situation the fix is simply to extend the frames until they reach 60 but create a keyframe on #26 which has some code to set the visible property to false:

    storeFront.visible = false;

    You might need to put the opposite of that on #25 incase you go backwards in your navigations, so the same as above except "= true;".

    Using getChildAt is ok but it can prove tricky if you add or remove any elements from the clip in question. It is far safer to give the child clips instance names. It looks like you have done that already based on your diagram but I thought it was safer to mention it than assume.

    So now that both child clips have instance names you can use getChildByName like so:

    var buff:MovieClip = storeFront.getChildByName("cart") as MovieClip;
    Thanks for your help.

    This somewhat worked. To clarify though, 'Scene 1' is the parent, and reviewPage and storeFront are siblings. This is what I tried:

    var buff:MovieClip = parent.getChildByName("storeFront") as MovieClip;

    I was able to pass the entire storeFront MC to reviewPage. Problem I am having now is to access the 'cart' variable inside storeFront.

    The issue I am having now is that reviewPage gets initialized at the very beginning of the movie. This is an issue because during runtime, the user adds items to the variable 'cart' (which is a Vector.<String>) and therefore only a NULL 'cart' is passed to reviewPage.

    I have tried to stop(); and reviewPage.enabled = false; but neither seem to work.
    Last edited by balajiam; August 30th, 2011 at 09:12 PM. Reason: to clarify
  6. #4
  7. No Profile Picture
    Gotta get to the next screen..
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2003
    Location
    Legion of Dynamic Discord
    Posts
    6,678
    Rep Power
    3164
    Well, how about instead of trying to pass around MovieClip references we just create the cart variable at the top most level and then we can just refer to it there:
    Code:
    import flash.display.MovieClip;
    var appRoot:MovieClip = parent as MovieClip
    appRoot.cart = new Vector.<String>();
    appRoot.cart.push("Cats");
    appRoot.cart.push("Dogs");
    Then later on in the other clip you can just access it in the same way:
    Code:
    import flash.display.MovieClip;
    var appRoot:MovieClip = parent as MovieClip
    if(appRoot.cart[0] == "Cats") { trace("Meow"); }
    Quis custodiet ipsos custodes?
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    3
    Rep Power
    0
    Thanks for your help. That did the trick!

    Coming from a background in C++, I have an aversion to global variables. But in Flash, it's better to have global variables than to attempt crazy passes. Lesson learned.
  10. #6
  11. No Profile Picture
    Gotta get to the next screen..
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2003
    Location
    Legion of Dynamic Discord
    Posts
    6,678
    Rep Power
    3164
    Well, to be honest, the proper way to do it would be to have a document class and handle things through that. Under the hood, Flash is creating one for you automatically since you have not defined one yourself. Global variables are a bit more tricky to make in AS3 than they were in AS2. Usually with AS3 you would create a static variable and then use a static getter/setter to access it. In AS2 you didn't need to do that and could just use _root as an easy access system. The use of static variables/properties can be quite a divider amongst people. Some people swear by them whilst others avoid them like the plague. I use them but only when necessary and usually just as a cheat to avoid type casting and the additional code required for that.

    The document class on its own wouldn't really do anything more than you already have but following on from that you could create your own classes for each of your sub clips. Then you can create functions in those classes which allow you to pass the data in a nice way instead of having to do type casts all over the place.

    Personally, I rarely use purely timeline based animations anymore. I probably did 3-5 in the entire past year. I do use hybrids every now and then, so I'd have a timeline based animation for a preloader but then I'd use code to actually control it in time with the actual loading process. For code based tweening I highly recommend TweenMax.
    Quis custodiet ipsos custodes?

IMN logo majestic logo threadwatch logo seochat tools logo