Hi and sorry for my bad english first.
I’m creating a multiplayer-mode for my little pong game at the moment, but when both players try to move their “character”, only one of them reacts.
my pretty simple code:
void draw ()
{
background(0);
strokeWeight(playerspeed);
stroke(255);
strokeCap(SQUARE);
line(width/2,3*playerspeed,width/2,height-3*playerspeed);
rect(xPosP1 , yPosP1 , playerWidth , playerHeight);
rect(xPosP2 , yPosP2 , playerWidth , playerHeight);
rect(xBall , yBall , ballWidth , ballHeight);
player1();
player2();
}
void player1 ()
{
if (keyPressed)
{
if (key == 'w' || key == 'W')
{
yPosP1 -= playerspeed;
} else if (key == 's' || key == 'S')
{
yPosP1 += playerspeed;
}
}
if (yPosP1 < playerHeight/2)
{
yPosP1 = playerHeight/2;
} else if (yPosP1 > height - (playerHeight/2))
{
yPosP1 = height - (playerHeight/2);
}
}
void player2 ()
{
if (keyPressed)
{
if (key == 'k' || key == 'K')
{
yPosP2 -= playerspeed;
} else if (key == 'm' || key == 'M')
{
yPosP2 += playerspeed;
}
}
if (yPosP2 < playerHeight/2)
{
yPosP2 = playerHeight/2;
} else if (yPosP2 > height - (playerHeight/2))
{
yPosP2 = height - (playerHeight/2);
}
}
How can I make Processing react to both inputs at the same time?
By the way, I’m an absolute beginner.
Can anyone help me?
Thanks in advance.