Robotic Soldier Grungram Posted March 4, 2014 Share Posted March 4, 2014 I'm trying to remove sprites from a group using destroy() whenever the sprite goes out of the world bounds, but I'm getting an unexpected error: Cannot read property 'world' of null. Any idea why this is happening? function create() { buildings = game.add.group() var building = buildings.create(0, 0, 'building') building.body.velocity.y += 90 building.events.onOutOfBounds.add(onBuildingOutOfBounds, this)}function onBuildingOutOfBounds(building) { building.destroy() console.log(buildings.length)} Link to comment Share on other sites More sharing options...
Sebi Posted March 4, 2014 Share Posted March 4, 2014 The sprite is probably in the update loop and after you destroyed it, pixi still wants to update it.So maybe destroy it in the next tick and not in the same tick in which you updated the sprite. (unless this is an phaser issue but I have that kind of issues with pixi too when trying to kill a sprite that was just updated) Link to comment Share on other sites More sharing options...
Robotic Soldier Grungram Posted March 4, 2014 Author Share Posted March 4, 2014 The sprite is probably in the update loop and after you destroyed it, pixi still wants to update it. So maybe destroy it in the next tick and not in the same tick in which you updated the sprite. (unless this is an phaser issue but I have that kind of issues with pixi too when trying to kill a sprite that was just updated) This does indeed seem to be the issue. Is there an example that shows how to check for the next tick? Link to comment Share on other sites More sharing options...
gikdew Posted April 3, 2014 Share Posted April 3, 2014 How can i solve this?? Link to comment Share on other sites More sharing options...
Heppell08 Posted April 3, 2014 Share Posted April 3, 2014 The destory of the sprite is removing it from the world group. The world is a group itself so maybe you might want to try removing it from the group with:Sprite.group.remove(sprite);That will remove it from the given group that you want to remove it from. Link to comment Share on other sites More sharing options...
GourmetGorilla Posted May 6, 2015 Share Posted May 6, 2015 Heppell08 - a little dumb here, just learning phaser. In your example, what do I replace here? And what do I replace it with? Sprite.group.remove(sprite);My object is called alien, in a group called aliens, with a texture called 'cured'. How do I write the above for my situation?I'm confused, what is a sprite in phaser, is it the image attached to the object, or the object itself? Link to comment Share on other sites More sharing options...
sszark Posted January 3, 2016 Share Posted January 3, 2016 I know this is an old thread but I recently stumbled upon the issue as well and if someone is still looking for a solution, this is how I did it. Create a graveyard:var graveyard = [];Kill and push the object you want to destroy to the graveyard:object.kill();graveyard.push(object);In your update function clear the graveyard in the next pass:function update(){ //Your Code ... for(var key in graveyard) graveyard[key].destroy(); graveyard = [];} webdva 1 Link to comment Share on other sites More sharing options...
Recommended Posts