hustlerinc Posted August 8, 2014 Share Posted August 8, 2014 Hi, I just started playing around with Phaser and I need some help. I have a group named coins, and I want to delete each coin inside if it's x position is less than 0. I add the coins like this:for (i = 0; i < coinArray.length; i++) { var coin = coins.create(game.world.width + coinArray[i].x, coinArray[i].y, 'coin'); coin.body.velocity.x -= 250;}How can I do this? Link to comment Share on other sites More sharing options...
Dumtard Posted August 8, 2014 Share Posted August 8, 2014 group.forEach(function(coin) { if (coin.x < 0) { coin.destroy(); } }, this); Link to comment Share on other sites More sharing options...
hustlerinc Posted August 8, 2014 Author Share Posted August 8, 2014 group.forEach(function(coin) { if (coin.x < 0) { coin.destroy(); }}, this);I tried your code (changed group to coins) but the script dies when the first coin reaches the border and I get "Cannot read property 'x' of undefined". I updated my first post to show how the coins are added. Link to comment Share on other sites More sharing options...
Heppell08 Posted August 8, 2014 Share Posted August 8, 2014 function coinKill(coin){ if(coin.x < 0) {coin.destroy();} }Its how I kill coins but its similar to collide coding the coins. You shouldn't need to reference the actual group itself because you can reference the children (coin) instead. Link to comment Share on other sites More sharing options...
hustlerinc Posted August 8, 2014 Author Share Posted August 8, 2014 function coinKill(coin){ if(coin.x < 0) {coin.destroy();} }Its how I kill coins but its similar to collide coding the coins. You shouldn't need to reference the actual group itself because you can reference the children (coin) instead.For some reason as soon as I try to look at a property of "coin" I get the same error. It works fine for collision between player and coin where I do this:function collectCoin (player, coin) { coin.destroy();}game.physics.arcade.overlap(player, coins, collectCoin, null, this);but when I try your code I get the same undefined error. What am I missing? Link to comment Share on other sites More sharing options...
Heppell08 Posted August 8, 2014 Share Posted August 8, 2014 Place coin as global and not anon in your create function. Link to comment Share on other sites More sharing options...
hustlerinc Posted August 8, 2014 Author Share Posted August 8, 2014 Place coin as global and not anon in your create function.You mean something like just "var coin;" right? Doesn't work. And if that was the problem my collectCoin() function wouldn't work either. Also I have to create the coins in update() as it's random. The create function is just called once right? Link to comment Share on other sites More sharing options...
Heppell08 Posted August 8, 2014 Share Posted August 8, 2014 Hmm, well if even making it global doesn't help them maybe trying to user a getAt() in a loop and cycling them. I would have thought a forEach would do the job or a getFirstDead() cycling the group would do the work for you. Link to comment Share on other sites More sharing options...
Heppell08 Posted August 8, 2014 Share Posted August 8, 2014 Maybe set them to out of bounds kill so when they go off screen they are killed that way? Link to comment Share on other sites More sharing options...
hustlerinc Posted August 8, 2014 Author Share Posted August 8, 2014 Maybe set them to out of bounds kill so when they go off screen they are killed that way?How would I do that? I just started learning Phaser today. Link to comment Share on other sites More sharing options...
Dumtard Posted August 8, 2014 Share Posted August 8, 2014 Try this instead of my original post.for (var i = 0; i < coins.children.length;) { if (coins.children[i].x < -coins.children[i].width) { coins.remove(coins.children[i], true); continue; } i++;}jsfiddle of it working. Basically what happened with my first post is classic example of removing from a list while iterating over it. It screws with the indexing. I originally meant coins.remove(coin, true) figuring the group would handle the removed coins but it also was getting undefined coins. So just went with a more classic approach to the problem, handling the loop indexing myself instead of blindly incrementing like forEach seems to.EDIT:Here is a jsfiddle closer to what I originally meant to do handling the undefined coins. Link to comment Share on other sites More sharing options...
hustlerinc Posted August 8, 2014 Author Share Posted August 8, 2014 Try this instead of my original post.for (var i = 0; i < coins.children.length;) { if (coins.children[i].x < -coins.children[i].width) { coins.remove(coins.children[i], true); continue; } i++;}jsfiddle of it working. Basically what happened with my first post is classic example of removing from a list while iterating over it. It screws with the indexing. I originally meant coins.remove(coin, true) figuring the group would handle the removed coins but it also was getting undefined coins. So just went with a more classic approach to the problem, handling the loop indexing myself instead of blindly incrementing like forEach seems to.Perfect, this does the trick. Thank you. Link to comment Share on other sites More sharing options...
amuseum Posted August 9, 2014 Share Posted August 9, 2014 yea , be careful when you try to delete objects in a foreach loop. sometimes i even work backwards through the indexes. to check and delete the last object in the list first. Link to comment Share on other sites More sharing options...
SatriaAI Posted December 28, 2017 Share Posted December 28, 2017 On 8/9/2014 at 5:30 AM, Dumtard said: Try this instead of my original post. for (var i = 0; i < coins.children.length;) { if (coins.children[i].x < -coins.children[i].width) { coins.remove(coins.children[i], true); continue; } i++;} jsfiddle of it working. Basically what happened with my first post is classic example of removing from a list while iterating over it. It screws with the indexing. I originally meant coins.remove(coin, true) figuring the group would handle the removed coins but it also was getting undefined coins. So just went with a more classic approach to the problem, handling the loop indexing myself instead of blindly incrementing like forEach seems to. EDIT: Here is a jsfiddle closer to what I originally meant to do handling the undefined coins. Does this trick ca be paired with time.events.loop? Like this? Quote this.meLoop = this.game.time.events.loop(this.rndTime, spawn, this); Link to comment Share on other sites More sharing options...
Recommended Posts