【发布时间】:2014-12-11 18:21:24
【问题描述】:
我尝试了几种方法来实现从布尔到 if 语句的输出。不知何故我做错了。我能够更改布尔值和 console.log 它的值,所以应该是正确的。然后我尝试在 if 语句中使用它,但不知何故它被忽略并且没有给出预期的结果。 这是我的代码:
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(delay, 3500);这行将运行,然后 继续 到下一个(您的条件)moveOn仍然是"",因为delay()尚未执行。 -
我怎样才能停止程序直到 moveOn 发生变化?还是有其他解决方案?
-
setTimeout安排对delay()的调用,因此如果您希望在延迟 之后运行,请将其放入delay() -
我不能设置
moveOn = false;吗?
标签: javascript jquery if-statement boolean var