Skip to main content
Inspiring
December 24, 2016
Answered

How Can I Create Multiple Symbol Children With Code?

  • December 24, 2016
  • 1 reply
  • 438 views

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;

}

This topic has been closed for replies.
Correct answer kglad

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;

}

1 reply

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
December 24, 2016

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;

}

Inspiring
December 24, 2016

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?

kglad
Community Expert
Community Expert
December 24, 2016

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?