Skip to main content
Participant
May 20, 2008
Answered

How to create objects with names being set "on the fly"?

  • May 20, 2008
  • 1 reply
  • 354 views
Hi everyone,

I don't know how to create objects with varying suffixes in their references "constructed" on the fly via "for" loop. What I mean is, the "typical" way of creating few sprites could be:

var sprite1:Sprite = new Sprite();
var sprite2:Sprite = new Sprite();
container.addChild(sprite1);
container.addChild(sprite2);

and what if we want to create 100 such objects using "for" loop so that we don't need to type:

var sprite1:Sprite = new Sprite();
var sprite2:Sprite = new Sprite();
var sprite3:Sprite = new Sprite();
var sprite4:Sprite = new Sprite();
...
var sprite100:Sprite = new Sprite();

the question is how to "incorporate" the number into object's instance name during mass object creation? I remember some old "eval" and/or square brackets '["objectName" + objectId]' magic spells but they don't seem to work in AS3

Greetings
This topic has been closed for replies.
Correct answer Damon Edwards
:

1 reply

Ned Murphy
Legend
May 20, 2008
Neither do I, but the link below provides a couple of alternatives. I'll be happy to see a response that addresses your posting with the answer I know you are looking for...

http://www.ultrashock.com/forums/actionscript/as3-dynamic-variable-instance-naming-93105.html
Damon Edwards
Damon EdwardsCorrect answer
Inspiring
May 20, 2008
: