I want to make my character move using 'x' and 'y', so pressing the left key changes 'x' by -5 for example. I think my problem is the fact that it doesn't recognise 'x' and 'y' as coordinates, so changing them does nothing. (I also want him to switch images as he walks, but I think that part's okay).
My script so far goes as follows:
import greenfoot.*;
public class Character extends Actor
{
private GreenfootImage Image1;
private GreenfootImage Image2;
private GreenfootImage Image3;
private GreenfootImage Image4;
private int y;
private int x;
public Character()
{
Image1 = new GreenfootImage("Right Hand Backwards.png");
Image2 = new GreenfootImage("Right Hand Forward.png");
Image3 = new GreenfootImage("Left Hand Forward.png");
Image4 = new GreenfootImage("Left Hand Backwards.png");
setImage(Image1);
}
/**
* Act - do whatever the Character wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
switchImage();
checkKeyPress();
checkKeyPress2();
}
public void switchImage() //Alternates the images, making the Character more realistic.
{
}
public void checkKeyPress()
{
if (Greenfoot.isKeyDown("up"))
{
y=y+5;
}
if (Greenfoot.isKeyDown("right"))
{
setImage (Image2);
x=x+5;
}
else
{
setImage(Image3);
}
}
public void checkKeyPress2()
{
if (Greenfoot.isKeyDown("left"))
{
setImage (Image1);
x=x-5;
}
else
{
setImage(Image4);
}
if (Greenfoot.isKeyDown("down"))
{
y=y-5;
}
}
}
Thanks.