Jump to content

Need help with Collision


Enviooren
 Share

Recommended Posts

I'm new to programming for the most part so need a little help here to get me back on track.

 

I've used one of the phaser examples/tutorials to create my bullet.prototype sprite. My enemies are normal groups. I cant seem to get a read on the bullet body for my collision handler. I either get Bullet is undefined or no change at all. Any help would be most appreciated!

 

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;
        game.physics.enable(this, Phaser.Physics.ARCADE);
        this.body.setSize(60,60, 26,30);
 
        this.tracking = false;
        this.scaleSpeed = 0;
        };
 
        Bullet.prototype = Object.create(Phaser.Sprite.prototype);
        Bullet.prototype.constructor = Bullet;

 

 

//EnemyClass

greenEnemies = game.add.group();
greenEnemies.enableBody = true;
game.physics.arcade.enable(greenEnemies);
greenEnemies.createMultiple(5, "enemy-green");
greenEnemies.setAll("anchor.x", 0.5);
greenEnemies.setAll("anchor.y", 0.5);
greenEnemies.setAll("scale.x", 0.5);
greenEnemies.setAll("scale.y", 0.5);
greenEnemies.setAll("angle", 180);
greenEnemies.setAll("outOfBoundsKill", true);
greenEnemies.setAll("checkWorldBounds", true);
greenEnemies.forEach(function(enemy){
enemy.body.setSize(enemy.width * 3 / 4, enemy.height * 3 / 4);
});
 

 

game.physics.arcade.overlap(greenEnemies, Bullet, this.hitEnemy, null, this);

 

hitEnemy: function(enemy, Bullet) {

    var explosion = explosions.getFirstExists(false);
    explosion.reset(Bullet.body.x + Bullet.body.halfWidth, Bullet.body.y + Bullet.body.halfHeight);
     explosion.body.velocity.y = enemy.body.velocity.y;
    explosion.alpha = 0.7;
    explosion.play('explosion', 30, false, true);
    enemy.kill();
     console.log(1);
},

 

 

Link to comment
Share on other sites

PhaserGame.prototype = {
 
    create: function() {
 
        var Bullet = function (game, key) {
 
        Phaser.Sprite.call(this, game, 0, 0, key);
        game.physics.enable(Bullet, Phaser.Physics.ARCADE);
 
        this.texture.baseTexture.scaleMode = PIXI.scaleModes.NEAREST;
 
        this.anchor.set(0.5);
 
        this.checkWorldBounds = true;
        this.outOfBoundsKill = true;
        this.exists = false;
 
        this.tracking = false;
        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.reset(x, y);
        this.scale.set(1);
 
        var bulletOffset = 20 * Math.sin(game.math.degToRad(player.angle));
        this.reset(player.x + bulletOffset, player.y -30);
 
 
        this.game.physics.arcade.velocityFromAngle(angle, speed, this.body.velocity);
 
        this.angle = -180;
 
        this.body.gravity.set(gx, gy);
 
        };

 

Everything with the weapon seem to work. I just cant get a read on the Bullet body. 

Link to comment
Share on other sites

Hi,

 

By any chance are you also using the Weapon class, var = Weapon {};

 

If you are you might want to try this in your update function, I'm assuming you are using the Weapon class used in Coding Tips 7, to create different bullet types. Here is what you should try:

 

in update function try this line:

this.game.physics.arcade.overlap(this.weapons[this.currentWeapon], greenEnemies, this.hitEnemy, null, this);

 

then in your hitEnemy function:

 

hitEnemy: function(enemy, Bullet) {

    var explosion = explosions.getFirstExists(false);
    explosion.reset(Bullet.body.x + Bullet.body.halfWidth, Bullet.body.y + Bullet.body.halfHeight);
     explosion.body.velocity.y = enemy.body.velocity.y;
    explosion.alpha = 0.7;
    explosion.play('explosion', 30, false, true);
    enemy.kill();
 
   Bullet.kill();
 
     console.log(1);
},

 

Let me know if this helps or if i caused more confusion : )

Link to comment
Share on other sites

This did the trick! Thanks a lot corpsefilth, very much appreciated  :D

 

Hi,

 

By any chance are you also using the Weapon class, var = Weapon {};

 

If you are you might want to try this in your update function, I'm assuming you are using the Weapon class used in Coding Tips 7, to create different bullet types. Here is what you should try:

 

in update function try this line:

this.game.physics.arcade.overlap(this.weapons[this.currentWeapon], greenEnemies, this.hitEnemy, null, this);

 

then in your hitEnemy function:

 

hitEnemy: function(enemy, Bullet) {

    var explosion = explosions.getFirstExists(false);
    explosion.reset(Bullet.body.x + Bullet.body.halfWidth, Bullet.body.y + Bullet.body.halfHeight);
     explosion.body.velocity.y = enemy.body.velocity.y;
    explosion.alpha = 0.7;
    explosion.play('explosion', 30, false, true);
    enemy.kill();
 
   Bullet.kill();
 
     console.log(1);
},

 

Let me know if this helps or if i caused more confusion : )

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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