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

2013/7/13

Get Actor to move infinitely back and forth until the player wins

brandondamante brandondamante

2013/7/13

#
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);
        }
    }
}
danpost danpost

2013/7/13

#
Instead of moving with 'speed' and '-speed', you should go ahead and change the value of 'speed' between '5' and '-5' and just add speed each act. Then you can use location and direction to determine when to change its value.
if ( (speed > 0 && getX() >= 900) ||
     (speed < 0 && getX() <=0) )
{
    speed = -speed;
}
move(speed);
brandondamante brandondamante

2013/7/13

#
danpost wrote...
Instead of moving with 'speed' and '-speed', you should go ahead and change the value of 'speed' between '5' and '-5' and just add speed each act. Then you can use location and direction to determine when to change its value.
if ( (speed > 0 && getX() >= 900) ||
     (speed < 0 && getX() <=0) )
{
    speed = -speed;
}
move(speed);
I'm sorry if I didn't ask my question well enough, but I want to make the Guard move back and forth repetitively. My problem is getting it to move from end to end. What usually happens is it goes to the wall and wont move back the other way, due to the isAtWorldEdge() being false after it leaves the wall, forcing it back to the wall immediately. This cycle is infinite.
danpost danpost

2013/7/14

#
Adding the boolean field 'a' and method 'isMoving' was not required, or even needed. In fact, as far as I can tell, you are not even calling the 'isAtWorldEdge' method; so, it is not a factor. Since you do not need to use any of the methods you supplied in the Character class, I have the Guard1 class extending the Actor class below; however, you could still put the act and move methods below in a Character or Guard class so more than one class can have the behavior of moving back and forth and have those classes extend it. This is the basic code for an actor to move back and forth from world edge to world edge horizontally:
import greenfoot.*;

public class Guard1 extends Actor
{
    private int speed = 5;

    public void act()
    {
        move();
        catchPrisoner();
    }
    
    private void move()
    {
        if ((speed > 0 && getX() == getWorld().getWidth()-1) ||
            (speed < 0 && getX() == 0)) speed = -speed;
        move(speed);
    }

    private void catchPrisoner()
    {
        Actor prisoner = getOneObjectAtOffset(0, 0, Prisoner.class);
        if (prisoner != null) getWorld().removeObject(prisoner);
    }
}
You need to login to post a reply.