Keyboard movement
in
Programming Questions
•
2 years ago
Working on a fairly simple top view game and have come to writing in the players character movement, which I thougt would be simple. I was looking for a simple w, s, a, d movement system, but it appears to be running into issues. Is there a way of putting in the void keypressed function into a class? If that makes sence.
I've been able to write a simple prog. that moves an ellipse around the screen but in my larger program would like to include it in a class, so later I can add a case statement to use 1 or 2 players using the w, s, a, d and arrow keys as movement
(Here's what I did for the test)
- float playerPosX, playerPosY;
void setup()
{
size(600, 600);
playerPosX = 25;
playerPosY = 25;
} - void draw()
{
background (180);
fill(60, 60, 180);
ellipse (playerPosX, playerPosY, 15, 15);
} - void keyPressed()
{
if (key == 'd')
{
playerPosX = playerPosX + 7;
}
if (key == 'a')
{
playerPosX = playerPosX - 7;
}
if (key == 'w')
{
playerPosY = playerPosY -7;
}
if (key == 's')
{
playerPosY = playerPosY + 7;
}
}
1