So I am trying to make code for laser collision, where whenever the laser is shot and collides with a body part of a centipede, the centipede and the laser will disappear. When I run my code, this happens, but then it follow up with an error saying:
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:714)
How do I solve this issue? The issue is apparently on line 46 for the centipede class
Here is my code for the laser:
and the code for the centipede:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Laser here. * * @author (your name) * @version (a version number or a date) */ public class Laser extends Actor { /** * Act - do whatever the Laser wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public final int speed; public Laser(){ speed = 10; } public void act() { setLocation(getX(), getY() - speed); } }
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.List; /** * Write a description of class Ball here. * * @author (your name) * @version (a version number or a date) */ public class Centipede extends Actor { /** * Act - do whatever the Ball wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ boolean bounceWall; int width; int height; int dx; int dy; int counter; int bounceCounter; public Centipede(){ GreenfootImage image = getImage(); image.scale(25, 25); bounceWall = false; width = image.getWidth(); height = image.getHeight(); dx = width / 5; dy = 0; counter = 0; bounceCounter = 0; setImage(image); } public void act() { if(counter != 0){ wallCollision(); } counter++; mushroomCollision(); laserCollision(); setLocation(getX() + dx, getY() + dy); dy = 0; bounceWall = false; }