Hi all,
I’m building a small prototype game where I want to remove objects from a SpriteGroup when you touch them. I’ve defined a group and add a sprite to this group each x seconds. When clicking the sprite it should be removed from the group and be destroyed as well. Unfortunately I seem to be running into an issue where the sprite is still being referenced after I’ve destroyed it. Here’s the code:
function create () {
this.balloons = this.add.group({
classType: Phaser.GameObjects.Sprite,
active: true,
});
this.spawnBalloonTimer = this.time.addEvent({ delay: 2000, callback: spawnBalloon, callbackScope: this, loop: true });
this.input.on('pointerdown', function(pointer){
var touchX = pointer.x;
var touchY = pointer.y;
this.balloons.children.iterate(function(child) {
if (child.getBounds().contains(touchX, touchY)) {
this.balloons.remove(child, true);
}
}, this)
}, this);
}
spawnBalloon() {
let balloon = this.physics.add.sprite(randomX, 575, randomBalloon);
balloon.setInteractive();
balloon.body.setGravityY(-200);
balloon.setScale(scaleRatio, scaleRatio);
this.balloons.add(balloon);
}
This throws the following error Uncaught TypeError: Cannot read property 'getBounds' of undefined
, but the balloon is removed from the group and from the screen. I checked with this.balloons.getLength()
. Am I doing something wrong?