【问题标题】:Why isn't my collision detection working in Phaser?为什么我的碰撞检测在 Phaser 中不起作用?
【发布时间】:2016-03-30 02:00:57
【问题描述】:

为简单起见,我有一个游戏,我试图让我的车撞到墙上。我已经尝试过搜索并尝试了几件事,如下所示,我不确定为什么我无法让它工作。

这是我的create 函数中的代码:

    create: function () {
        game.physics.startSystem(Phaser.Physics.ARCADE);

        this.carSprite = game.add.sprite(game.world.centerX, game.world.centerY, 'car');
        game.physics.arcade.enable(this.carSprite);
    this.timer = game.time.events.loop(300,
        this.addRoad, this);
    }

然后在我的update 函数中:

    update: function () {
        game.physics.arcade.overlap(this.carSprite, this.wallSprite, this.scoring, null, this);
}

我正在像这样创建wallSprite

 addOneRoadSection: function (x, y) {
this.scoreCalculator += 1;
        if (this.scoreCalculator == 10) {
            this.scoreCalculator = 0;

            this.wallSprite = game.add.sprite(x + 100, y, 'wall');
            game.physics.arcade.enable(this.wallSprite);
            this.wallAnimation = this.wallSprite .animations.add('wallAnimation');
            this.wallSprite.animations.play('wallAnimation', 30, true);
            this.wallSprite.body.velocity.y = 100;
            this.wallSprite.checkWorldBounds = true;
            this.wallSprite.outOfBoundsKill = true;
        }
}

我这样打电话给addOneRoadSection

    addRoad: function () {
        this.addOneRoadSection(this.xValue, 0);
}

addRoad 正在使用this.timercreate 中调用。

总而言之,这是在 scoreCalculator 为 10 时添加一堵墙。效果很好,墙的动画效果很好,但碰撞检测根本不起作用。

我尝试将if 语句中的代码移动到我的create 函数中,它在碰撞检测中工作得很好(但其他东西坏了,所以我不能把它留在那里)。我究竟做错了什么?我怀疑由于我平均每秒调用一次this.wallSprite,它被添加为this.wallSprite 的新精灵覆盖,但我不完全确定我应该怎么做?

【问题讨论】:

    标签: javascript phaser-framework


    【解决方案1】:

    所以我想通了。发生的事情是,我最初的倾向是创建一个新的精灵,而重写另一个精灵是正确的。

    这是我解决问题的方法:

    create:

    create: function(){ 
     //Create a wallSprite array
     var wallSprite = [];
    }
    

    现在我有了一个数组,当我在addOneRoadSection 中添加一个新的wallSprite 时,我可以像这样为每个精灵添加物理:

       addOneRoadSection: function (x, y) {
    
                this.wallSprite.push(game.add.sprite(x + 100, y, 'wall'));
                game.physics.arcade.enable(this.wallSprite[this.wallSprite.length -1]);
                this.wallAnimation = this.wallSprite[this.wallSprite.length - 1].animations.add('wallAnimation');
                this.wallSprite[this.wallSprite.length - 1].animations.play('wallAnimation', 30, true);
                this.wallSprite[this.wallSprite.length -1].body.velocity.y = 100;
                this.wallSprite[this.wallSprite.length - 1].checkWorldBounds = true;
                this.wallSprite[this.wallSprite.length -1].outOfBoundsKill = true;
    }
    

    最后在update我只需要这样做:

        for (var i = 0; i < this.wallSprite.length; i++) {
            game.physics.arcade.overlap(this.car, this.wallSprite, this.scoring, null, this);
        }
    

    瞧——碰撞检测现在适用于每个精灵。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-07
      • 1970-01-01
      • 2020-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多