This site requires JavaScript, please enable it in your browser!
Greenfoot back
Camyh15
Camyh15 wrote ...

2015/7/31

Actor changing directions

Camyh15 Camyh15

2015/7/31

#
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);
        }
  }
}
danpost danpost

2015/7/31

#
Basically, you just need to set the appropriate image when the left or right key is down.
if (Greenfoot.isKeyDown("right"))
{
    move(5);
    setImage(/** right-facing image */);
}
if (Greenfoot.isKeyDown("left"))
{
    move(-5);
    setImage(/** left-facing image */);
}
However, there is a conflict between the turning (of 17 degrees) and the movement (of intervals of 90 degrees) which should be resolved. Well, maybe it is the fact that you are using 'setRotation(90)' and 'setRotation(0)' instead of 'turn(90)' and 'turn(-90)'. If you do make this change, you would, instead of the change above, insert something like the following at line 33 in your code above:
if (getRotation() >= 90 && getRotation() < 270)
    setImage(/** left-facing image */);
else
    setImage(/** right-facing image */);
Since you would then be setting the image every act cycle, in would be wise to have the images stored in fields within the class so that they are not created every act cycle, but only set.
Camyh15 Camyh15

2015/8/1

#
This is not working because it says that there is "no suitable method found for setImage (no arguments)."
danpost danpost

2015/8/1

#
Camyh15 wrote...
This is not working because it says that there is "no suitable method found for setImage (no arguments)."
You need to put references to the images you are using for the parameter of the method. For example:
setImage(rightFaceImage);
// where 'rightFaceImage' is previously set with something like
GreenfootImage rightFaceImage = new GreenfootImage("snake-right.png");
You need to login to post a reply.