So verschieben Sie ein Bild, um mit Pfeiltasten javascript

Ich habe vor kurzem begann mit der Entwicklung eines kleinen javascript-Spiel, nur zum Spaß. Die Idee war, dass Sie kontrollierte einen kleinen Punkt mit den Pfeiltasten oder awsd oder ich interessiere mich nicht, was) in einen Kasten auf dem Bildschirm. Kleine Rechtecke würden dann nach dem Zufallsprinzip laichen auf alle Kanten der box und der Fortschritt über Sie hinweg. Sie haben zu vermeiden, den Kontakt mit Ihnen. Das Projekt erwies sich schwieriger, als ich erwartet hatte, und ich konnte nicht die Bewegung richtig zu arbeiten. Wenn Sie mir helfen könnte, daß wäre Super. auch, fühlen Sie sich frei, um das Konzept und das wenige, was ich bisher getan habe und tun, was Sie will. Ich wäre daran interessiert zu sehen, Ihre Ergebnisse. Unten ist der code, den ich für die spawns ohne die Bewegung scripts. Ich war mit der Grundgedanke dieser code für die Bewegung:

var x = 5; //Starting Location - left
var y = 5; //Starting Location - top
var dest_x = 300;  //Ending Location - left
var dest_y = 300;  //Ending Location - top
var interval = 2; //Move 2px every initialization

function moveImage() {
    //Keep on moving the image till the target is achieved
    if(x<dest_x) x = x + interval; 
    if(y<dest_y) y = y + interval;

    //Move the image to the new location
    document.getElementById("ufo").style.top  = y+'px';
    document.getElementById("ufo").style.left = x+'px';

    if ((x+interval < dest_x) && (y+interval < dest_y)) {
        //Keep on calling this function every 100 microsecond 
        // till the target location is reached
        window.setTimeout('moveImage()',100);
    }
}

Hauptteil:

<html>
<head>
    <style type="text/css">
        html::-moz-selection{
            background-color:Transparent;
        }

        html::selection {
            background-color:Transparent;
        }
        img.n {position:absolute; top:0px; width:5px; height:10px;}
        img.e {position:absolute; right:0px; width:10px; height:5px;}
        img.s {position:absolute; bottom:0px; width:5px; height:10px;}
        img.w {position:absolute; left:0px; width:10px; height:5px;}
        #canvas {
            width:300px;
            height:300px;
            background-color:black;
            position:relative;
        }
    </style>
    <script type="text/javascript">
    nmecount=0
        function play(){
            spawn()
            var t=setTimeout("play()",1000);
        }
        function spawn(){
            var random=Math.floor(Math.random()*290)
            var side=Math.floor(Math.random()*5)
            var name=1
            var z=10000
            if (side=1)
            {
            var nme = document.createElement('img');
            nme.setAttribute('src', '1.png');
            nme.setAttribute('class', 'n');
            nme.setAttribute('id', name);
            nme.setAttribute('style', 'left:'+random+'px;');
            nme.onload = moveS;
            document.getElementById("canvas").appendChild(nme);
            }
            if (side=2)
            {
            var nme = document.createElement('img');
            nme.setAttribute('src', '1.png');
            nme.setAttribute('class', 'e');
            nme.setAttribute('id', name);
            nme.setAttribute('style', 'top:'+random+'px;');
            nme.onload = moveW;
            document.getElementById("canvas").appendChild(nme);
            }
            if (side=3)
            {
            var nme = document.createElement('img');
            nme.setAttribute('src', '1.png');
            nme.setAttribute('class', 's');
            nme.setAttribute('id', name);
            nme.setAttribute('style', 'left:'+random+'px;');
            nme.onload = moveN;
            document.getElementById("canvas").appendChild(nme);
            }
            if (side=4)
            {
            var nme = document.createElement('img');
            nme.setAttribute('src', '1.png');
            nme.setAttribute('class', 'w');
            nme.setAttribute('id', name);
            nme.setAttribute('style', 'top:'+random+'px;');
            nme.onload = moveE;
            document.getElementById("canvas").appendChild(nme);
            }
        name=name+1
        }
    </script>
</head>
<body onLoad="play()">
<div id="canvas">
<img id="a" src="1.png" style="position:absolute; z-index:5; left:150px; top:150px; height:10px; width=10px;" />
<button onclick="moveleft()"><</button>
</body>
</html>
Dies beantwortet nicht deine Frage aber, korrigieren Sie Ihren Stil am Ende in das img-tag mit der id 'ein'. Sie haben height:10px; width=10px; wo die Breite sollte width:10px;.

InformationsquelleAutor Quinn | 2011-08-27

Schreibe einen Kommentar