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?


1 | getImage().mirrorHorizontally(); |
1 2 3 4 5 | public class Crocodile extends Actor { // ... private boolean moveRight = true ; // ... } |
1 2 3 | int speed = ...; if (moveRight) move(speed); else move(-speed); |
1 | moveRight = !moveRight; |
1 2 3 4 5 6 7 8 9 | // The variable int direction = 1 ; // 1 = right, -1 = left // moving int speed = ...; move(speed * direction); // flipping direction direction *= - 1 ; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | 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(); } } |