so my game worked fine until I added another actor (flyingsaucer) to mimic my existing actor (asteroid) and now im getting this error. please help this is due tonight.
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:711)
at greenfoot.Actor.getOneIntersectingObject(Actor.java:958)
at Bullet.checkFlyingSaucerHit(Bullet.java:45)
at Bullet.act(Bullet.java:38)
at greenfoot.core.Simulation.actActor(Simulation.java:604)
at greenfoot.core.Simulation.runOneLoop(Simulation.java:562)
at greenfoot.core.Simulation.runContent(Simulation.java:221)
at greenfoot.core.Simulation.run(Simulation.java:211)
}
}
import greenfoot.*;
import java.util.List;
public class Asteroid extends SmoothMover
{
private int size;
private int stability;
public Asteroid()
{
this(50);
}
public Asteroid(int size)
{
super(new Vector(Greenfoot.getRandomNumber(360), 2));
setSize(size);
}
public Asteroid(int size, Vector speed)
{
super(speed);
setSize(size);
}
public void act()
{
move();
}
public void setSize(int size)
{
stability = size;
this.size = size;
GreenfootImage image = getImage();
image.scale(size, size);
}
public int getStability()
{
return stability;
}
public void hit(int damage) {
stability = stability - damage;
if(stability <= 0)
breakUp ();
}
private void breakUp()
{
Greenfoot.playSound("Explosion.wav");
if(size <= 16)
{
Space space = (Space) getWorld();
space.countScore();
getWorld().removeObject(this);
return;
}
else
{
int r = getMovement().getDirection() + Greenfoot.getRandomNumber(45);
double l = getMovement().getLength();
Vector speed1 = new Vector(r + 60, l * 1.2);
Vector speed2 = new Vector(r - 60, l * 1.2);
Asteroid a1 = new Asteroid(size/2, speed1);
Asteroid a2 = new Asteroid(size/2, speed2);
getWorld().addObject(a1, getX(), getY());
getWorld().addObject(a2, getX(), getY());
a1.move();
a2.move();
getWorld().removeObject(this);
return;
}
}
}import greenfoot.*;
import java.util.List;
public class Bullet extends SmoothMover
{
private static final int damage = 16;
private int life = 30;
public Bullet()
{
}
public Bullet(Vector speed, int rotation)
{
super(speed);
setRotation(rotation);
addForce(new Vector(rotation, 15));
Greenfoot.playSound("EnergyGun.wav");
}
public void act()
{
if(life <= 0) {
getWorld().removeObject(this);
return;
}
else {
life--;
move();
checkAsteroidHit();
checkFlyingSaucerHit();
}
}
private void checkFlyingSaucerHit()
{
FlyingSaucer flyingsaucer = (FlyingSaucer) getOneIntersectingObject(FlyingSaucer.class);
if (flyingsaucer != null){
getWorld().removeObject(this);
flyingsaucer.hit(damage);
return;
}
}
private void checkAsteroidHit()
{
Asteroid asteroid = (Asteroid) getOneIntersectingObject(Asteroid.class);
if (asteroid != null){
getWorld().removeObject(this);
asteroid.hit(damage);
return;
}[code]import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
import java.awt.Color;
import java.util.List;
public class Space extends World
{
private Counter scoreCounter;
private int startAsteroids = 3;
private int startFlyingSaucers = 2;
public Space()
{
super(600, 400, 1);
GreenfootImage background = getBackground();
background.setColor(Color.BLACK);
background.fill();
createStars( 300 );
Rocket rocket = new Rocket();
addObject(rocket, getWidth()/2 + 100, getHeight()/2);
addFlyingSaucers(startFlyingSaucers);
addAsteroids(startAsteroids);
scoreCounter = new Counter("Score: ");
addObject(scoreCounter, 60, 380);
Explosion.initializeImages();
ProtonWave.initializeImages();
}
private void addFlyingSaucers(int count)
{
for(int i = 0; i < count; i++)
{
int x = Greenfoot.getRandomNumber(getWidth()/2);
int y = Greenfoot.getRandomNumber(getHeight()/2);
addObject(new FlyingSaucer(), x, y);
}
}
private void addAsteroids(int count)
{
for(int i = 0; i < count; i++)
{
int x = Greenfoot.getRandomNumber(getWidth()/2);
int y = Greenfoot.getRandomNumber(getHeight()/2);
addObject(new Asteroid(), x, y);
}
}
private void createStars( int number )
{
GreenfootImage background = getBackground();
for ( int i = 0; i < number; i++ )
{
int x = Greenfoot.getRandomNumber( getWidth() );
int y = Greenfoot.getRandomNumber( getHeight() );
int color = Greenfoot.getRandomNumber( 256 );
background.setColor( new Color(color, color, color) );
background.fillOval(x, y, 2, 2);
}
}
public void countScore()
{
List<Counter> score = getObjects(Counter.class);
for (Counter s : score)
{
s.add(100);
}
}
public void gameOver()
{
List<Counter> score = getObjects(Counter.class);
for (Counter s : score)
{
int finalScore = s.getValue();
addObject( new ScoreBoard( finalScore ), getWidth()/2, getHeight()/2 );
}
}
}import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
import java.util.List;
public class Rocket extends SmoothMover
{
private static final int gunReloadTime = 5;
private static final int protonReloadTime = 500;
private int reloadDelayCount;
private int protonReloadDelayCount;
private GreenfootImage rocket = new GreenfootImage("rocket.png");
private GreenfootImage rocketWithThrust = new GreenfootImage("rocketWithThrust.png");
public Rocket()
{
reloadDelayCount = 5;
protonReloadDelayCount = 500;
}
public void act()
{
checkKeys();
reloadDelayCount++;
protonReloadDelayCount++;
move();
checkCollision();
}
private void checkKeys()
{ if (Greenfoot.isKeyDown ("left"))
setLocation(getX()-3, getY());
if (Greenfoot.isKeyDown ("right"))
setLocation(getX()+3, getY());
if (Greenfoot.isKeyDown ("up"))
setLocation(getX(), getY()-3);
if (Greenfoot.isKeyDown ("down"))
setLocation(getX(), getY()+3);
ignite(Greenfoot.isKeyDown("right"));
if (Greenfoot.isKeyDown("z"))
{
startProtonWave();
}
if (Greenfoot.isKeyDown("space"))
{
fire();
}
}
private void fire()
{
if (reloadDelayCount >= gunReloadTime)
{
Bullet bullet = new Bullet (getMovement().copy(), getRotation());
getWorld().addObject (bullet, getX(), getY());
bullet.move ();
reloadDelayCount = 0;
}
}
private void ignite( boolean boosterOn )
{
if (boosterOn)
{
setImage(rocketWithThrust);
}
if (!boosterOn)
{
setImage(rocket);
}
}
private void checkCollision()
{
Actor flyingsaucer = getOneIntersectingObject(FlyingSaucer.class);
if (flyingsaucer != null)
{
getWorld().addObject( new Explosion(), getX(), getY() );
Space space = (Space) getWorld();
space.gameOver();
getWorld().removeObject(this);
}
Actor asteroid = getOneIntersectingObject(Asteroid.class);
if (asteroid != null)
{
getWorld().addObject( new Explosion(), getX(), getY() );
Space space = (Space) getWorld();
space.gameOver();
getWorld().removeObject(this);
}
}
private void startProtonWave()
{
if (protonReloadDelayCount >= protonReloadTime)
{
getWorld().addObject( new ProtonWave(), getX(), getY() );
protonReloadDelayCount = 0;
}
}
}import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
public abstract class SmoothMover extends Actor
{
private Vector movement;
private double exactX;
private double exactY;
public SmoothMover()
{
this(new Vector());
}
public SmoothMover(Vector movement)
{
this.movement = movement;
}
public void move()
{
exactX = exactX + movement.getX();
exactY = exactY + movement.getY();
if(exactX >= getWorld().getWidth()) {
exactX = 0;
}
if(exactX < 0) {
exactX = getWorld().getWidth() - 1;
}
if(exactY >= getWorld().getHeight()) {
exactY = 0;
}
if(exactY < 0) {
exactY = getWorld().getHeight() - 1;
}
super.setLocation((int) exactX, (int) exactY);
}
public void setLocation(double x, double y)
{
exactX = x;
exactY = y;
super.setLocation((int) x, (int) y);
}
public void setLocation(int x, int y)
{
exactX = x;
exactY = y;
super.setLocation(x, y);
}
public double getExactX()
{
return exactX;
}
public double getExactY()
{
return exactY;
}
public void addForce(Vector force)
{
movement.add(force);
}
public void accelerate(double factor)
{
movement.scale(factor);
if (movement.getLength() < 0.15) {
movement.setNeutral();
}
}
public double getSpeed()
{
return movement.getLength();
}
public void stop()
{
movement.setNeutral();
}
public Vector getMovement()
{
return movement;
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
public class Explosion extends Actor
{
private final static int IMAGE_COUNT= 12;
private static GreenfootImage[] images;
private int imageNo = 0;
private int increment=1;
public Explosion()
{
initializeImages();
setImage(images[0]);
Greenfoot.playSound("MetalExplosion.wav");
}
public synchronized static void initializeImages()
{
if(images == null) {
GreenfootImage baseImage = new GreenfootImage("explosion-big.png");
images = new GreenfootImage[IMAGE_COUNT];
for (int i = 0; i < IMAGE_COUNT; i++)
{
int size = (i+1) * ( baseImage.getWidth() / IMAGE_COUNT );
images[i] = new GreenfootImage(baseImage);
images[i].scale(size, size);
}
}
}
public void act()
{
setImage(images[imageNo]);
imageNo += increment;
if(imageNo >= IMAGE_COUNT) {
increment = -increment;
imageNo += increment;
}
if(imageNo < 0) {
getWorld().removeObject(this);
}
}
}import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
import java.awt.Color;
import java.awt.Font;
import java.util.Calendar;
public class ScoreBoard extends Actor
{
public static final float FONT_SIZE = 48.0f;
public static final int WIDTH = 400;
public static final int HEIGHT = 300;
public ScoreBoard()
{
this(100);
}
public ScoreBoard(int score)
{
makeImage("Game Over", "Score: ", score);
}
private void makeImage(String title, String prefix, int score)
{
GreenfootImage image = new GreenfootImage(WIDTH, HEIGHT);
image.setColor(new Color(255,255,255, 128));
image.fillRect(0, 0, WIDTH, HEIGHT);
image.setColor(new Color(0, 0, 0, 128));
image.fillRect(5, 5, WIDTH-10, HEIGHT-10);
Font font = image.getFont();
font = font.deriveFont(FONT_SIZE);
image.setFont(font);
image.setColor(Color.WHITE);
image.drawString(title, 60, 100);
image.drawString(prefix + score, 60, 200);
setImage(image);
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
import java.util.List;
public class ProtonWave extends Actor
{
private int imageCount = 0;
private static final int DAMAGE = 30;
private static final int NUMBER_IMAGES= 30;
private static GreenfootImage[] images;
public ProtonWave()
{
initializeImages();
setImage(images[0]);
Greenfoot.playSound("proton.wav");
}
public static void initializeImages()
{
if(images == null)
{
GreenfootImage baseImage = new GreenfootImage("wave.png");
images = new GreenfootImage[NUMBER_IMAGES];
for (int i = 0; i < NUMBER_IMAGES; i++)
{
int size = (i+1) * ( baseImage.getWidth() / NUMBER_IMAGES );
images[i] = new GreenfootImage(baseImage);
images[i].scale(size, size);
}
}
}
public void act()
{
checkCollision();
grow();
}
private void grow()
{
if (imageCount >= NUMBER_IMAGES )
{
getWorld().removeObject(this);
}
else
{
setImage(images[imageCount]);
imageCount++;
}
}
private void checkCollision()
{
int range = getImage().getWidth()/2;
List<Asteroid> asteroids = getObjectsInRange(range, Asteroid.class);
for ( Asteroid asteroid : asteroids )
{
asteroid.hit(DAMAGE);
}
}
}
import greenfoot.Greenfoot;
public final class Vector
{
double dx;
double dy;
int direction;
double length;
public Vector()
{
}
public Vector(int direction, double length)
{
this.length = length;
this.direction = direction;
updateCartesian();
}
public Vector(double dx, double dy)
{
this.dx = dx;
this.dy = dy;
updatePolar();
}
public void setDirection(int direction)
{
this.direction = direction;
updateCartesian();
}
public void add(Vector other)
{
dx += other.dx;
dy += other.dy;
updatePolar();
}
public void setLength(double length)
{
this.length = length;
updateCartesian();
}
public void scale(double factor)
{
length = length * factor;
updateCartesian();
}
public void setNeutral() {
dx = 0.0;
dy = 0.0;
length = 0.0;
direction = 0;
}
public void revertHorizontal() {
dx = -dx;
updatePolar();
}
public void revertVertical() {
dy = -dy;
updatePolar();
}
public double getX() {
return dx;
}
public double getY() {
return dy;
}
public int getDirection() {
return direction;
}
public double getLength() {
return length;
}
private void updatePolar()
{
this.direction = (int) Math.toDegrees(Math.atan2(dy, dx));
this.length = Math.sqrt(dx*dx+dy*dy);
}
private void updateCartesian()
{
dx = length * Math.cos(Math.toRadians(direction));
dy = length * Math.sin(Math.toRadians(direction));
}
public Vector copy()
{
Vector copy = new Vector();
copy.dx = dx;
copy.dy = dy;
copy.direction = direction;
copy.length = length;
return copy;
}
}import greenfoot.*;
import java.util.List;
public class FlyingSaucer extends SmoothMover
{
private int size;
private int stability;
public FlyingSaucer()
{
this(50);
}
public FlyingSaucer(int size)
{
super(new Vector(Greenfoot.getRandomNumber(360), 2));
setSize(size);
}
public FlyingSaucer(int size, Vector speed)
{
super(speed);
setSize(size);
}
public void act()
{
move();
}
public void setSize(int size)
{
stability = size;
this.size = size;
GreenfootImage image = getImage();
image.scale(size, size);
}
public int getStability()
{
return stability;
}
public void hit(int damage) {
stability = stability - damage;
if(stability <= 0)
breakUp ();
}
private void breakUp()
{
Greenfoot.playSound("Explosion.wav");
if(size <= 16)
{
Space space = (Space) getWorld();
space.countScore();
getWorld().removeObject(this);
return;
}
else
{
int r = getMovement().getDirection() + Greenfoot.getRandomNumber(45);
double l = getMovement().getLength();
Vector speed1 = new Vector(r + 60, l * 1.2);
Vector speed2 = new Vector(r - 60, l * 1.2);
FlyingSaucer f1 = new FlyingSaucer(size/2, speed1);
FlyingSaucer f2 = new FlyingSaucer(size/2, speed2);
getWorld().addObject(f1, getX(), getY());
getWorld().addObject(f2, getX(), getY());
f1.move();
f2.move();
getWorld().removeObject(this);
return;
}
}
}