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

How Can I Create Multiple Symbol Children With Code?

Community Beginner ,
Dec 24, 2016 Dec 24, 2016

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;

}

Views

288

Translate

Translate

Report

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;

}

Votes

Translate

Translate
Community Expert ,
Dec 24, 2016 Dec 24, 2016

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;

}

Votes

Translate

Translate

Report

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

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?

Votes

Translate

Translate

Report

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

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?

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

kglad-

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

Thanks for your assistance!!

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

LATEST

you're welcome.

Votes

Translate

Translate

Report

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