简体   繁体   English

在基于JavaScript的画布游戏上上下移动

[英]Moving up and down on javascript based canvas game

I am making game based on this tutorial: https://developer.mozilla.org/en-US/docs/Games/Workflows/2D_Breakout_game_pure_JavaScript 我正在根据本教程制作游戏: https : //developer.mozilla.org/en-US/docs/Games/Workflows/2D_Breakout_game_pure_JavaScript

I think i understand how the paddle moves right and left. 我想我了解球拍如何左右移动。 But when i try to make it move up and down, its not working at all. 但是,当我尝试使其上下移动时,它根本无法工作。 I have literally tried everyting but still its not working. 我已经尝试了一切,但仍然无法正常工作。 So the question is how i can make it move up and down? 所以问题是我如何使它上下移动?

Here is the whole code: 这是完整的代码:

    <script>
var canvas = document.getElementById("tausta");
var ctx = canvas.getContext("2d");
var ballRadius = 10;
var x = canvas.width/2;
var y = canvas.height-30;
var dx = 2;
var dy = -2;
var paddleHeight = 10;
var paddleWidth = 75;
var paddleX = (canvas.width-paddleWidth)/2;
var paddleY = canvas.height-paddleHeight;
var rightPressed = false;
var leftPressed = false;
var upPressed = false;
var downPressed = false;

document.addEventListener("keydown", keyDownHandler, false);
document.addEventListener("keyup", keyUpHandler, false);

function keyDownHandler(e) {
    if(e.keyCode == 39) {
        rightPressed = true;
    }
    if(e.keyCode == 37) {
        leftPressed = true;
    }
    if(e.keycode == 38){
        upPressed = true;
    }
    if(e.keycode == 40){
        downPressed = true; 
    }
}
function keyUpHandler(e) {
    if(e.keyCode == 39) {
        rightPressed = false;
    }
    if(e.keyCode == 37) {
        leftPressed = false;
    }
    if(e.keycode == 38){
        upPressed = false;
    }
    if(e.keycode == 40){
        downPressed = false; 
    }
}

function drawBall() {
    ctx.beginPath();
    ctx.arc(x, y, ballRadius, 0, Math.PI*2);
    ctx.fillStyle = "#0095DD";
    ctx.fill();
    ctx.closePath();
}
function drawPaddle() {
    ctx.beginPath();
    ctx.rect(paddleX, paddleY, paddleWidth, paddleHeight);
    ctx.fillStyle = "#0095DD";
    ctx.fill();
    ctx.closePath();
}

function draw() {
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    drawBall();
    drawPaddle();

    if(x + dx > canvas.width-ballRadius || x + dx < ballRadius) {
        dx = -dx;
    }
    if(y + dy > canvas.height-ballRadius || y + dy < ballRadius) {
        dy = -dy;
    }

    if(rightPressed && paddleX < canvas.width-paddleWidth) {
        paddleX += 5;
    }
    if(leftPressed && paddleX > 0) {
        paddleX -= 5;
    }
    if(upPressed && paddleY < canvas.height-paddleHeight) {
        paddleY += 5;
    }
    if(downPressed && paddleY > 0) {
        paddleY -= 5;
    }

    x += dx;
    y += dy;
}

setInterval(draw, 10);

</script>

Turns out it was a simple mistake. 原来这是一个简单的错误。 You typed 您输入了

if(e.keycode == 38){
if(e.keycode == 40){

But it should be 但这应该是

if(e.keyCode== 38){
if(e.keyCode== 40){

capital C 大写C

http://jsfiddle.net/cagwyd6m/4/ http://jsfiddle.net/cagwyd6m/4/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM