Copy link to clipboard
Copied
I want to create multiple instances of a movieclip (linkage name "my_circle") and control their positions with code. I want to name the instances circle1, circle2, circle3, etc. I tried using EVAL with the code below. It doesn't work.
var itemName = "circle";
var evalName;
for (var i = 1; i < 5; ++i) {
evalName = eval("itemName + i");
evalName = new lib.my_circle(); // create instance
evalName.x = Math.floor(Math.random() * 400); // position instance
}
this.addEventListener("tick", fl_AnimateHorizontally.bind(this));
function fl_AnimateHorizontally() {
circle1.x+=10;
}
one way:
var itemName = "circle";
for (var i = 1; i < 5; ++i) {
var circle = new lib.my_circle(); // create instance
circle.name = itemName + i;
circle.x = Math.floor(Math.random() * 400); // position instance
this.addChild(circle);
}
this.addEventListener("tick", fl_AnimateHorizontally.bind(this));
function fl_AnimateHorizontally() {
this.getChildByName("circle1").x+=10;
}
Copy link to clipboard
Copied
one way:
var itemName = "circle";
for (var i = 1; i < 5; ++i) {
var circle = new lib.my_circle(); // create instance
circle.name = itemName + i;
circle.x = Math.floor(Math.random() * 400); // position instance
this.addChild(circle);
}
this.addEventListener("tick", fl_AnimateHorizontally.bind(this));
function fl_AnimateHorizontally() {
this.getChildByName("circle1").x+=10;
}
Copy link to clipboard
Copied
kglad-
That successfully created the instances. Thank you! However, this part of your code generates an error:
this.getChildByName("circle1").x+=10;
Isn't "getChildByName" AS3 code?
Copy link to clipboard
Copied
it's the same for as3 and easeljs, EaselJS v0.8.2 API Documentation : MovieClip so that line of code would not generate an error.
what makes you think it's a problem?
Copy link to clipboard
Copied
kglad-
My error. I modified something in the code that generated an error.
Thanks for your assistance!!
Copy link to clipboard
Copied
you're welcome.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now