原生js實現游戲人物上下左右跑步效果



75 299 100



特效描述:原生js實現 游戲人物 上下左右 跑步效果,原生js實現游戲人物上下左右跑步效果

代碼結構

1. HTML代碼

<img id="im" src="images/down-0.png" />
<table>
  <tr>
    <td><input id="leftUp" type="button" value="左上" /></td>
    <td><input id="goUp" type="button" value="向上" /></td>
    <td><input id="rightUp" type="button" value="右上" /></td>
  </tr>
  <tr>
    <td><input id="goLeft" type="button" value="左" /></td>
    <td><input id="stop" type="button" value="停止" /></td>
    <td><input id="goRight" type="button" value="右" /></td>
  </tr>
  <tr>
    <td><input id="leftDown" type="button" value="左下" /></td>
    <td><input id="goDown" type="button" value="向下" /></td>
    <td><input id="rightDown" type="button" value="右下" /></td>
  </tr>
</table>
<script type="text/javascript">
		var i = 0, clc = null, flage;
		var images = document.getElementById('im');
		var oGoUp = document.getElementById('goUp');
		var oGoDown = document.getElementById('goDown');
		var oGoLeft = document.getElementById('goLeft');
		var oGoRight = document.getElementById('goRight');
		var oLeftUp = document.getElementById('leftUp');
		var oLeftDown = document.getElementById('leftDown');
		var oRightUp = document.getElementById('rightUp');
		var oRightDown = document.getElementById('rightDown');
		var oStop = document.getElementById('stop');
		images.style.top = '300px';
		images.style.left = '500px';
		//停止
		oStop.onclick = function(){
			switch(flage){
				case 1: images.src = 'images/up-0.png';break;
				case 2: images.src = 'images/down-0.png';break;
				case 3: images.src = 'images/left-0.png';break;
				case 4: images.src = 'images/right-0.png';break;
				case 5: images.src = 'images/rightUp-0.png';break;
				case 6: images.src = 'images/rd-0.png';break;
				case 7: images.src = 'images/ld-0.png';break;
				case 8: images.src = 'images/lu-0.png';break;
			}
			clearInterval(clc);
		}
		//向上
		oGoUp.onclick = function(){
			i = 0;
			clearInterval(clc);
			clc = setInterval("goUp(i++);", 100);
		}
		function goUp(){
			i = i % 4;
			var name ="images/up-" + i + "." + "png";
			images.src = name;
			images.style.top = parseInt(images.style.top) - 10 + 'px';
			flage = 1;
		}
		//向下
		oGoDown.onclick = function(){
			i = 0;
			clearInterval(clc);
			clc = setInterval("goDown(i++);", 100);
		}
		function goDown(){
			i = i % 4;
			var name ="images/down-" + i + "." + "png";
			images.src = name;
			images.style.top = parseInt(images.style.top) + 10 + 'px';
			flage = 2;
		}
		//向左
		oGoLeft.onclick = function(){
			i = 0;
			clearInterval(clc);
			clc = setInterval("goLeft(i++);", 100);
		}
		function goLeft(){
			i = i % 4;
			var name ="images/left-" + i + "." + "png";
			images.src = name;
			images.style.left = parseInt(images.style.left) - 10 + 'px';
			flage = 3;
		}
		//向右
		oGoRight.onclick = function(){
			i = 0;
			clearInterval(clc);
			clc = setInterval("goRight(i++);", 100);
		}
		function goRight(){
			i = i % 4;
			var name ="images/right-" + i + "." + "png";
			images.src = name;
			images.style.left = parseInt(images.style.left) + 10 + 'px';
			flage = 4;
		}
		//向右上
		oRightUp.onclick = function(){
			i = 0;
			clearInterval(clc);
			clc = setInterval("goRightUp(i++);", 100);
		}
		function goRightUp(){
			i = i % 4;
			var name ="images/rightUp-" + i + "." + "png";
			images.src = name;
			images.style.left = parseInt(images.style.left) + 10 + 'px';
			images.style.top = parseInt(images.style.top) - 10 + 'px';
			flage = 5;
		}
		//向右下
		oRightDown.onclick = function(){
			i = 0;
			clearInterval(clc);
			clc = setInterval("goRightDown(i++);", 100);
		}
		function goRightDown(){
			i = i % 4;
			var name ="images/rd-" + i + "." + "png";
			images.src = name;
			images.style.left = parseInt(images.style.left) + 10 + 'px';
			images.style.top = parseInt(images.style.top) + 10 + 'px';
			flage = 6;
		}
		//向左下
		oLeftDown.onclick = function(){
			i = 0;
			clearInterval(clc);
			clc = setInterval("goLeftDown(i++);", 100);
		}
		function goLeftDown(){
			i = i % 4;
			var name ="images/ld-" + i + "." + "png";
			images.src = name;
			images.style.left = parseInt(images.style.left) - 10 + 'px';
			images.style.top = parseInt(images.style.top) + 10 + 'px';
			flage = 7;
		}
		//向左上
		oLeftUp.onclick = function(){
			i = 0;
			clearInterval(clc);
			clc = setInterval("goLeftUp(i++);", 100);
		}
		function goLeftUp(){
			i = i % 4;
			var name ="images/lu-" + i + "." + "png";
			images.src = name;
			images.style.left = parseInt(images.style.left) - 10 + 'px';
			images.style.top = parseInt(images.style.top) - 10 + 'px';
			flage = 8;
		}
	</script>
<div style="text-align:center;clear:both">
</div>



用戶評論
大牛,別默默的看了,快登錄幫我點評一下吧!:)      登錄 | 注冊


熱門標簽: 小游戲 H5游戲 游戲源碼 手機游戲 微信游戲 網頁游戲 貪吃蛇 貪吃蛇游戲 H5貪吃蛇游戲 貪吃蛇游戲源碼
×
×

注冊

官方QQ群

掃描上面二維碼加微信群

官方QQ群

jQuery/js討論群
群號:642649996
Css3+Html5討論群
群號:322131262

加群請備注:從官網了解到

老夫子电子