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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.