Author Topic: Any Javascript gurus in here?  (Read 488 times)

Any idea why this doesn't work?

Code: [Select]
function timeTankMove() {
var t=setTimeout("moveTank()",1000);
}
var direction = "right";

var amount = 0;
function moveTank() {

if (direction === "left" && amount<5) {
$(".tank").animate({"left": "-=70px"}, "slow");
amount += 1;
} else if (direction === "right" && amount<5) {
$(".tank").animate({"left": "+=70px"}, "slow");
amount += 1;
} else {
if (direction === "left") {
direction = right;
} else {
direction = left;
}
amount = 0;
}

$("p.debug").html(direction + " " + amount);
timeTankMove();

}
The tank moves to the right 5 times then stops, when it should be changing the direction to the opposite direction then reseting the count. Any idea why it's not working?
If you need me to, I can upload a version online or something.
Please excuse me if I made any ridiculous mistakes there. I'm not used to Javascript at all.

While I don't work with JavaScript, perhaps your else check is wrong?

Code: [Select]
direction = right;to
Code: [Select]
direction = "right";
Same for left.

Oh god damnit. I can't believe I missed that.
Thanks Orthone.