View Full Version : AS 3 instance names
-infect-
08-01-2006, 07:59 PM
I know that you can add a movie clip to the stage using the addChild() function but my question is how can you give a instance name to a dynamicly placed MovieClip?
:beer:
senocular
08-01-2006, 08:03 PM
my_mc.name = "yourNameOfChoice";
-infect-
08-01-2006, 08:16 PM
my_mc being the variable name?
-infect-
08-01-2006, 08:27 PM
What if you want to do several instances of the same MovieClip?
senocular
08-01-2006, 08:32 PM
yes, my_mc being a variable name.
And there's nothing wrong with having several instances of the same movie clip so long as you create a new instance for each instance.
gburks
08-03-2006, 10:50 PM
yes, my_mc being a variable name.
And there's nothing wrong with having several instances of the same movie clip so long as you create a new instance for each instance.
In AS3, I can't seem to use the instance name to assign properties like I could in AS2. For example, I add a child to the display list and give it an instance name. I cannot update its properties using this name. I have to use the variable name instead:
var tempItem = new MyItem();
tempItem.name = "myItem1";
addChild(tempItem);
myItem1.x = 200; // DOESN'T WORK, "Error #1065: Variable myItem1 is not defined.
tempItem.x = 200; // Instead, I have to call it by the variable name.
That is a problem if you want to run through a loop, using that variable to create new instances and assign them names that you would later like to talk to.
Any solutions?
-Graham
Case-Sensitive
08-04-2006, 06:40 AM
Hey,
You might want to try using the bracket or array notation for the instance name. I asked yesterday when i was trying to dynamically create a textfield and when i used trace it gave me an UNDEFINED message.
e.g. try this:
this[myItem1].x = 200;
instead of:
myItem1.x = 200;
senocular
08-04-2006, 10:12 AM
in AS3 instance names are just names, they are not variables like they were in AS1 and AS2. In fact I have yet to use an instance name in AS3 because there usually isn't any reason to. How you reference movie clips is through the variable you first assigned them to when you first created them - basically read what gburks said.
If you want to use an instance name to access a movie clip (or any other kind of display object). Then you have to use
parent.getChildByName("instanceName");
jwopitz
01-04-2007, 10:52 PM
I am a little confused here. How do you have a symbol linked to a class, then inside that symbol you have another symbol in which you have an instance name in the properties panel in the Flash IDE? I am getting 'cannot create property...' error.
I have done this though with success. In the Flash IDE inside some class-linked symbol, I have another mc attached in the timeline with an instance name btn. Now in order to avoid the above mentioned error, i have to do the following:
//inside the class file
var btn:Class;
But then to make use of that is where I run into those errors again. Any suggestions?
Thanks.
Dazzer
01-04-2007, 11:37 PM
that's cuz Class isn't an actual definition.
What is inside your class is a at the least a DisplayObject, and at the most a MovieClip/Sprite
So you should declare it as such. And also must be public.
jwopitz
01-05-2007, 09:25 AM
Yeah, I had read later that in order to that you must declare the class dynamic or that particular property public. I hope Adobe fixes that soon.
bombsaway
09-23-2007, 09:19 AM
That is a problem if you want to run through a loop, using that variable to create new instances and assign them names that you would later like to talk to.
Any solutions?
-Graham
sorry to bring this back from the dead but i have this problem exactly and don't kow how to work around it... posted a thread about it here (http://www.kirupa.com/forum/showthread.php?t=275973) and i've been experimenting and searching like crazy.. anybody know how to get around this?
vBulletin® v3.7.0, Copyright ©2000-2009, Jelsoft Enterprises Ltd.