0
當我嘗試僅在第一個「移動命令」中將div(坦克)移到右側時,並且僅在該方向上時,我遇到了問題,因此我的div射出向右幾千像素,遠離屏幕區域。希望有人會幫助我明白爲什麼會這樣。立即向右移動div遠離屏幕區域
function animate() {
var tank = document.getElementById("tank");
tank.style.marginLeft="360px";
tank.style.marginTop="440px";
window.xpos = tank.style.marginLeft;
window.ypos = tank.style.marginTop;
window.x = xpos.replace("px","");
window.y = ypos.replace("px","");
document.onkeydown = checkKey;
function checkKey(e) {
e = e || window.event;
if (e.keyCode == '37') {
if (x > 0) {
x = x - 20;
tank.style.marginLeft = x + "px";
}
} else if (e.keyCode == '39') {
if (x < 70) {
x = x + 20;
tank.style.marginLeft = x + "px";
}
} else if (e.keyCode == '38') {
if (y > 0) {
y = y - 20;
tank.style.marginTop = y + "px";
}
} else if (e.keyCode == '40') {
if (y < 440) {
y = y + 20;
tank.style.marginTop = y + "px";
}
}
}
checkKey(e);
}
window.lives = 3;
function destroy() {
if (lives != 0) {
alert("Life Lost!");
lives--;
window.collision == false;
animate();
} else {
alert("Try Again!");
}
}
window.collision = true;
function state() {
if (collision == false) {
window.state = 1;
} else if (collision == true) {
window.state = 0;
}
return state;
}
state();
if (state == 1) {
animate();
} else {
destroy();
}
不知道,但你可能要考慮用'開關(N)代替那麼複雜,如果/ elseif的語句{...}'功能。 – Lopsided
還沒有真正使用開關盒的功能,但一旦我找出問題就會發揮它,謝謝! – user2882574
這真是太棒了。今年早些時候我開始使用它,但它尚未引起任何問題。看看這裏:http://www.w3schools.com/js/js_switch.asp – Lopsided