Flash Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsWeb DesignFlash Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 29th, 2011, 10:49 PM
balajiam balajiam is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2011
Posts: 3 balajiam User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 23 m 15 sec
Reputation Power: 0
ActionScript 3 - 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.

Reply With Quote
  #2  
Old August 30th, 2011, 05:10 AM
Tann San Tann San is offline
Gotta get to the next screen..
Dev Shed God 4th Plane (6500 - 6999 posts)
 
Join Date: Nov 2003
Location: Legion of Dynamic Discord
Posts: 6,663 Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)  Folding Points: 14767 Folding Title: Novice Folder
Time spent in forums: 1 Month 1 Week 3 Days 20 h 11 m 52 sec
Reputation Power: 3163
Facebook MySpace
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?

Reply With Quote
  #3  
Old August 30th, 2011, 09:11 PM
balajiam balajiam is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2011
Posts: 3 balajiam User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 23 m 15 sec
Reputation Power: 0
Quote:
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

Reply With Quote
  #4  
Old August 31st, 2011, 03:23 AM
Tann San Tann San is offline
Gotta get to the next screen..
Dev Shed God 4th Plane (6500 - 6999 posts)
 
Join Date: Nov 2003
Location: Legion of Dynamic Discord
Posts: 6,663 Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)  Folding Points: 14767 Folding Title: Novice Folder
Time spent in forums: 1 Month 1 Week 3 Days 20 h 11 m 52 sec
Reputation Power: 3163
Facebook MySpace
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"); }

Reply With Quote
  #5  
Old August 31st, 2011, 03:22 PM
balajiam balajiam is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2011
Posts: 3 balajiam User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 23 m 15 sec
Reputation 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.

Reply With Quote
  #6  
Old August 31st, 2011, 06:09 PM
Tann San Tann San is offline
Gotta get to the next screen..
Dev Shed God 4th Plane (6500 - 6999 posts)
 
Join Date: Nov 2003
Location: Legion of Dynamic Discord
Posts: 6,663 Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)  Folding Points: 14767 Folding Title: Novice Folder
Time spent in forums: 1 Month 1 Week 3 Days 20 h 11 m 52 sec
Reputation Power: 3163
Facebook MySpace
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignFlash Help > ActionScript 3 - Accessing child of another MovieClip

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap