通过重叠()的碰撞检测在phaser.io中不起作用

时间:2016-12-22 14:46:25

标签: javascript game-physics phaser-framework

请注意,我已完全使用Phaser.io创建了以下代码。

所以我创建了一个敌人组和一个武器组(使用内置的game.add.weapon())。

我检查这两个组之间的重叠但由于某种原因,hitEnemy()函数永远不会被触发。

我认为这与使用内置武器组有关,但我无法弄清楚究竟是什么。

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, enemy) {
        this.enemy.kill();
        console.log("Hit");
    },
};

1 个答案:

答案 0 :(得分:1)

找到解决方案。

而不是检查与this.enemies和this.weapon的重叠。我应该检查与this.enemies和this.weapon.bullets

的碰撞