Javascript 为什么不是';我的碰撞检测不能在相位器中工作吗?

Javascript 为什么不是';我的碰撞检测不能在相位器中工作吗?,javascript,phaser-framework,Javascript,Phaser Framework,为了简单起见,我有一个游戏,我想让我的车撞到墙上。我试过搜索,也试过一些方法,你会在下面看到,我不知道为什么我不能让它工作 以下是我的create功能中的代码: create: function () { game.physics.startSystem(Phaser.Physics.ARCADE); this.carSprite = game.add.sprite(game.world.centerX, game.world.centerY, 'car

为了简单起见,我有一个游戏,我想让我的车撞到墙上。我试过搜索,也试过一些方法,你会在下面看到,我不知道为什么我不能让它工作

以下是我的
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: function () {
        game.physics.arcade.overlap(this.carSprite, this.wallSprite, this.scoring, null, this);
}
然后在我的
update
功能中:

    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: function () {
        game.physics.arcade.overlap(this.carSprite, this.wallSprite, this.scoring, null, this);
}
我正在创建
墙精灵
,如下所示:

 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;
        }
}
    addRoad: function () {
        this.addOneRoadSection(this.xValue, 0);
}
   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;
}
我是这样呼叫的
addOneRoadSection

 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;
        }
}
    addRoad: function () {
        this.addOneRoadSection(this.xValue, 0);
}
   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;
}
正在使用
this.timer
create
中调用
addRoad

总之,当
scoreCalculator
为10时,它就是一堵墙。这很好,墙的动画效果很好,但碰撞检测根本不起作用


我尝试将
if
语句中的代码移动到我的
create
函数中,它可以很好地进行碰撞检测(但其他东西坏了,所以我无法将它保留在那里)。我做错了什么?我有一种怀疑,因为我平均每秒调用一次
this.wallSprite
,它被添加为
this.wallSprite
的新精灵写得太多了,但我不完全确定我还应该怎么做?

所以我找到了答案。发生的事情是,我最初倾向于创造一个新的精灵,而过度书写另一个是正确的

下面是我如何解决这个问题的:

创建中

create: function(){ 
 //Create a wallSprite array
 var wallSprite = [];
}
现在,当我在
addOneLoadSection
中添加一个新的
wallSprite
时,我有了一个数组,我可以将物理添加到每个sprite,如下所示:

 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;
        }
}
    addRoad: function () {
        this.addOneRoadSection(this.xValue, 0);
}
   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);
    }
for(var i=0;i
瞧,碰撞检测现在适用于每一个精灵