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).
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{ } } }
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); } } }