phaserjs / phaser

Phaser is a fun, free and fast 2D game framework for making HTML5 games for desktop and mobile web browsers, supporting Canvas and WebGL rendering.
https://phaser.io
MIT License
36.95k stars 7.08k forks source link

Phaser.Particles.Arcade.Emitter.prototype.destroy - Uncaught TypeError: Cannot read property 'particles' of null #1991

Closed nickryall closed 8 years ago

nickryall commented 9 years ago

When calling destroy() on an Emitter, I get the following error on this line:

93476: this.game.particles.remove(this);

Uncaught TypeError: Cannot read property 'particles' of null

I'm guessing the reference to this.game is lost when creating a new emitter.

photonstorm commented 9 years ago

I can't replicate this. The example below works fine. What is different about your version to throw this?


var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create });

var emitter;

function preload() {

    game.load.image('diamond', 'assets/sprites/diamond.png');

}

function create() {

    game.physics.startSystem(Phaser.Physics.ARCADE);

    game.stage.backgroundColor = 0x337799;

    emitter = game.add.emitter(0, 0, 100);

    emitter.makeParticles('diamond');
    emitter.gravity = 200;

    game.input.onDown.addOnce(particleBurst, this);

}

function particleBurst(pointer) {

    emitter.x = pointer.x;
    emitter.y = pointer.y;

    emitter.start(true, 4000, null, 10);

    //  And 2 seconds later we'll destroy the emitter
    game.time.events.add(2000, destroyEmitter, this);

}

function destroyEmitter() {

    emitter.destroy();

}
nickryall commented 9 years ago

Sorry I totally missed your reply to this. I will look into it further and report back.

nickryall commented 8 years ago

This was indeed a bug in my own code. Long story short, I was calling destroy() on the emitter instance twice in quick succession. So the 2nd time around as expected this.game = null.

Sorry about that. Thanks for taking the time to take a look.