hustlerinc

destroy() specific item from group?

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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);

 

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...

  • Recently Browsing   0 members

    No registered users viewing this page.