[英]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.