Hi, i have an image that moves left and right when the a and d buttons are pressed. How do i get the picture to point the direction it is going? P.s im new at greenfoot so explain it simply! Thanks :)
if (Greenfoot.isKeyDown("right"))
{
if (lastDir=="left")
{getImage().mirrorHorizontally();} //turns around the player if you're trying to go
//right after going left
lastDir="right";
//your moving code here
}if (Greenfoot.isKeyDown("right")) && !faceRight || Greenfoot.isKeyDown("left")) && faceRight)
{
getImage().mirrorHorizontally();
faceRight = !faceRight;
}private GreenfootImage imageR, imageL;
imageR = getImage(); imageL = new GreenfootImage(imageR); imageL.mirrorHorizontally();
// get key input for direction
int dir = 0;
if (Greenfoot.isKeyDown("left")) dir--;
if (Greenfoot.isKeyDown("right")) dir++;
// use direction for moving and facing if moving at all
if (dir != 0)
{
// moving
setLocation(getX()+dir*speed, getY());
// facing
if (dir < 0 && getImage() == imageR) setImage(imageL);
if (dir > 0 && getImage() == imageL) setImage(imageR);
}