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

HTML Canvas - Addchild from a string/array?

Community Beginner ,
Feb 11, 2019 Feb 11, 2019

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);

}

247
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
LEGEND ,
Feb 11, 2019 Feb 11, 2019

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

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 ,
Feb 11, 2019 Feb 11, 2019
LATEST

try

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

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