我嘗試了幾種方法將布爾值輸出實現爲if語句。不知怎的,我做錯了。我可以改變布爾值和console.log的值,所以這應該是正確的。然後我嘗試在一個if語句中使用它,但不知何故它被忽略,並沒有給出預期的結果。 這是我的代碼:將布爾值從true更改爲false並將其用於if()之後使用jquery
var heroVelX = game.currentHero.GetLinearVelocity().x;
var heroVelY = game.currentHero.GetLinearVelocity().y;
var speed = Math.sqrt(Math.pow(heroVelX, 2) + Math.pow(heroVelY, 2));
var moveOn = "";
function delay(){
moveOn = Boolean(speed<1.5);
console.log("Speed = " + speed + " " + moveOn);
};
setTimeout(delay, 3500);
// These are the conditions I have tried using. I can see in console.log that the value is changed. But somehow it is ignored? All the other conditions are accepted in the if-statement.
// moveOn ===!false
// moveOn == true
if(!game.currentHero.IsAwake() || moveOn === !false || heroX<0 || heroX >game.currentLevel.foregroundImage.width){
// then delete the old hero
box2d.world.DestroyBody(game.currentHero);
game.currentHero = undefined;
// and load next hero
game.mode = "load-next-hero";
}
誰能告訴我什麼,我做錯了什麼?
'的setTimeout(延遲,3500);'這條線將運行,那麼* *繼續到下一個(你的情況),其中'moveOn'仍然是' 「」 '因爲'delay()'還沒有執行。 – 2014-10-16 11:41:17
如何才能停止該程序,直到moveOn發生變化?還是有另一種解決方案? – 2014-10-16 13:03:32
'setTimeout'安排對'delay()'的調用,所以如果你想在*延遲之後運行*,把它放在'delay()' – 2014-10-16 13:09:49