我正在尝试使用 two.js 编写一个简单的游戏。我有一个从按钮单击事件和游戏循环调用的函数:
function endGame(gameLoop){
showStartGame();
gameLoop.pause();
$("#gameSquare svg:first").remove();
$("#startGame").show();
}
该行$("#startGame").show()
仅在从事件调用时才能正确执行,其余的在两种情况下都可以正常工作。
单击事件处理程序:
$("#abandonGame").click(function(){
endGame(two);
gameLoopPaused = true;
gameStarted = false;
});
无法正常工作的调用(this.update() 在游戏循环中调用):
this.update = function(){
var computedVector = new Two.Vector(0,0);
screens.forEach(function(screen){
screen.update(speed);
screen.getRectangles().forEach(function(item){
computedVector.x = item.rect.translation.x;
computedVector.y = item.rect.translation.y + screen.getPosition().y;
if(computedVector.distanceTo(new Two.Vector(ship.getX(), ship.getY())) < latura + 20)
endGameEvent();
});