kou*_*lya 1 javascript html5-canvas
如何使用html5和javascript使用键盘键移动对象.在这里我尝试使用下面的代码,但它不移动可以任何人帮助使用键盘箭头键移动对象?
<!DOCTYPE html>  
<html>  
<head>
</head>
<body onLoad="gameLoop()" onkeydown="keyDown(event)" onkeyup="keyUp(event)" bgcolor='green'>  
<canvas id="mycanvas"></canvas>    
<script>  
var canvas = document.getElementById('mycanvas');  
var ctx = canvas.getContext('2d');  
ctx.beginPath();  
ctx.arc(100, 100, 50, 0.25 * Math.PI, 1.25 * Math.PI, false);  
ctx.fillStyle = "rgb(255, 255, 0)";  
ctx.fill();  
ctx.beginPath();  
ctx.arc(100, 100, 50, 0.75 * Math.PI, 1.75 * Math.PI, false);  
ctx.fill();  
ctx.beginPath();  
ctx.arc(100, 75, 10, 0, 2 * Math.PI, false);
  ctx.fillStyle = "rgb(0, 0, 0)";  
ctx.fill();  
//moves//  
var xpos=500;  
var ypos=550;  
var xspeed=1;  
var yspeed=0;  
var maxspeed=5;  
//boundaries//  
var minx=500;  
var miny=200;  
var maxx=990;  
var maxy=400;  
//controller   
var upPressed=0;   
var downPressed=1;  
var leftPressed=0;  
var rightPressed=0;   
function slowDownX()  
{  
 if(xspeed > 0) xspeed=xspeed-1;  if(xspeed<0)  xspeed=xspeed+1;
  }  
function slowDownY()  
 {  
if(yspeed > 0)
  yspeed = yspeed-1;  
if(yspeed < 0)  
yspeed = yspeed+1;
  }  
function gameLoop()  
{  
xpos=Math.min(Math.max(xpos+xspeed,minx),maxx);   ypos=Math.min(Math.max(ypos+yspeed,miny),maxy);  
xpos = document.getElementById('mycanvas').style.left;  
ypos = document.getElementById('mycanvas').style.top;  
if (upPressed == 1)  
yspeed = Math.max(yspeed - 3,-3*maxSpeed);  
if (downPressed == 1)  
yspeed = Math.min(yspeed + 3,3*maxSpeed)  
if (rightPressed == 1)  
xspeed = Math.min(xspeed + 1,1*maxSpeed);  
if (leftPressed == 1)  
xspeed = Math.max(xspeed - 1,-1*maxSpeed);  
if (upPressed == 0 && downPressed == 0)  
slowDownY();  
if (leftPressed == 0 && rightPressed == 0)  
slowDownX();  
return setTimeout("gameLoop()",10);  
}  
function keyDown(e)  
{  
var code = e.keyCode ? e.keyCode : e.which;  
if (code == 38)  
upPressed = 1;  
if (code == 40)  
downPressed = 1;  
if (code == 37)  
leftPressed = 1;  
if (code == 39)  
rightPressed = 1;  
}  
function keyUp(e)  
{  
var code = e.keyCode ? e.keyCode : e.which;  
if (code == 38)  
upPressed = 0;  
if (code == 40)  
downPressed = 0;  
if (code == 37)  
leftPressed = 0;  
if (code == 39)  
rightPressed = 0;  
}  
</script>  
</body>  
</html>
Run Code Online (Sandbox Code Playgroud)
    以下是在html画布上绘制形状并使用箭头键移动它的基础知识
免责声明:此代码不是最好的游戏技术,它意味着明确的指令.;)
首先创建一些定义球的变量:
    // the ball will be at coordinates 70,75
    var ballX=70;
    var ballY=75;
    // the ball will have a radius of 15;
    var ballRadius=15;
