Reputation: 190
I have tried several ways to implement the output from a boolean to an if-statement. Somehow I do it wrong. I'm able to change the value of the boolean and console.log it, so that should be right. I then try to use it in an if-statement, but somehow it is ignored and doesn't give the expected outcome. This is my code:
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";
}
Can someone tell me what I'm doing wrong?
Upvotes: 1
Views: 2177
Reputation: 983
You are doing several things wrong...
First of all, it's horrible to define a var as a String, and after that change it to boolean.
Secondly, put your boolean into a different log, and use dir instead.
The if statement its too confusing.
And there is no need to cast to Boolean.
Take a look at this:
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 = false;
function delay(){
moveOn = (speed<1.5) ? true : false;
console.log("Speed = " + speed);
console.dir(moveOn);
};
setTimeout(delay, 3500);
if(!game.currentHero.IsAwake() || moveOn || 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";
}
Upvotes: 1