I am making a simple "Escape the jail" game for my Java class. I want my Guard Actor to move back and forth until the player wins. Any Ideas? Heres my code... (In the Character class, I felt as if a boolean for if the guard is moving would come into play, as the main problem I get is the guard spazzes left and right (since the isAtWorldEdge(), once true, would make it turn back), but, I am unsure how I would get the return on if the guard is moving or not).
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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | import greenfoot.*; public class Character extends Actor{ public void act(){ } public boolean isAtWorldEdge(){ boolean a; int xPos = getX(); int yPos = getY(); if (((xPos >= 0 ) && (xPos <= 10 )) || ((xPos >= 890 ) && (xPos <= 901 )) || ((yPos >= 0 ) && (yPos <= 10 )) || ((yPos >= 690 ) && (yPos <= 701 ))){ a = true ; } else { a = false ; } return a; } public void moveGuardLeft( int speed){ if ( this .getX() < 900 ){ move(-speed); } else { moveGuardRight(speed); } } public void moveGuardRight( int speed){ if ( this .getX() > 0 ){ move(speed); } else { moveGuardLeft(-speed); } } public boolean isMoving(){ boolean moving; if (){ } else { } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | import greenfoot.*; public class Guard1 extends Character{ private int speed = 5 ; private int counter = 60 ; public void act(){ catchPrisoner(); moveGuardLeft(speed); moveGuardRight(speed); } public void catchPrisoner(){ if (isTouching(Prisoner. class )){ Actor prisoner = getOneObjectAtOffset( 0 , 0 , Prisoner. class ); getWorld().removeObject(prisoner); } } } |