0

最初の「移動コマンド」でのみ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();
}
4

1 に答える 1