Jump to content

Require help with collision between weapon and enemy group


PixelProgrammer
 Share

Recommended Posts

Hi there! Newbie to phaser.

I seem to have some issues with my code. For some reason, the hitEnemy function never seems to run when my bullets (from weapon group) hit an enemy.

Here's the code. I've removed parts that don't matter. I'd love if you could find out what happened and why.. and also how to fix it.

 

Quote

var playState = {
    preload: function () {
        game.load.image('player', 'assets/player.png');
        game.load.image('enemy', 'assets/enemy.png');
        game.load.image('floor', 'assets/floor.png');
        game.load.image('bullet', 'assets/bullet.png');
    },

    create: function () {
        game.stage.backgroundColor = '#000000';
        game.physics.startSystem(Phaser.Physics.ARCADE);
        game.renderer.renderSession.roundPixels = true;

        this.cursors = game.input.keyboard.createCursorKeys();
        this.fireButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);

        this.createBullets();

        this.createEnemies(game.width/2, 120, 'enemy');




    },
     update: function () {
    
        game.physics.arcade.collide(this.player, this.floor);
        game.physics.arcade.overlap(this.weapon, this.enemies, this.hitEnemy, null, this);
  },
     createBullets: function () {    
        this.weapon = game.add.weapon(30, 'bullet');
        this.weapon.bulletKillType = Phaser.Weapon.KILL_WORLD_BOUNDS;
        this.weapon.bulletSpeed = 850;
        this.weapon.fireRate = 100;
        this.weapon.trackSprite(this.player);
    },

    createEnemies: function (x,y ,size) {
        this.enemies = game.add.group();
        this.enemies.enableBody = true;
        this.enemies.physicsBodyType = Phaser.Physics.ARCADE;

        var asteroid = this.enemies.create(x, y, size);
        asteroid.anchor.setTo(0.5,0.5);
        asteroid.name = "enemy1";
        asteroid.body.immovable;
    },
    hitEnemy: function (player, enemies) {
        this.enemies.kill();
        console.log("Hit");
    },
};

 

 

 

Link to comment
Share on other sites

38 minutes ago, FinalFantasyVII said:

Hi!

Try this:


 update: function () {
    
        game.physics.arcade.collide(this.player, this.floor);
        game.physics.arcade.overlap(this.weapon.bullets, this.enemies, this.hitEnemy, null, this);
  }

@FinalFantasyVII Just figured that out.. thanks for the reply!

However, now although the collision works the hitEnemy function throws an error. It says that hit.enemies.kill is not a function

hitEnemy: function (player, enemies) {
        this.enemies.kill();
        console.log("Hit");
    },

Whoops.. nevermind. Just figured out the error

hitEnemy: function (player, enemies) {
        enemies.kill();
        console.log("Hit");
    }
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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