Skip to main content
Participant
January 9, 2020
Answered

Trying to reference dynamically created children on the stage

  • January 9, 2020
  • 2 replies
  • 497 views

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?

This topic has been closed for replies.
Correct answer ClayUUID

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.

2 replies

ClayUUIDCorrect answer
Legend
January 9, 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 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.

MoocowAuthor
Participant
January 9, 2020

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

Inspiring
January 9, 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;

MoocowAuthor
Participant
January 9, 2020

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.