Home >Web Front-end >JS Tutorial >Example of using keyboard to control character movement implemented in JavaScript_javascript skills

Example of using keyboard to control character movement implemented in JavaScript_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:38:352501browse

In fact, this example uses two core times of js, the keyboard event onkeydown and the periodic execution event setInterval.

Achieve results

When a certain key on the keyboard is pressed, the characters on the webpage will perform corresponding actions, achieving the effect of using the keyboard to control movement

Implementation steps

1. Function of booking key:

w: up
s: downward
a:Left
d: Right
Space: Stop

2. After booking the key value, you need to be able to capture the key event and determine which key the user pressed?

To capture keyboard events you can use onkeydown
To obtain the key-value code, you can use event.keyCode

3. Use setInterval periodic execution event to replace the picture

The replacement of pictures is to achieve the task walking effect
But be careful to use clearInterval to clear the cycle execution, otherwise it will go faster and faster

Sample code:

<html>
	<head>
		<meta charset="utf-8" />
		<title>人物走动</title>
	</head>
	<body onkeydown="show()">
		<ul style="posttion:absolute;border:1px solid #999;list-style:none;width:150px;padding:20px;background:#ffffef;">
			<li>w:向上</li>
			<li>s:向下</li>
			<li>a:向左</li>
			<li>d:向右</li>
			<li>空格:停止</li>
			<li style="margin-top:20px;"><u><a href="http://www.jb51.net">脚本之家</a></u></li>
		</ul>
		<div style="position:absolute;top:0;left:0" id='di'><img src="http://files.jb51.net/file_images/article/201408/201482791327688.gif&#63;201472791345" id="img"></div>
		<script>
			var img1='http://files.jb51.net/file_images/article/201408/201482791656841.gif&#63;201472791722';
			var img2='http://files.jb51.net/file_images/article/201408/201482791327688.gif&#63;201472791345';
			var x=0;
			var y=0;
			var xs=0;
			var ys=0;
			var flag=true;
			var zq=null;
			function ks(){
				zq=setInterval(function(){	
					var s=document.getElementById('img');	
					var str=s.src;
					var area=document.getElementById('di');
					var xpos=parseInt(area.style.left);
					var ypos=parseInt(area.style.top);
					x=x+xs;
					y=y+ys;
					area.style.left=x;
					area.style.top=y;
					var pos=str.lastIndexOf('/')+1;
					var hz=str.substr(pos);
					if(hz==img1){
						s.src= img2;
					}else{
						s.src= img1;
					}				
				},50);
			}
			ks();

			function show(){
				var sz=window.event.keyCode;
				switch(sz){
					case 87:
						img1='http://files.jb51.net/file_images/article/201408/ren_h_1.gif';
						img2='http://files.jb51.net/file_images/article/201408/ren_h_2.gif';
						ys=-5;
						xs=0;
						break;
					case 65:
						img1='http://files.jb51.net/file_images/article/201408/ren_l_1.gif';
						img2='http://files.jb51.net/file_images/article/201408/ren_l_2.gif';
						xs=-5;
						ys=0;
						break;
					case 68:
						img1='http://files.jb51.net/file_images/article/201408/ren_r_1.gif';
						img2='http://files.jb51.net/file_images/article/201408/ren_r_2.gif';
						xs=5;
						ys=0;
						break;
					case 83:
						img1='http://files.jb51.net/file_images/article/201408/ren_q_1.gif';
						img2='http://files.jb51.net/file_images/article/201408/ren_q_2.gif';
						ys=5;
						xs=0;
						break;
					case 32:
					  if(flag){
						  clearInterval(zq);
						  flag=false;
						  break;
					   }
					case 13:
					 if(!flag){
						 ks();
						 flag=true;
						break;
					  }
				}
			}
		</script>
	</body>
</html>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn