這是我在這裏的第一篇文章......嗯,我需要的是使用鍵盤上的4個按鍵在屏幕上移動圖像並更改每個按鍵的圖像,例如:
--W--
ASD
當我按下「d」鍵,我的對象應該移動到右邊,當我按下「A」鍵,我的對象應我的形象應該是「facingright」
移動到左側,我的圖像應該是「facingleft」,所以它走...
如何使用JavaScript更改和移動圖像
****編輯:這是我的項目後,你的幫助!謝謝!!!:http://xonny.esy.es/Xonnygame/index.html
我有這個代碼在這裏,但它似乎是不可能做這樣的事情我想要的,所以我需要修改它!
<html>
<head>
<title>Moving an Image</title>
<script language="JavaScript">
function moveObj(name, Xpix, Ypix)
{
obj = document.getElementById(name);
px = parseInt(obj.style.left) + Xpix;
py = parseInt(obj.style.top) + Ypix;
obj.style.left = px;
obj.style.top = py;
}
function ProcessKeypress(e)
{
var myObj = "mario";
var moveBy = 10;
if (e.keyCode) keycode=e.keyCode;
else keycode=e.which;
ch=String.fromCharCode(keycode);
if(ch=='a' || ch=='A') moveObj(myObj, -moveBy, 0);
else if(ch=='d' || ch=='D') moveObj(myObj, moveBy, 0);
else if(ch=='w' || ch=='W') moveObj(myObj, 0, -moveBy);
else if(ch=='s' || ch=='S') moveObj(myObj, 0, moveBy);
}
</script>
<body onKeyPress="ProcessKeypress(event);">
<p><img id="mario" style="z-index: 0; left: 300px; position: absolute; top: 250px"
height=72 width=100 align=baseline border=0 hspace=0 src="mario right.jpg"></p>
</body>
</html>
我建議在突出顯示整個代碼塊後單擊代碼按鈕;反引號僅用於在線代碼。像這樣:'一些代碼在這裏,等等。 – Daedalus 2014-09-30 00:52:13
那麼它在做什麼或者不在做什麼?你有錯誤嗎?請提供有關您遇到的問題的更多信息。 – 2014-09-30 01:31:40