2016-12-22 15 views
0

Phaser.ioを使用して次のコードを作成しました。overlap()による衝突検出がphaser.ioで機能しない

私は敵のグループと武器のグループ(inbuilt game.add.weapon()を使用)を作成しました。

これらの2つのグループの重複をチェックしますが、なんらかの理由で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.enemiesとthis.weapon.bulletsとの衝突をチェックすることになっていた

関連する問題