Super_Hippo wrote...
I already stated it. If 'getOneObjectAtOffset' returns null, it doesn't mean you don't touch the object when you are moving.
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. at greenfoot.Actor.failIfNotInWorld(Actor.java:663) at greenfoot.Actor.getX(Actor.java:157) at Fireball.act(Fireball.java:24) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205)
import greenfoot.*;
public class Fireball extends Actor
{
private int ani = 0; //animation variable
private int timer = 0;
private int bounce = 0; //bounce counter
private boolean onGround; //ground variable
public void act()
{
if (getX() > 954){ //somehow atWorldEdge didn't work, no clue why so I had to do this
getWorld().removeObject(this);
}
dostuff();
if (bounce == 0){ //the ball can bounce from a wall once, then if it hits the wall a second time, gets removed.
bounce();
setLocation(getX()+6, getY()+4);
} else if (bounce == 1){
setLocation(getX()+6, getY()-4);
bounce();
} else if (bounce >1){
getWorld().removeObject(this);
}
}
public void dostuff() { //does stuff
animation();
if (timer >=85 && timer <90){ //despawn animation, when time runs out
setImage("fireball8.png");
} else if (timer >=90 && timer <95){
setImage("fireball9.png");
} else if (timer >=95 && timer <100){
setImage("fireball10.png");
}
ani++; //animation variable +1
timer++; //timer varlable +1
if (timer >=100){ //after a time, the fireball gets removed
getWorld().removeObject(this);
}
}
public void bounce(){
onGround = false; // for current on ground state
Actor ground = getOneIntersectingObject(Ground.class); //checks if in ground of not
if (ground != null){ //If Fireball is in Ground
setLocation(getX()-5, getY()-3);
bounce++;
}
}
public void animation(){ //animation for left movement
if (ani == 0){
setImage("fireball1.png");
}
else if (ani == 8){
setImage("fireball2.png");
}
else if (ani == 16){
setImage("fireball3.png");
}
else if (ani == 24){
setImage("fireball4.png");
}
else if (ani == 32){
setImage("fireball5.png");
}
else if (ani == 40){
setImage("fireball6.png");
}
else if (ani == 48){
setImage("fireball7.png");
ani = 16;
}
}
}if (getWorld() == null) return;