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

2014/2/11

Need help with turning at edges

Marky3100 Marky3100

2014/2/11

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Snake here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Snake extends Animal
{
    /**
     * 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() 
    {
        moveAround();
    }    

    private void moveAround()  
    {  
           
        if (getX() <= 550 || getX() >= getWorld() . getWidth() -5)  
        {  
            
            setImage("snakeLeft.png");
            setLocation(getX() + 2, getY());
        }
        if (getX() >= 5 || getX() <= getWorld() . getWidth() -5)
        {  
           
            setImage("snake2.png");
            setLocation(getX() + 2, getY());

        }}}
I need my snake actor to turn 180degrees when it reaches the edge of the world. It has to move without keypresses. My problem is: The if i use setRotation/Turn the image will be upside down :c Snake2 = wachting to the right snakeLeft = watchting to the left I hope someone can help me!
Gevater_Tod4711 Gevater_Tod4711

2014/2/11

#
You just need to turn the actor and afterwards set the actor's image:
private void moveAround() {    
     if (getX() <= getWorld().getWidth() -5) {
         setLocation(getX() - 2, getY());
         turn(180);
         setImage("snakeLeft.png");  
     }  
     else if (getX() >= 5)  {
         setLocation(getX() + 2, getY());  
         turn(180);
         setImage("snake2.png");
      }
}  
danpost danpost

2014/2/11

#
From what I can tell, this is what your 'moveAround' method will do: The first if will, for any x except between 550 and 5 from the right edge of the world (in a world 600 wide, that would be excepting the range 551 to 554), have the snake face right and move 2 to the right. The second if will, for ALL x, have the snake face left and move 2 to the right. In total, this means the snake will for all x, the snake will face left and move 2 to the right; within the small range between 551 and 554, it will move an additional 2 to the right. What you need is an instance field for the direction of the snake (call it 'direction') whose value will be either '2' or '-2' and negate its value when the snakes exceeds its position for the direction it is going. Then use its value to adjust the position of the snake in the 'setLocation' statement (instead of the hard-coded '+2').
danpost danpost

2014/2/11

#
@Gevater_Tod4711, I do not believe that your solution will work. When at 6, the first if is executed moving it to 4. The second if will never be executed. The actor will end up at x = 0 and basically stop there.
danpost danpost

2014/2/11

#
@Marky3100, at line 11, insert the following field declaration:
private int direction = 2;
Then replace your 'moveAround' method with this:
private void moveAround()
{
    if ((direction == -2 && getX() <= 5) || (direction == 2 && getX() >= getWorld().getWidth()-5))
    {
        direction = -direction;
        if (direction == 2) setImage("snake2.png"); else setImage("snakeLeft.png");
    }
    setLocation(getX()+direction, getY());
}
Marky3100 Marky3100

2014/2/13

#
Thanks a lot! It works great!
You need to login to post a reply.