繁体   English   中英

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") 按钮时它已显示在屏幕上。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM