Jump to content

Sprite.destroy() throwing error: Cannot read property 'world' of null


Robotic Soldier Grungram
 Share

Recommended Posts

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

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

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

  • 5 weeks later...
  • 1 year later...

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

  • 7 months later...

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 = [];}
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...