Ninjadoodle Posted September 16, 2014 Share Posted September 16, 2014 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! Quote Link to comment Share on other sites More sharing options...
kucira Posted September 16, 2014 Share Posted September 16, 2014 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. Ninjadoodle 1 Quote Link to comment Share on other sites More sharing options...
Ninjadoodle Posted September 16, 2014 Author Share Posted September 16, 2014 Hi @kucira Thank you heaps, it works perfectly I really appreciate you help! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.