JQuery .show() 仅适用于点击事件

JQuery .show() works only from click event

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

尝试将其包装在 document.ready

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

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

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