Highlighted

Trying to reference dynamically created children on the stage

Community Beginner ,
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?

Adobe Community Professional
Correct answer by ClayUUID | Adobe Community Professional

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.

TOPICS
ActionScript, How to, Other

Views

85

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

Trying to reference dynamically created children on the stage

Community Beginner ,
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?

Adobe Community Professional
Correct answer by ClayUUID | Adobe Community Professional

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.

TOPICS
ActionScript, How to, Other

Views

86

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

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...
Jan 09, 2020 1
Community Beginner ,
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.

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...
Jan 09, 2020 0
Adobe Community Professional ,
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.

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...
Jan 09, 2020 1
Moocow LATEST
Community Beginner ,
Jan 09, 2020

Copy link to clipboard

Copied

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

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