Highlighted

HTML Canvas - Addchild from a string/array?

Community Beginner ,
Feb 11, 2019

Copy link to clipboard

Copied

Hi, is it possible to addChild from a variable?

My end goal is to add and remove children based on a random array number.

Here is my test code but it is obviously looking for a linkage called P in the library (needs to load libImage1):

var _root = this;

var ButtNumbers = ["1", "2", "3"];

this.blackbutton.addEventListener('click',clickLoadLmage.bind(this));

function clickLoadLmage()

{

var libImage1 = new lib.Butt1();

var libImage2 = new lib.Butt2();

var libImage3 = new lib.Butt3();

var T = ButtNumbers[0];

var P = "libImage"+T;

_root.blackbutton.addChild(P);

}

Views

95

Likes

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

HTML Canvas - Addchild from a string/array?

Community Beginner ,
Feb 11, 2019

Copy link to clipboard

Copied

Hi, is it possible to addChild from a variable?

My end goal is to add and remove children based on a random array number.

Here is my test code but it is obviously looking for a linkage called P in the library (needs to load libImage1):

var _root = this;

var ButtNumbers = ["1", "2", "3"];

this.blackbutton.addEventListener('click',clickLoadLmage.bind(this));

function clickLoadLmage()

{

var libImage1 = new lib.Butt1();

var libImage2 = new lib.Butt2();

var libImage3 = new lib.Butt3();

var T = ButtNumbers[0];

var P = "libImage"+T;

_root.blackbutton.addChild(P);

}

Views

96

Likes

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
Feb 11, 2019 0
Adobe Community Professional ,
Feb 11, 2019

Copy link to clipboard

Copied

Yes, it is possible. No, it's not working because P is just a string. addChild() doesn't accept a string.

This should guide you to the correct syntax:

Property accessors - JavaScript | MDN

Likes

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
Reply
Loading...
Feb 11, 2019 1
Community Beginner ,
Feb 11, 2019

Copy link to clipboard

Copied

try

var P = this["libImage"+T];

Likes

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
Reply
Loading...
Feb 11, 2019 1