I get an error of the acotr not being in the world, but the actor is in the world...
"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 Zombie.act(Zombie.java:51)"
Here is the code:
The weird thing is that it worked before...
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) import java.awt.Color; /** * * * @author Dallin Wrathall */ public class MyWorld extends World { public int level; private Counter theCounter; private Survivor theSurvivor; public MyWorld() { super(600,400, 1); GreenfootImage background = getBackground(); background.setColor(Color.WHITE); background.fill(); theCounter = new Counter(); addObject(new Survivor(), 300, 200); theSurvivor = new Survivor(); addObject(theCounter, 300, 20); } public void act() { // level 0 is actually level 1 if (level == 0) { if (Greenfoot.getRandomNumber(1000) < 5) { addObject(new Zombie(),0,0) ; } if (Greenfoot.getRandomNumber(1000) < 5) { addObject(new Zombie(), 600, 0); } if (Greenfoot.getRandomNumber(1000) < 5) { addObject(new Zombie(), 600,400); } if (Greenfoot.getRandomNumber(1000) < 5) { addObject(new Zombie(), 0, 400); } } //level 1 is actually level 2 if (level == 1) { if (Greenfoot.getRandomNumber(300) < 5) { addObject(new Zombie(),0,0) ; } if (Greenfoot.getRandomNumber(300) < 5) { addObject(new Zombie(), 600, 0); } if (Greenfoot.getRandomNumber(300) < 5) { addObject(new Zombie(), 600,400); } if (Greenfoot.getRandomNumber(300) < 5) { addObject(new Zombie(), 0, 400); } } //level 2 is actually level 3 if (level == 2) { if (Greenfoot.getRandomNumber(100) < 5) { addObject(new Zombie(),0,0) ; } if (Greenfoot.getRandomNumber(100) < 5) { addObject(new Zombie(), 600, 0); } if (Greenfoot.getRandomNumber(100) < 5) { addObject(new Zombie(), 600,400); } if (Greenfoot.getRandomNumber(100) < 5) { addObject(new Zombie(), 0, 400); } } } public Counter getCounter() { return theCounter; } public Survivor getSurvivor() { return theSurvivor; } public void levelSwitch(int amount) { level += amount; } }
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) /** * A survivor of the zombie apocolypse, control using a,w,s,d. * The gun is fired by hitting the 'space' key. * * */ public class Survivor extends Mover { private int gunReloadTime; private int reloadDelayCount; private Vector acceleration; public int shotsFired; public static Survivor main; public static boolean survivorAlive; public int weapon; /** * */ public Survivor() { main = this; gunReloadTime = 20; reloadDelayCount = 0; shotsFired = 0; survivorAlive = false; } /** * Do what a Survivor's gotta do. (Which is:shooting when the rig keys are pressed.) */ public void act() { survivorAlive = true; MouseInfo mouse = Greenfoot.getMouseInfo(); if (Greenfoot.mouseMoved(null)) setRotation(getRotationToPoint(mouse.getX(), mouse.getY())); move(); checkCollision(); checkKeys(); reloadDelayCount++; if (weapon == 2) gunReloadTime = 15; } public int getShotsFired() { return shotsFired; } public void setGunReloadTime(int reloadTime) { gunReloadTime = reloadTime; } private void checkCollision() { Zombie a = (Zombie) getOneIntersectingObject(Zombie.class); if(a != null) { getWorld().addObject(new Explosion(), getX(), getY()); survivorAlive = false; getWorld().removeObject(this); } } /** * Check whether there are any key pressed and react to them. */ private void checkKeys() { if(Greenfoot.isKeyDown("w")) { setLocation(getX(),(getY()-2)); } if(Greenfoot.isKeyDown("a")) { setLocation(getX() - 2, getY()); } if(Greenfoot.isKeyDown("d")) { setLocation(getX() + 2, getY()); } if (Greenfoot.isKeyDown("s")) { setLocation(getX(),(getY()+2)); } if(Greenfoot.isKeyDown("space")) { fire(); } } private void ignite(boolean boosterOn) { if(boosterOn) { acceleration.setDirection(getRotation()); increaseSpeed(acceleration); } else { } } /** * Fire a bullet if the gun is ready. */ private void fire() { if(reloadDelayCount >= gunReloadTime) { Bullet b = new Bullet(getMovement().copy(), getRotation()); getWorld().addObject(b, getX(), getY()); b.move(); shotsFired++; reloadDelayCount = 0; } } public int getRotationToPoint(int x, int y) { int angle = 1; if (getX() > x && getY() > y) { int opp = getY() - y; int adj = getX() - x; angle = (int)(180 + Math.toDegrees(Math.atan(opp/adj))); } else if (getX() > x && getY() < y) { int opp = y - getY(); int adj = getX() - x; angle = (int)(180 - Math.toDegrees(Math.atan(opp/adj))); } else if (getX() < x && getY() > y) { int opp = getY() - y; int adj = x - getX(); angle = (int)(0 - Math.toDegrees(Math.atan(opp/adj))); } else if (getX() < x && getY() < y) { int opp = y - getY(); int adj = x - getX(); angle = (int)(360 + Math.toDegrees(Math.atan(opp/adj))); } return angle; } public void upgradeWeapon(int amount) { weapon += amount; } }
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) /** * A brain eating infected dude. * * * @author Dallin Wrathall */ public class Zombie extends Mover { /** Size of this Zombie */ private int size; /** When the Zombie's life reaches 0 the Zombie will die */ private int stability; public int killed; public Zombie() { this(64); } /** * Create an Zombie with a given size and default speed. */ public Zombie(int size) { size = Greenfoot.getRandomNumber(200); } /** * Create an Zombie with a given size size and speed. */ private Zombie(int size, Vector speed) { super(speed); setSize(size); } /** * Let the Zombie act. That is: go to Survivor. */ public void act() { move(2); turnTowards(Survivor.main.getX(), (Survivor.main.getY())); } /** * Set the size of this Zombie. */ public void setSize(int size) { stability = size; this.size = size; GreenfootImage image = getImage(); image.scale(size, size); } /** * Return the current stability of this Zombie. (If it goes down to * zero, it breaks up.) */ public int getStability() { return stability; } /** * Hit this Zombie dealing the given amount of damage. */ public void hit(int damage) { stability = stability - damage; if(stability <= 0) { getWorld().removeObject(this); } } }