Sign in to follow this  
Ninjadoodle

How to reference a sprite in a for loop? (SOLVED)

Recommended Posts

Hi guys

 

I have a bunch of sprites I want to loop through, but I can't figure out how to correctly reference them, in order to print out their values.

var ninja0 = new game.Sprite('ninja.png');ninja0.anchor.set(0.5, 1);ninja0.position.set(100, 320);mg.addChild(ninja0);var ninja1 = new game.Sprite('ninja.png');ninja1.anchor.set(0.5, 1);ninja1.position.set(200, 320);mg.addChild(ninja1);var ninja2 = new game.Sprite('ninja.png');ninja2.anchor.set(0.5, 1);ninja2.position.set(300, 320);mg.addChild(ninja2);for (i=0; i<3; i++) {     console.log(['player'+i].x);}

I can't figure out what to put in front of ['player'+i].x to get this to work :(

 

Thank you in advance for any help!

Share this post


Link to post
Share on other sites

You should put your object to an Array.

var ninjas = [];var ninja0 = new game.Sprite('ninja.png');ninja0.anchor.set(0.5, 1);ninja0.position.set(100, 320);mg.addChild(ninja0);ninjas.push(ninja0);var ninja1 = new game.Sprite('ninja.png');ninja1.anchor.set(0.5, 1);ninja1.position.set(200, 320);mg.addChild(ninja1);ninjas.push(ninja1);var ninja2 = new game.Sprite('ninja.png');ninja2.anchor.set(0.5, 1);ninja2.position.set(300, 320);mg.addChild(ninja2);ninjas.push(ninja2);for (i=0; i< ninjas.length; i++) {     console.log(ninjas[i].position.x);} 

Hope it works. :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.