【发布时间】: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.timer 在create 中调用。
总而言之,这是在 scoreCalculator 为 10 时添加一堵墙。效果很好,墙的动画效果很好,但碰撞检测根本不起作用。
我尝试将if 语句中的代码移动到我的create 函数中,它在碰撞检测中工作得很好(但其他东西坏了,所以我不能把它留在那里)。我究竟做错了什么?我怀疑由于我平均每秒调用一次this.wallSprite,它被添加为this.wallSprite 的新精灵覆盖,但我不完全确定我应该怎么做?
【问题讨论】:
标签: javascript phaser-framework