Hello, I am trying to make my actor change direction when I press a certain key, for example, when I press the right key I want it to face right, and when I press the left key I want it to face left. This is what have got so far:
import greenfoot.*;
/**
* Write a description of class snake here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class snake extends Actor
{
/**
* Act - do whatever the snake wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
if (Greenfoot.isKeyDown("right"))
move(5);
if (Greenfoot.isKeyDown("left"))
move(-5);
if (Greenfoot.isKeyDown("space"))
turn(17);
setRotation(90);
if (Greenfoot.isKeyDown("up"))
{ move(-5); }
if (Greenfoot.isKeyDown("down"))
{ move(5); }
setRotation(0);
if (isAtEdge() )
{
turn(17);
}
Actor snowman = getOneIntersectingObject(snowman.class);
if(snowman != null)
{
World myWorld = getWorld();
myWorld.removeObject(snowman);
}
Actor tux = getOneIntersectingObject(tux.class);
if(tux != null)
{
World myWorld = getWorld();
myWorld.removeObject(tux);
}
Actor robot = getOneIntersectingObject(robot.class);
if(robot !=null)
{
World myWorld = getWorld();
myWorld.removeObject(robot);
}
}
}
