Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How Can I Create Multiple Symbol Children With Code?

Community Beginner ,
Dec 24, 2016 Dec 24, 2016

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;

}

366
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Dec 24, 2016 Dec 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;

}

Translate
Community Expert ,
Dec 24, 2016 Dec 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;

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 24, 2016 Dec 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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 24, 2016 Dec 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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 24, 2016 Dec 24, 2016

kglad-

My error. I modified something in the code that generated an error.

Thanks for your assistance!!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 24, 2016 Dec 24, 2016
LATEST

you're welcome.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines