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

2016/3/22

Enemy patrolling left to right

sidetracked sidetracked

2016/3/22

#
I just want the spider to move left for X seconds, then right for the same amount of time, on a loop. My code instantly stops the spider from moving
public class Spider extends Actor
{
    public int patrolDelay;
    public boolean goingLeft;

    public void act() 
    {
        patrolArea();
        patrolDelay--;
    }

    public void patrolArea()
    {
        if(getObjectsInRange(200, PlayerOne.class).isEmpty())  
        {

            if (goingLeft && patrolDelay < 1)
            {
                move(3);
                goingLeft = false;
                patrolDelay = 300;
            }

            if (!goingLeft && patrolDelay < 1)
            {
                move(-3);
                goingLeft = true;
                patrolDelay = 300;

            }
        }
    }
danpost danpost

2016/3/23

#
What your code does: For 299 act cycles, 'patrolDelay' will be greater than zero. On the one act cycle out of each 300 that the value of the field becomes zero, will it move; and then it will only move back and forth between two points that are three cells apart from each other. I will presume that your world cell size is one since you apparently what your patrolling to cover 900 cells horizontally (3 cells per move for 300 cycles before changing directions). With the current code, splitting up your dual conditions on lines 17 and 24 would be the correct course of action so that moving will not be on the condition that the value of 'patrolDelay' be zero (by splitting, I mean by moving one condition inside the block of the other): < Code Omitted > On second thought, you are dealing with two separate things here, controlling the movement and controlling the 'patrolDelay' field. Lines 17 through 30 can be simplified to this:
if (patrolDelay == 0)
{
    goingLeft = !goingLeft;
    patrolDelay = 300;
}

if (goingLeft)
{
    move(-3);
}
else
{
    move(3);
}
Actually, there is a much easier way to code the whole thing:
public class Spider extends Actor
{
    public int steps;
    public int speed = 3;
 
    public void act() 
    {
        patrolArea();
        steps = (steps+1)%300;
    }
 
    private void patrolArea()
    {
        if (getObjectsInRange(200, PlayerOne.class).isEmpty())  
        {
            if (steps == 0) speed = -speed;
            move(speed);
        }
    }
}
danpost danpost

2016/3/23

#
You will probably have more questions about the movement. With any of the above code, there is nothing to prevent the actor from being put in a holding pattern for a short time along either the left or right edges of the world. You could move the looping of the counter (line 9) in the outer 'if' block of the patrolArea method; but, if the actor moves at all while the PlayerOne object is within range, then you could still have temporary edge waiting. If the actor is to go from the left edge to the right and back, completely across the world horizontally, then you could just use the x-coordinate of its location to determine when to change directions:
public class Spider extends Actor
{
    public int speed = 3;
  
    public void act() 
    {
        patrolArea();
    }
  
    private void patrolArea()
    {
        if (getObjectsInRange(200, PlayerOne.class).isEmpty())  
        {
            if getX() == 0) speed = 3;
            if (getX() == getWorld().getWidth()-1) speed = -3;
            move(speed);
        }
    }
}
NOTE: this would require a bounded world (a world where actors cannot pass beyond the edges of the world).
sidetracked sidetracked

2016/3/23

#
thanks, I will do some testing when I get home from college, appreciate all the help!
You need to login to post a reply.