Hey there!
I'm new on Greenfoot and Java in general, trying to make a small puzzle game for a school project.
I want to make a class that can move around a grid with 1 step each time a certain arrow key is pressed. I got the class to move around when I press the arrow keys, but it's on full speed and for as long as I keep the the button pressed.
This is what I've got on the class' movement:
public class Smarty extends Actor
{
/**
* Allows smarty to act.
*/
public void act()
{
{
processKeys();
}
}
/**
* Handle keyboard input.
*/
public void processKeys()
{
if(Greenfoot.isKeyDown ("left")) {
setLocation(getX() - 1, getY());
}
else {
setLocation(getX(),getY());
}
if(Greenfoot.isKeyDown ("down")) {
setLocation(getX(),getY() + 1);
}
else {
setLocation(getX(),getY());
}
if(Greenfoot.isKeyDown ("right")) {
setLocation(getX() + 1,getY());
}
else {
setLocation(getX(),getY());
}
if(Greenfoot.isKeyDown ("up")) {
setLocation(getX(),getY() - 1);
}
else {
setLocation(getX(),getY());
}
}
}
Is there anything I should change or add to make this happen?
Thank a lot in advance!
hesh--