簡體   English   中英

Phaser.js 為什么重疊不起作用?

[英]Phaser.js why overlap doesn't work?

我在 phaser.js 上創建了一個小場景

當敵人向我開槍時,我必須死,但它不起作用

而且,當我射擊敵人時,他們應該死。 我在代碼中寫了一個重疊函數,但它不起作用。

這是情況的屏幕截圖:

游戲場景

為什么會這樣,你能幫幫我嗎?

var playState = {

create: function()
{
    this.enemiesArray = [];

    game.physics.startSystem(Phaser.Physics.ARCADE);

    this.spacebar;
    this.spacebar = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
    this.escapeKey = game.input.keyboard.addKey(Phaser.Keyboard.ESC);

    this.ship = game.add.sprite(game.width / 2, game.height / 2 + 300, 'ship');
    this.ship.anchor.setTo(0.5, 0.5);
    this.ship.scale.setTo(0.4, 0.4);
    this.cursor = game.input.keyboard.createCursorKeys();

    this.enemy = game.add.sprite(game.width / 2, game.height / 2 - 300, 'alien');
    this.enemy.anchor.setTo(.5,.5);
    this.enemy.scale.setTo(0.4,0.4);
    game.time.events.loop(1000, this.spawnEnemy, this);
    game.time.events.loop(750, this.spawnEnemyBullet, this);

    game.physics.arcade.enable(this.ship, this.enemy, this.bullet, this.enemyBullet);

},

update: function()
{

    if(this.cursor.left.isDown)
    {
        this.ship.x -= 7;
    }

    for(var a = 0; a < this.enemiesArray.length; a++)
    {
        this.enemiesArray[a].x -= 2;
    }

    if(this.escapeKey.isDown)
    {
        game.state.start('menu');
    }

    if(this.cursor.right.isDown)
    {
        this.ship.x += 7;
    }

    if(this.spacebar.isDown)
    {
        this.bullet = game.add.sprite(this.ship.x, this.ship.y, 'bullet');
        this.bullet.anchor.setTo(0.5,0.5);
        this.bullet.scale.setTo(0.2,0.2);
        game.physics.arcade.enable(this.bullet);
        this.bullet.body.velocity.y = -600;

        if(!this.bullet.inWorld)
        {
            this.bullet.kill();
        }
    }
    game.physics.arcade.overlap(this.enemyBullet,this.ship,this.gameOverOpenMenuScreen,null,this);
    game.physics.arcade.overlap(this.bullet,this.enemy,this.killenemy,null,this);
},

killenemy: function()
{
    this.enemy.kill();
},

gameOverOpenMenuScreen: function()
{
    game.state.start('menu');
},

spawnEnemy: function()
{
    this.enemy = game.add.sprite(Math.random()*game.width, game.height / 2 - 300, 'alien');
    this.enemy.anchor.setTo(.5,.5);
    this.enemy.scale.setTo(0.4,0.4);

    this.enemiesArray.push(this.enemy);
},

spawnEnemyBullet: function()
{
    for(var i = 0; i < this.enemiesArray.length; i++)
    {
        this.enemyBullet = game.add.sprite(this.enemiesArray[i].x, this.enemiesArray[i].y, 'bullet');
        this.enemyBullet.anchor.setTo(0.5,0.5);
        this.enemyBullet.scale.setTo(0.2,0.2);
        this.enemyBullet.angle = 180;
        game.physics.arcade.enable(this.enemyBullet);
        this.enemyBullet.body.velocity.y = 600;
    }
}

}

您應該首先將所有項目符號分組為一組:

const bulletsGroup = this.add.group();

(注意本次現場裁判)

然后將每個項目符號添加到組中:

bulletsGroup.add(<your game object>);

在這兩個步驟之后,您可以設置重疊功能。 該函數獲取前 2 個參數的組/對象,以及在發生重疊時調用的回調:

game.physics.arcade.overlap(this.ship, bulletGroup, () => console.log('boom!'));

您可以使用組來創建項目符號,但我認為您應該先糾正這一點:

game.physics.arcade.enable([this.ship, this.enemy]);

將此添加到您的代碼中:

create : function() {
   ...
   //After creating the sprites
   this.bulletPlayerTime = 0;

   this.bulletsPlayer = game.add.group();
   this.bulletsPlayer.enableBody = true;
   this.bulletsPlayer.physicsBodyType = Phaser.Physics.ARCADE;

   for (var i = 0; i < 20; i++)
   {
       var b = this.bulletsPlayer.create(0, 0, 'bullet');
       b.name = 'bullet' + i;
       b.exists = false;
       b.visible = false;
       b.checkWorldBounds = true;
       b.events.onOutOfBounds.add(function(bullet) { bullet.kill(); }, this);
   }
   ...
}

update : function() {

  ...
  if (this.spacebar.isDown) {
     this.firePlayer();
  }
  ...
}

firePlayer : function() {

   if (game.time.now > this.bulletPlayerTime) {
      bullet = bulletsPlayer.getFirstExists(false);

      if (bullet) {
        bullet.reset(this.ship.x + 6, this.ship.y - 8);
        bullet.body.velocity.y = -300;
        this.bulletPlayerTime = game.time.now + 300;
      }
   }

}

現在您必須執行相同的功能,但這次是與敵人...

你可以在這里看到更多

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM