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

Trying to reference dynamically created children on the stage

Community Beginner ,
Jan 09, 2020 Jan 09, 2020

Copy link to clipboard

Copied

I'm using HTML5 and I need to dynamically create some objects from code, then modify them later by name. Right now I have:

var a1 = new lib.symbol3();
a1.name = "A1";
stage.addChild(a1);
var a2 = new lib.symbol3();
a2.name = "A2";
stage.addChild(a2);

 and so on.

But when I try to reference them by name, I get errors saying they are undefined. Even when I console.log them

console.log(stage["A1"]);
console.log(stage["A2"]);

I get undefined. 

Am I doing something wrong?

TOPICS
ActionScript , How to , Other

Views

240

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 2 Correct answers

Contributor , Jan 09, 2020 Jan 09, 2020

May not be the proper answer, but try this as workaround to adress your created clips:

stage.getChildByName('A2');

 

For example to position it to an x-value of 150, try this:

stage.getChildByName('A2').x=150;

Votes

Translate

Translate
LEGEND , Jan 09, 2020 Jan 09, 2020

If you weren't doing anything wrong it would be working, so...  yes.

 

In HTML5 Canvas documents, display objects are JavaScript objects, and are referenced as such. The .name property isn't used at all. So when you use addChild(), the object is pushed into the children collection as an anonymous reference. There are broadly two ways to reference the object from this point: You can keep the references returned when you initially instantiated the objects (your a1 and a2 variables), usually collec

...

Votes

Translate

Translate
Contributor ,
Jan 09, 2020 Jan 09, 2020

Copy link to clipboard

Copied

May not be the proper answer, but try this as workaround to adress your created clips:

stage.getChildByName('A2');

 

For example to position it to an x-value of 150, try this:

stage.getChildByName('A2').x=150;

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 ,
Jan 09, 2020 Jan 09, 2020

Copy link to clipboard

Copied

Amazing! I didn't realize I could do that instead. Thanks for your reply!

However I am still curious if there is a 'correct' way to use bracket notation after creating an symbol on stage from code. Using brackets works just fine for symbols that I place on the stage myself.

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
LEGEND ,
Jan 09, 2020 Jan 09, 2020

Copy link to clipboard

Copied

If you weren't doing anything wrong it would be working, so...  yes.

 

In HTML5 Canvas documents, display objects are JavaScript objects, and are referenced as such. The .name property isn't used at all. So when you use addChild(), the object is pushed into the children collection as an anonymous reference. There are broadly two ways to reference the object from this point: You can keep the references returned when you initially instantiated the objects (your a1 and a2 variables), usually collected in an object or array, or you can create named properties on the container clip that hold the subclip references. So instead of this:

a1.name = "A1";

Do this:

stage.A1 = a1;

 Be careful when doing this not to overwrite any properties that may already exist.

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 ,
Jan 09, 2020 Jan 09, 2020

Copy link to clipboard

Copied

LATEST

Woah it works! I never would have guessed that would be the solution. Thank you for your answers!

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