简体   繁体   English

需要帮助在Canvas上移动两个对象

[英]Need help moving two objects on Canvas

To start off, I'm learning JavaScript and trying to create an old Atari Pong game. 首先,我正在学习JavaScript并尝试创建一个旧的Atari Pong游戏。 I'm stuck at this point where I can get one of the paddles moving, but not the second. 我停留在这一点,我可以让其中一个桨移动,但不是第二个。 The game is still functional, but one paddle won't move. 游戏仍然有效,但一个桨不会移动。 Here is my code for the paddle that won't move: 这是我不会移动的桨的代码:

var p2X = canvas.width/2 + 550;
var p2Y = canvas.height/2;
var p2Radius = 35;
var p2Height = 100;

and: 和:

var p2UpPressed = false;
var p2DownPressed = false;
document.addEventListener("keydown", keyDownHandler, false);
document.addEventListener("keyup", keyUpHandler, false);

I already used EventListener and keyUp/downHandler for the first paddle, and I'm wondering if this is why the second won't move? 我已经使用EventListener和keyUp / downHandler作为第一个paddle,我想知道这是否是第二个不会移动的原因?

function keyDownHandler(e){
if(e.keyCode == 87){
p2UpPressed = true;
}
else if(e.keyCode == 83){
p2DownPressed = true;
}
}
function keyUpHandler(e){
if(e.keyCode == 87){
p2UpPressed = false;
}
else if(e.keyCode == 83){
p2DownPressed = false;
}
}

Here is the actual moving bit: 这是实际的移动位:

if(p2UpPressed && p2Y <canvas.height-p2Radius){
p2Y += 7;
}
else if(p2DownPressed && p2Y > 0){
p2Y-=7;
}

Any help would be much appreciated. 任何帮助将非常感激。 Thanks! 谢谢!

First, a tip on debugging: Start by seeing if the handlers for player 2 are being called! 首先,提示调试:首先查看是否正在调用播放器2的处理程序! Easy to do, just put console.log("hello fren"); 容易做,只需把console.log("hello fren"); in the handler, then press the key and watch the console. 在处理程序中,然后按键并观察控制台。 Alternatively you can use breakpoints or "watch" the p2UpPressed variable in the console, but those techniques are a bit more advanced and not really necessary in this situation. 或者,您可以在控制台中使用断点或“监视” p2UpPressed变量,但这些技术有点高级,在这种情况下并不是必需的。

So based on a guess and then your confirmation in the comments, you've got your handler functions named the same for both players. 因此,基于猜测,然后在评论中进行确认,您的处理程序功能对于两个玩家都是相同的。 That won't work because it will simply use whichever function is defined first in the code. 这将无法工作,因为它将简单地使用代码中首先定义的任何函数。 Don't forget you can name functions whatever you want, so you could name them p1KeyDownHandler and p2KeyDownHandler , or yoyoboi and misterdude if you felt so inclined (although the convention is that you name them something sensible :P). 不要忘记你可以随心所欲地命名函数,所以你可以将它们命名为p1KeyDownHandlerp2KeyDownHandler ,或yoyoboimisterdude如果你觉得如此倾向(虽然惯例是你给它们命名为明智的东西:P)。

I'm pretty sure you can add multiple handlers to the same event, but this is probably the better way to do this: 我很确定你可以为同一个事件添加多个处理程序,但这可能是更好的方法:

function keyDownHandler(e) {
    if (e.keyCode == xx) { //replace xx with whatever keycode you want for p1
        p1UpPressed = true;
    } else if (e.keyCode == xx) { //same as above
        p1DownPressed = true;
    } else if (e.keyCode == 87) {
        p2UpPressed = true;
    } else if (e.keyCode == 83) {
        p2DownPressed = true;
    }
}

function keyUpHandler(e) {
    if (e.keyCode == xx) { //same as above
        p1UpPressed = false;
    } else if (e.keyCode == xx) { //same as above
        p1DownPressed = false;
    } else if (e.keyCode == 87) {
        p2UpPressed = false;
    } else if (e.keyCode == 83) {
        p2DownPressed = false;
    }
}

And then you only do this once: 然后你只做一次:

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

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

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