Facet wrote...
This means you're removing the player before thing like the movement code have stopped being executed, try moving the remove player line down the class
private SimpleTimer timer = new SimpleTimer(); private Actor pursued; private int bulletTimer;
public void act()
{
if (!getObjectsInRange(650, Player_Test1.class).isEmpty())
{
if (pursued == null)
{
pursued = getObjectsInRange(650, Player_Test1.class).get(0);
}
if (!los.obstructedView(this, pursued, Walls.class))
{
pursue();
}
else
{
randomMovement();
}
}
else
{
pursued = null;
randomMovement();
}
}public class Enemy_Test3 extends Animal
{
//instance fields
/**
* Assigns SimpleTimer method to the variable of timer to this class.
*/
private SimpleTimer timer = new SimpleTimer();
/**
* turnPhase is assigned to hold a boolean value (true or false).
*/
private boolean turnPhase;
/**
* phasteTimer is assigned to hold a numerical value of -1
*/
private int phaseTimer = -1;
/**
* Actor is assigned to be the variable pursued
*/
private Actor pursued;
/**
* obstacleTimer is assigned to hold a numerical value
*/
private int obstacleTimer;
/**
* bulletTimer is assigned to hold a numerical value
*/
private int bulletTimer;
/**
* Method summary: This act method detects whether the Player_Test1 class is within 800 pixel range and whether it has any Wall classes obstructing its line of sight (meaning it will check whether there is a Wall class between the Player and this class by drawing up an invisible line that links between the two which checks for any Wall classes intersecting that line).
*
* If Player_Test1 class is outside 650 pixel range or has a Wall class intersecting the line of sight to this class, Enemy_Test3 class will execute method randomMovement.
* If Player_Test1 class is within 650 pixel range and has no Wall classes intersecting the line of sight to this class Enemy_Test3 class will execute method pursue.
*/
public void act()
{
if (pursued != null && pursued.getWorld() == null)
{
pursued = null;
}
if (pursued == null)
{
if (getObjectsInRange(650, Player_Test1.class).isEmpty())
{
randomMovement();
return;
}
else
{
pursued = (Actor)getObjectsInRange(650, Player_Test1.class).get(0);
}
}
if (pursued != null)
{
if (obstacleTimer == 0)
{
if (los.obstructedView(this, pursued))
{
obstacleTimer = 100;
}
else
{
pursue();
return;
}
}
}
if (obstacleTimer > 0)
{
if (--obstacleTimer == 0)
{
if (los.obstructedView(this, pursued))
{
pursued = null;
}
}
else
{
if (los.obstructedView(this, pursued, Walls.class))
{
randomMovement();
return;
}
else
{
obstacleTimer= 0;
pursue();
return;
}
}
}
else
{
pursue();
}
}
/**
* Method summary: This method makes this class move forward at a randomly selected distance then to stop for a randomly selected amount of time only then to instantly turn a random amount of degrees to then stop for a randomly selected amount of time to only move foward again a randomly selected distance (loop). This method also able to detect when this class is in contact with a wall, if this class is in contact with a wall the class will turn away from the wall.
*/
public void randomMovement()
{
// control code (when out of range of or path obstructed toward player)
int sign = (int)Math.signum(phaseTimer); // '1' means moving or turning and '-1' means stopped after moving or turning
phaseTimer -= sign; // step toward zero
if (phaseTimer == 0)
{
if (sign == -1)
{
if (turnPhase) phaseTimer = 50+Greenfoot.getRandomNumber(400);
else phaseTimer = 1;
turnPhase = !turnPhase;
}
else
{
if (turnPhase) phaseTimer = -(50+Greenfoot.getRandomNumber(400));
else phaseTimer = -(50+Greenfoot.getRandomNumber(30));
}
}
if (sign > 0)
{
if (turnPhase) turn(Greenfoot.getRandomNumber(360)); else move(1);
}
if (atWorldEdge() || isTouching(Walls.class) || isTouching(BulletPassableWalls.class))
{
turn(160+Greenfoot.getRandomNumber(180)-90);
}
}
/**
* Method summary: This method will get the coordinates of the pursued (Player_Test1.class) and will continuously turn towards those coordinates and move foward at a move speed of 2 and will execute the shoot method. This method also able to detect when this class is in contact with a wall, if this class is in contact with a wall the class will turn away from the wall.
*/
public void pursue()
{
int x = getX(), y = getY();
turnTowards(pursued.getX(), pursued.getY());
move(1);
shoot();
if (atWorldEdge() || isTouching(Walls.class))
{
turn(160+Greenfoot.getRandomNumber(180)-90);
}
if (isTouching(Walls.class) || isTouching(BulletPassableWalls.class))
{
setLocation(x, y);
}
}System.out.println(getIntersectingObjects(null).size());