2016-12-22 86 views
0

請注意,我完全使用Phaser.io創建了以下代碼。碰撞檢測通過重疊()在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

找到了解決方案。

而不是檢查與this.enemies和this.weapon重疊。我本來應該檢查與這個敵人和this.weapon.bullets碰撞