I am having some trouble with the code below in the dealEnemyDamage() method in the MrBoom class and the aquireTarget() method in the EnemyDamageField class... When a MrBoom actor makes contact with the EnemyDamageField actor, it is supposed to subtract 1 from the health bar (hb in the code) and then remove the EnemyDamageField actor from the world. Currently, all I am getting is this error java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. or null pointer exceptions... if more code is needed I am happy to upload my project for a short time.
import greenfoot.*;
import java.util.List;
/**
* Write a description of class MrBoom here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class MrBoom extends Ally
{
private GreenfootImage [] imgs;
int i=1;
boolean isAttacked = false;
Round round;
HealthBar hb;
public MrBoom(Round rnd)
{
round = rnd;//round instance field
imgs = new GreenfootImage[16];//array of images
for(int i=1;i<16;i++)
imgs[i]= new GreenfootImage("MrBoom_"+i+".png");//sets each picture to a new position in the array
hb = new HealthBar("","hp",100,100);
hb.setBarWidth(25);
hb.setBarHeight(5);
}
/**
* Act - do whatever the Ally wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
health();
despawn();
dealEnemyDamage();
if(getWorld()!=null)
isAttacking();
if(isAttacking())
{
i++;//counter used for determining which image is being used
setImage(imgs[i%16]);
if(i==imgs.length-1)
i=0;
damage();
}
}
public int getDamage()
{
EnemyDamageField enemy = (EnemyDamageField)(getOneObjectAtOffset(0, 0, EnemyDamageField.class));
int damage = enemy.power();
return damage;
}
public void dealEnemyDamage()
{
isAttacked=true;
if(getOneObjectAtOffset(100, 100, EnemyDamageField.class)!=null)
{
hb.subtract(1);
}
if(canSee(EnemyDamageField.class))
{
eat(EnemyDamageField.class);
}
}
public void despawn()
{
if(hb.getValue()==0)
{
getWorld().removeObject(hb);
getWorld().removeObject(this);
}
}
public void health()
{
getWorld().addObject(hb, getX(), getY()-30);
hb.setLocation(getX(), getY()-50);
}
/**
* Checks if Ally has made contact with the enemy
* if true- stop moving and fight
* if false- move until true
*/
public boolean isAttacking()
{
if(getWorld()!=null)
{
List players = getObjectsAtOffset(0, 0, Enemy.class);
List players2 = getObjectsAtOffset(0,0, EnemyCastle.class);
Actor enemyCastle = (Actor) (getWorld().getObjects(EnemyCastle.class).get(0));
if (! players.isEmpty()&& getOneObjectAtOffset(50, 50, Enemy.class)!=null||isTouching(Enemy.class)&&!players.isEmpty()) {
Actor player = (Actor) players.get(0);
if (player.getX()-50>getX())
move(1);
else
move(0);
dealEnemyDamage();
return true;
}
else if(getX()>=enemyCastle.getX()-175)
{
move(0);
return true;
}
else {
move(1);
setImage(imgs[1]);
return false;
}
}
return false;
}
/**
* creates a new DamageField at certain parts of the image
*/
public void damage()
{ if(i>8)//checks if image is i or greater
{
for(int w=getImage().getWidth()/3;w<getImage().getWidth()-10;w+=120)//width
{
for(int h=0; h<getImage().getHeight()/3;h+=+150)//height
{
if(getImage().getColorAt(w, h)!=null)//checks the pixels (getColor not needed)
{
getWorld().addObject(new DamageField(round,MrBoom.class), getX()+w, getY()+h);//add new DamageField
}
}
}
}
}
}
import greenfoot.*;
/**
* Write a description of class EnemyDamageField here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class EnemyDamageField extends Animal
{
Class damageType;
Round round;
int damagePower=2;
int life= Greenfoot.getRandomNumber(1)+5;
int speed = (Greenfoot.getRandomNumber(6)+3)*(1);
Class[] clss = {MrBoom.class};
/**
* Round rnd- allows DamageField to know which round it is
* damage is calculated by round
* Actor actr-gets the Actor DamageField was created by
* damage is calculated by Actor type
*/
public EnemyDamageField(Round rnd,Class obj)
{
damageType=obj;
round=rnd;
}
/**
* Act - do whatever the DamageField wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
aquireTarget();
// if(canSee(EnemyDamageField.class)&&getWorld()!=null )
// eat(EnemyDamageField.class);
if(life<=0||atWorldEdge()&&getWorld()!=null||speed<=0)
getWorld().removeObject(this);
move(-1);
speed--;
life--;
}
public int power()
{
if(damageType.equals(Skeletor.class))
damagePower = 1;
return damagePower;
}
public void aquireTarget()
{
for(int i=0;i<clss.length;i++)
{
Ally ally = (MrBoom)(getOneObjectAtOffset(0, 0, Ally.class)) ;
Castle castle = (Castle)(getOneObjectAtOffset(0, 0, Castle.class));
if(ally!=null)
{
turnTowards(ally.getX(), ally.getY());
getWorld().removeObject(this);
}
else if(castle!=null&&ally!=null)
{
turnTowards(ally.getX(), ally.getY());
getWorld().removeObject(this);
}
else if(castle!=null)
{
turnTowards(castle.getX(), castle.getY());
castle.getHealthBar().subtract(power());
getWorld().removeObject(this);
}
}
}
}


