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

Add symbol to stage in JS and push to array

Community Expert ,
Jun 24, 2022 Jun 24, 2022

Trying to dynamically add symbols from library to an array using JS. Using the code snippet to add one instance, but multiple instances causes problems with the array. Here's my code:

 

var newCoin = new lib.Coin();

 

function loadCoins(){
     for(let i = 0; i < 6; i++){
          myCoins.push(newCoin);
          root.addChild(myCoins[i]);
     }
}
loadCoins();

TOPICS
Code , How to
343
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

correct answers 1 Correct answer

Community Expert , Jun 24, 2022 Jun 24, 2022

Hi.

 

Please try this:

var root = this;
var myCoins = [];

function loadCoins()
{
	var newCoin;
	
	for(let i = 0; i < 6; i++)
	{
		newCoin = new lib.Coin();
		myCoins.push(newCoin);
		root.addChild(newCoin);
	}
}

loadCoins();

 

Regards,

JC

Translate
Community Expert ,
Jun 24, 2022 Jun 24, 2022

Hi.

 

Please try this:

var root = this;
var myCoins = [];

function loadCoins()
{
	var newCoin;
	
	for(let i = 0; i < 6; i++)
	{
		newCoin = new lib.Coin();
		myCoins.push(newCoin);
		root.addChild(newCoin);
	}
}

loadCoins();

 

Regards,

JC

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 Expert ,
Jun 24, 2022 Jun 24, 2022

Thank you, that did the trick. I was convinced that I couldn't have the newCoin object in my function without declaring it first. Also, I needed to have an instance of the symbol already on the stage for the thing to work. (It turns out that if it isn't on the stage, it doesn't get added to the sprite atlas, making the rest of the code hard to run).

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 Expert ,
Jun 24, 2022 Jun 24, 2022

The big catch here is that you must call new lib.Coin() in the for loop. If you call it outside the loop, you're gonna create only one instance and add it six times to the array.

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 Expert ,
Jun 24, 2022 Jun 24, 2022

Which is exactly what I was doing. Thanks again.

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 Expert ,
Jun 24, 2022 Jun 24, 2022
LATEST

You're welcome!

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