So I'm trying to make a shooting laser, but I am having some trouble. Whenever I press the start button, I have this weird error that says:
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.
I don't know why this is happening. Can someone help me out? Here's my code for my world class:
Code for the player:
Code for the laser:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class MyWorld here. * * @author (your name) * @version (a version number or a date) */ public class MyWorld extends World { /** * Constructor for objects of class MyWorld. * */ public boolean laserSpawned = false; public MyWorld() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(400, 600, 1, false);; addLaser(); addPlayer(); } public void addPlayer(){ Player player = new Player(); int pWidth = player.getImage().getWidth(); int pHeight = player.getImage().getHeight(); addObject(player, getWidth() / 2, getHeight() - pHeight / 2); } public void addLaser(){ Laser laser = new Laser(); addObject(laser, 0, 0); laserSpawned = true; } }
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Player here. * * @author (your name) * @version (a version number or a date) */ public class Player extends Actor { /** * Act - do whatever the Player wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public Player(){ Centipede centipede = new Centipede(); int width = centipede.getImage().getWidth(); int height = centipede.getImage().getHeight(); GreenfootImage image = getImage(); image.scale(width, height); turn(90); } public void act() { movingMech(); shootingMech(); } private void movingMech(){ World world = getWorld(); int worldWidth = world.getWidth(); int worldHeight = world.getHeight(); int width = getImage().getWidth(); int height = getImage().getHeight(); int dx = 0; int dy = 0; if(Greenfoot.isKeyDown("left")){ dy = 0; if(getX() < width / 2){ dx = 0; }else{ dx = -width / 2; } }else if(Greenfoot.isKeyDown("right")){ dy = 0; if(getX() > worldWidth - width / 2){ dx = 0; }else{ dx = width / 2; } }else if(Greenfoot.isKeyDown("up")){ dx = 0; if(getY() < worldHeight * 0.8 + height / 2){ dy = 0; }else{ dy = -height / 2; } }else if(Greenfoot.isKeyDown("down")){ dx = 0; if(getY() > worldHeight - height){ dy = 0; }else{ dy = height / 2; } } setLocation(getX() + dx, getY() + dy); } private void shootingMech(){ if(Greenfoot.isKeyDown("space")){ MyWorld w = (MyWorld)getWorld(); w.addLaser(); } } }
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 void act() { boolean laserSpawn = ((MyWorld) getWorld()).laserSpawned; if(laserSpawn == true){ Player player = new Player(); int PlayerX = player.getX(); int PlayerY = player.getY(); int width = player.getImage().getWidth(); int height = player.getImage().getHeight(); GreenfootImage image = getImage(); image.scale(width, height); setLocation(PlayerX, PlayerY - height / 2); } } }