javascript - Phaser.js 为什么重叠不起作用?

标签 javascript phaser-framework

我在phaser.js上创建了一个小场景

当敌人向我开枪时,我必须死,但这不起作用

而且,当我射击敌人时,他们应该死。我在代码中写了一个重叠函数,但它不起作用。

以下是情况截图:

GameScene

为什么会这样,你能帮帮我吗?

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();

(请注意,this 裁判了场景)

然后将你们每个项目符号添加到组中:

bulletsGroup.add(<your game object>);

完成这 2 个步骤后,您可以设置重叠功能。该函数获取其前 2 个参数的组/对象,以及发生重叠时调用的回调:

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

关于javascript - Phaser.js 为什么重叠不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41920020/

相关文章:

javascript - 如何将类组件重写为使用钩子(Hook)的组件

javascript - 如何检测网页是否正在 WebBrowser Control 中呈现?

reactjs - 有没有办法使 Phaser 游戏对象成为 react 控制组件?

javascript - Sprite vs Group Collider 在启用Body 设置为 true 的移相器中不起作用

javascript - PHP Symfony API 和 jQuery Ajax 请求

javascript - 我不希望弹出窗口在用户点击离开时最小化

javascript - 允许在滚动动画期间改变元素高度

typescript - 使用 Typescript 设置 Phaser 游戏

javascript - 无法读取未定义的 Phaser.js 的属性 add()

phaser-framework - 移相器 : Gravity originating from a body