【发布时间】:2015-12-26 14:39:30
【问题描述】:
这是我的游戏plnkr。
(编辑:另一个plnkr 有一个静态怪物而不是多个动态怪物)
Enter 否则按钮将重新开始游戏。
谁能说出为什么取自here 的碰撞检测算法不起作用?它似乎不能准确地检测到命中(太广泛)。他们网站上的演示效果很好,但我不确定我做错了什么。
最相关的一段代码(内部更新函数):
// Are they touching?
if (heroImage.width) {
var heroImageData = ctx.getImageData(heroImage.x, heroImage.y, heroImage.width, heroImage.height);
var monsterImageData;
for (var i = 0; i < monsters.length; i++) {
var monster = monsters[i];
monster.x += monster.directionVector.x;
monster.y += monster.directionVector.y;
monsterImageData = ctx.getImageData(monster.monsterImage.x, monster.monsterImage.y, monster.monsterImage.width, monster.monsterImage.height);
if (isPixelCollision(heroImageData, hero.x, hero.y, monsterImageData, monster.x, monster.y)) {
stop();
}
}
}
【问题讨论】:
标签: javascript html canvas collision-detection game-physics