-4

我正在尝试使用 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();
});
4

2 回答 2

0

尝试将其包装在 document.ready

$(document).ready(function() {
    $("#startGame").show();
});

您不能在对象存在之前调用 show()

于 2017-01-25T16:19:43.880 回答
0

问题出在 $("#startGame") 的父级上,它也被隐藏了。它适用于事件,因为当我点击 $("#abandonGame") 按钮时,它已显示在屏幕上。

于 2017-01-25T16:42:08.123 回答