I'm making a capybara game and the enemy of the capybara is a crocodile. But how do I make the crocodile flip 180 degrees and move to the left side when it reaches the border?
getImage().mirrorHorizontally();
public class Crocodile extends Actor {
// ...
private boolean moveRight = true;
// ...
}
int speed = ...; if(moveRight) move(speed); else move(-speed);
moveRight = !moveRight;
// The variable int direction = 1; // 1 = right, -1 = left // moving int speed = ...; move(speed * direction); // flipping direction direction *= -1;
public class Crocodile extends Actor {
private boolean moveRight = true;
public void act() {
int speed = 1;
if(moveRight) move(speed);
else move(-speed);
// Flip direction when on the edge
if(isAtEdge())
moveRight = !moveRight;
}
}
import greenfoot.*;
public class Crocodile extends Actor
{
GreenfootImage[] images = new GreenfootImage[2];
int dir = 1; // -1 for left; 1 for right
int speed = 2;
public Crocodile()
{
images[0] = new GreenfootImage("crocodileright.png");
images[1] = new GreenfootImage(images[0]);
images[1].mirrorHorizontally();
}
public void act()
{
move(dir*speed);
if (isAtEdge())
{
dir = -dir;
setImage(images[(1-dir)/2];
}
if (isTouching(Capy.class)) Greenfoot.stop();
}
}