all groups > flash actionscript > august 2007 >
You're in the

flash actionscript

group:

addressing long chains of nested clips in AS3


addressing long chains of nested clips in AS3 SummerLongSince
8/28/2007 9:09:35 PM
flash actionscript:
Hi,
this is probably a very easy topic, but i haven't been able to figure it out
myself.
In my example i have a class that creates 3 clips (sp1, sp2, sp3) and places
them on the stage. In addition they are nested:
sp3 inside sp2,
and sp2 inside sp1.

From my main flash movie I am trying to address those clips. it works it if I
do things like:
sp3.x=70';
sp2.y=100;
etc.

But I want to know how I can address clips in a fashion similar to what it
used to be possible in AS2.

If i type this:
sp1.sp2.sp3.x=70;
I get this error message: TypeError: Error #1010: A term is undefined and has
no properties.

What is the new way to do this please?

Re: addressing long chains of nested clips in AS3 SummerLongSince
8/28/2007 9:19:08 PM
well, i kept poking around and found a way to address the nested clips in a
long chain. It just takes too many lines of code (at least 7) for something
that before could be done in 1 line. So I'm not sure it this is the best
approach.

In the class definition i created names for each of the clips (using the name
property).

and then in my main flash movie: I coded this:

var tempClip:*;
var tempClip2:*;
tempClip = sp1.getChildByName('sp2');
tempClip2 = tempClip.getChildByName('sp3');
tempClip2.x=70;

Is there a better approach? Thanks.


Re: addressing long chains of nested clips in AS3 SymTsb
8/29/2007 12:24:43 AM
Try this....

MovieClip(sp1).MovieClip(sp2).MovieClip(sp3).x = 70;

Personal though, if you are creating your clips on the main timeline of your
movie using var = new, it is so much easier to simply access the clip in
question using it's variable name in AS3 rather than going through the messes
of accessing nested clips.
Re: addressing long chains of nested clips in AS3 SummerLongSince
8/29/2007 12:45:12 AM
hi SymTsb,

Thanks for your suggestions. I tried the first one: using

MovieClip(sp1).MovieClip(sp2).MovieClip(sp3).x=70

got this error message:
TypeError: Error #1006: MovieClip is not a function.

If i try this instead:
MovieClip(sp1.sp2.sp3).x = 70;

the error I get is:
TypeError: Error #1010: A term is undefined and has no properties.

I am curious about your 2nd suggestion, but I'm not sure how to implement it.
I wonder if you can expand more on it.

I am thinking on situations when you need to address a nested clip and have
many copies of the parent clip already on stage. In this cases The instance
names of all the parent clips will always be different, but the nested clip may
all have the same instance name. So you need a way to specifically address the
nested clip of one of many parent clips.
Re: addressing long chains of nested clips in AS3 SymTsb
8/29/2007 2:55:59 AM
My second option refers to using variable declaration with the new operator.
It's the method for creating empty movieclips and attaching them from the
library from AS. Here's the code....



var sp1:MovieClip = new MovieClip();
var sp2:MovieClip = new MovieClip();
var sp3:MovieClip = new MovieClip();

addChild( sp1 );
sp2.addChild( sp3 );
sp1.addChild( sp2 );

If you wanted the x or y value of anyone of these, you can simply make a call
to the variable.property and no matter what the depth on the display list, the
Flash Player will return the proper result. So....

sp3.x = 70; is the same as AS2's sp1.sp2.sp3.x = 70;

Does that make sense?

Achieving the attachMovie method from AS2 is very similar to the above method
but requires giving a library item a unique class name and calling that class
name after new instead of MovieClip(); so...

Under properties for a library symbol, make sure Export For Actionscript is
selected and give your symbol a Class name i.e. mySymbolClass.
var myNamedClip = new mySymbolClass();

I hope this helps.
Re: addressing long chains of nested clips in AS3 SummerLongSince
8/29/2007 3:54:53 AM
this works.
AddThis Social Bookmark Button