PDA

View Full Version : "instance names"



vintage
July 30th, 2007, 05:32 PM
Finally I started to learn some basic as3 coding and trying to understand the frequently used stuff.
So Im trying the "attachMovie" in as,..best of all, it works :D
But I have this little question about the names that flash gives the attached movies.

I made this little script:

for(var i:Number=0; i<5; i++){

var mc = new MovieClip();
mc = addChild(new Ding()); //a mc with classname Ding in the library
mc.x = i*20
mc.y = i*50;
mc.addEventListener(MouseEvent.CLICK, actie);

}

function actie(event:MouseEvent):void{
trace(event.target.name)
}

Traces:
instance2
instance5
instance8
instance11
instance14


Can anyone explain the logic why the numbers add up by 3 ?
And can I use these target names for anything usefull ? (I prolly still think to much as2)

devonair
July 30th, 2007, 07:56 PM
hmmm.. very interesting.. Part of the explanation is that you're creating a MovieClip instance, and then converting it to a Ding instance - so there's two instances there..

So in each loop you're getting something like:

Loop 1:
instance1 = MovieClip
instance2 = Ding
instance3 = [Something - don't know what]

Loop 2
instance4 = MovieClip
instance5 = Ding
instance6 = [Something]

If you get rid of the MovieClip instance and just say:
for (var i:int = 0; i < 5; i++) {
var mc:Ding = new Ding();
addChild(mc);
mc.x = i * 20;
mc.y = i * 50;
mc.addEventListener(MouseEvent.CLICK, actie);
}
Then the Ding instance are only every other instance instead of every third..

I'd still like to know what that "something" is though.. The only guess I have is that since Ding is extending a MovieClip library instance each constructor actually makes both movieclip and ding instances.. seems odd though... I'm gonna play around some more..

pensamente
August 2nd, 2007, 08:27 AM
i'm trying to get the train to as3 also :)

feel stuck to understand the logic of mc instances.
as you show us flash gives instances names automatically, beside that i'm trying to focus how to give a name that will identify that clip so you can call it later. So i create a new var mc = new myclip () and then set the properties of this one using the name of the var mc.x = 30.
Suppose you overwrite the mc instace name to
mc.name = "mc_1"

then you can change it's properties like so
getChildByName ('mc_1').x = 200

but now imagine this, myclip is a mc i imported from library that contains a textField inside with the instance name myTextField, how can i set the text property of this?!

if it was set when creating the object it would be
mc.myTextField.text = "getIt"

can't figure out this... any clue

thx
am