Sprite - How to load the correct image dynamically?

Title might be confusing.

In resources.js, I have initialized few images.
For example, image_0_0: folder + '1.png' and so on. Folder is used to load image2|3|4x

this.initWithFile(image_0_0); 

will work good.

But i want to load the images randomly.

var rand = Math.floor(Math.random() * 20);
this.initWithFile(image_rand_rand) //it will not work. 

But how can i reference the image?

image_rand_rand is trying to pass a variable with that literal name. You need to pass in the actual image name. This might be folder + rand + rand + '.png' or 'image' + rand + rand + '.png' or something similar, but I’m not sure your exact variable setup.

1 Like

Yes. I need to add that folder variable and call the image. Thanks :smile: