Jump to content

Destroy sprite from group in update - TypeError: Cannot read property 'alive' of null


Recommended Posts

I got this setup:

update: function() { this.balls.forEach(this.ballUpdate, this);}
ballUpdate: function(ball) {if (ball.body.y > this.game.world.height + ball.height) {ball.destroy();}}

Which results in:


  1. Uncaught TypeError: Cannot read property 'alive' of null Game.js:566
    1. BasicGame.Game.ballUpdateGame.js:566
    2. Phaser.Group.forEachphaser.js:12324
    3. BasicGame.Game.updateGame.js:173
    4. Phaser.StateManager.updatephaser.js:10138
    5. Phaser.Game.updatephaser.js:13701
    6. Phaser.RequestAnimationFrame.updateRAFphaser.js:22114
I tried: 
update: function() {   if(this.balls.countLiving() > 0) {      this.balls.forEach(this.ballUpdate, this);  }}
Link to comment
Share on other sites

Heh, I have run into a similar problem. I have a group of display objects (text, sprites and buttons) that I want to destroy before removing everything in the group:

this.group.forEach(function(obj) {    if (obj.kill)        obj.kill();    else if (obj.destroy)        obj.destroy();});this.group.removeAll();

This results in an "Type Error: obj is null".


EDIT: I'm pretty sure this happens because Sprite.destroy now removes the sprite from its group. So you shouldn't destroy objects in a group with forEach

Link to comment
Share on other sites


  • Recently Browsing   0 members

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