Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / Flash / General Flash Topics / August 2008



Tip: Looking for answers? Try searching our database.

Quick newbie question on dynamically named variables

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
PTrice - 29 Aug 2008 20:26 GMT
I'm new to AS3.0 and learning the syntax.  I've scoured the help, but cannot
figure out how to get this simple code to work:

for (i=0;i<10;i++) {
var eval("myVar" + i ) :MovieClip
}
how do I create myVar0,myVar1, myVar2 etc
rritchey - 29 Aug 2008 20:47 GMT
If you want to dynamically create movie clips, look at the createEmptyMovieClip
function.

IE:

for(i=0;i<10;i++){
var mc:MovieClip = _root.createEmptyMovieClip("myVar"+i,i);
}

This way, you are using one variable to create many.

OR, attachMovie if you are so inclined...

for(i=0;i<10;i++){
var mc:MovieClip = _root.attachMovie("movieToAttach","myVar"+i,i);
}

To call any of these clips at a later time:

_root["myVar"+i]
PTrice - 29 Aug 2008 21:09 GMT
sorry if I wasn't clear, I'm trying to do this is AS 3.0.

so it seems like I need to declare my variables 1 at a time
then set them to a MovieClip class in my library
then addChild to the root (or a container class.

For example the following code works just fine

var myMovie0:MovieClip = new myClassFromLibrary();
_root.clipContainer_mc.addChild(myMovie0);

I simply cannot figure out the syntax on how to get  ("myMovie" + "0") to
parse out without throwing an error both after the "var" declarer, and in my
addChild method.
rritchey - 30 Aug 2008 12:51 GMT
my apologies, I missed the AS3 in your original question.

The point remains the same, though the syntax differs slightly.

for(var i:uint = 0 ; i < 10 ; i++){

var mc:MovieClip = new myClassFromLibrary();
mc.name = "myMovie"+i;

root.clipContainer_mc.addChild(mc);

}

Then later, you can get that instance again:

root.clipContainer_mc.getChildByName("myMovie"+i);
PTrice - 30 Aug 2008 23:21 GMT
One more quick question:
using your script above, later on I do this:

var XXXX:displayObject = root.clipContainer_mc.getChildByName("myMovie"+i);

at this point I want to make a sub clip of XXXX invisible
XXXX.visible = false; works, and makes the ENTIRE object invisible
what doesn't work is any of these:
root.clipContainer_mc.myMovie3.visible = false; or
root.clipContainer_mc.myMovie3.subMovie.visible = false; or
XXXX.subMovie.visible = false ;

so basiaclly I realize the problem is that AS 3.0 treats "subMovie" as a
property of the XXXX display object instead of a sub clip.
But I cant figure out how to directly access a sub movie clip that has been
added dynamically with addChild();

It really has me puzzled, because I'll debug my movie and see an objects
called:
     _level10.clipContainer_mc.myMovie0
            _level10.clipContainer_mc.myMovie0.subMovie
     _level10.clipContainer_mc.myMovie1
              _level10.clipContainer_mc.myMovie1.subMovie

in the Output window.  But I cannot access these movies even explicitly.
Putting this code in doesn't work:
clipContainer_mc.myMovie1.subMovie.visible = false;

very confusing, because if I drop "myMovie1" on stage.  and then drop
"subMovie" inside it, I can directly access the visible property with the above
code.
rritchey - 31 Aug 2008 16:17 GMT
You should be able to simply call:

root.clipContainer_mc.getChildByName("myMovie"+i).visible = false;

But, you should strictly type your objects.  If myMovie3 is a Movie Clip, then
XXXX should be a MovieClip.

var XXXX:MovieClip =
MovieClip(root.clipContainer_mc.getChildByName("myMovie"+i));
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.