Search the Community
Showing results for tags 'CollisionGroup destroy kill'.
-
I'm trying to handle "killing" enemies, in my case aliens, when they collide with the player's bullets. It feels like I'm doing the right thing when I use the destroy method on the body when they collide except my update functions are trying to move both the enemy and the bullet after they've been destroyed. Obviously, this causes a runtime error because the body is no longer in the collection. I'm using removeFromWorld() at the moment to avoid the runtime issue but my sprites just stop moving on screen. I'd like to remove them from the canvas (and play an explosion at some point). How do I remove the body from the group/world/etc. properly? Here's the essential bits of code: game.physics.p2.setImpactEvents(true);game.physics.p2.restitution = 0.8;alienCollisionGroup = game.physics.p2.createCollisionGroup();bulletCollisionGroup = game.physics.p2.createCollisionGroup();function destroyEnemy(body1, body2){ body1.removeFromWorld(); //body1.destroy(); body2.removeFromWorld(); //body2.destroy(); console.log(body1.toString());}// ENEMIESaliens = game.add.group();aliens.enableBody = true;aliens.physicsBodyType = Phaser.Physics.P2JS;for (var i = 0; i < 10; i++) { var alien = aliens.create(game.rnd.integerInRange(200, 1700), game.rnd.integerInRange(-200, 400), 'alien'); alien.body.setCollisionGroup(alienCollisionGroup); alien.body.collides([alienCollisionGroup, playerCollisionGroup]); alien.body.collides([alienCollisionGroup, bulletCollisionGroup]); alien.animations.add('left',[1], 3, true); alien.animations.add('right',[0],3,true); game.physics.p2.enable(alien,false);}var Bullet = function (game, key) { Phaser.Sprite.call(this, game, 0, 0, key); this.texture.baseTexture.scaleMode = PIXI.scaleModes.NEAREST; this.anchor.set(0.5); this.checkWorldBounds = true; this.outOfBoundsKill = true; this.exists = false; this.tracking = true; this.scaleSpeed = 0;};Bullet.prototype = Object.create(Phaser.Sprite.prototype);Bullet.prototype.constructor = Bullet;Bullet.prototype.fire = function (x, y, angle, speed, gx, gy) { gx = gx || 0; gy = gy || 0; this.body.setCollisionGroup(bulletCollisionGroup); this.body.collides(alienCollisionGroup, destroyEnemy, this); this.reset(x, y); this.scale.set(1); this.angle = angle; this.body.rotation = 0; this.body.force.x = Math.cos(angle) * speed; //this.body.force.y = Math.sin(angle) * speed; this.body.gravity.set(gx, gy);};Bullet.prototype.update = function () { if (this.tracking){ this.rotation = Math.atan2(this.body.velocity.y, this.body.velocity.x); //<-Runtime issue } if (this.scaleSpeed > 0){ this.scale.x += this.scaleSpeed; this.scale.y += this.scaleSpeed; }};Many thanks in advance