Run Code Online (Sandbox Code Playgroud)
接下来创建一个将在指定坐标处绘制球的函数
    function draw(){
        // clear the canvas for the next frame
        ctx.clearRect(0,0,canvas.width,canvas.height);
        // draw a ball that the use can move with left/right arrow keys
        // the ball's center will be at BallX / BallY
        // the ball will have a radius of BallRadius
        ctx.beginPath();
        ctx.arc(ballX,ballY,ballRadius,0,Math.PI*2,false);
        ctx.closePath();
        ctx.fill();
    }
Run Code Online (Sandbox Code Playgroud)
现在听取用户的击键.
    // Listen for when the user presses a key down
    window.addEventListener("keydown", keyDownHandler, true);
Run Code Online (Sandbox Code Playgroud)
当用户按下键时:
该代码处理密钥关闭时(由addEventListener调用):
    // Here we just handle command keys
    function keyDownHandler(event){
        // get which key the user pressed
        var key=event.which;
        // Let keypress handle displayable characters
        if(key>46){ return; }
        switch(key){
            case 37:  // left key
              // move the ball 1 left by subtracting 1 from ballX
              ballX -= 1;
              break;
            case 39:  // right key
              // move the ball 1 right by adding 1 to ballX
              ballX += 1;
              break;
            default:
              break;
        }
        // redraw the ball in its new position
        draw();
    }
Run Code Online (Sandbox Code Playgroud)
这是代码和小提琴:http: //jsfiddle.net/m1erickson/TsXmx/
<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" media="all" href="css/reset.css" /> <!-- reset css -->
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<style>
    body{ background-color: ivory; padding:20px;}
    #canvas{border:1px solid red;}
</style>
<script>
$(function(){
    var canvas=document.getElementById("canvas");
    var ctx=canvas.getContext("2d");
    ctx.strokeStyle="blue";
    ctx.fillStyle="red";
    var ballX=70;
    var ballY=75;
    var ballRadius=15;
    var leftWall=30;
    var rightWall=120;
    draw();
    function draw(){
        // clear the canvas for the next frame
        ctx.clearRect(0,0,canvas.width,canvas.height);
        // tell canvas to start a new path
        // draw walls on left and right
        ctx.beginPath();
        ctx.moveTo(leftWall,0);
        ctx.lineTo(leftWall,canvas.height);
        ctx.moveTo(rightWall,0);
        ctx.lineTo(rightWall,canvas.height);
        ctx.lineWidth=2;
        ctx.stroke();
        // draw a ball that the use can move with left/right arrow keys
        ctx.beginPath();
        ctx.arc(ballX,ballY,ballRadius,0,Math.PI*2,false);
        ctx.closePath();
        ctx.fill();
    }
    // Here we just handle command keys
    function keyDownHandler(event){
        // get which key the user pressed
        var key=event.which;
        // Let keypress handle displayable characters
        if(key>46){ return; }
        switch(key){
            case 37:  // left key
              // move the ball 1 left by subtracting 1 from ballX
              ballX -= 1;
              // calc the ball's left edge
              var ballLeft=ballX-ballRadius;
              // Keep the ball from moving through the left wall
              if(ballLeft<=leftWall){ ballX=leftWall+ballRadius; }
              break;
            case 39:  // right key
              // move the ball 1 right by adding 1 to ballX
              ballX += 1;
              // calc the ball's right edge
              var ballRight=ballX+ballRadius;
              // Keep the ball from moving through the right wall
              if(ballRight>=rightWall){ ballX=rightWall-ballRadius; }
              break;
            default:
              break;
        }
        // redraw everything
        draw();
    }
    // Listen for when the user presses a key down
    window.addEventListener("keydown", keyDownHandler, true);
}); // end $(function(){});
</script>
</head>
<body>
    <p>Click in the red box to get keyboard focus</p>
    <p>Then Move ball with left and right arrow keys</p>
    <canvas id="canvas" width=200 height=150></canvas>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)