This site requires JavaScript, please enable it in your browser!
Greenfoot back
fabipfolix
fabipfolix wrote ...

2018/6/7

Actor not in world - Error at the End of the game

fabipfolix fabipfolix

2018/6/7

#
In my game I got the class Player and a class Explosion. If the Player explodes I'll get this Error although the game should be stopped. 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:681) at greenfoot.Actor.getObjectsInRange(Actor.java:900) at Explosion.expand(Explosion.java:29) at Explosion.act(Explosion.java:12) at greenfoot.core.Simulation.actActor(Simulation.java:594) at greenfoot.core.Simulation.runOneLoop(Simulation.java:552) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) Explosion Class:
import greenfoot.*;

public class Explosion extends Actor
{
    int range = 300;
    int height;
    int width;
    int count=0;
    public void act() {
        count++;
        if(count>10){
            expand();
        }
    }
    
    public void expand(){
        
        height = (int)(this.getImage().getHeight()*1.2);
        width = (int)(this.getImage().getWidth()*1.2);
        if(width<range){
            this.getImage().scale(width, height);
        } else {
            if(isTouching(Player.class)){
                 
                 Player p = (Player)getOneIntersectingObject(Player.class);
                 p.gameOver();

            }
            getWorld().removeObjects(getObjectsInRange(range, Actor.class));
            getWorld().removeObject(this);
            
        }
    }
}
Player Class:
import greenfoot.*;
import java.util.List;

public class Player extends Actor{
    
    int speed = 3;
    int hp = 100;
    public int weapon = 1; 
    
    /* 1 = pistol
     * 2 = Assault Rifle
     * 3 = Sniper
     * 4 = Laser
     */
    int level = 1;
    public int delay= 2000;
    int wave = 1;
    
    public void act() 
    {
        delay++;
        
        enemiesAlive();

        checkKeypress();
        
        MouseInfo mouse = Greenfoot.getMouseInfo();
        if(mouse != null) {
            setRotation((int)(180*Math.atan2(mouse.getY()-getY(),mouse.getX()-getX())/Math.PI));
        }
        
        pickWeapon();
        enemiesAlive();
    }  
    
    public void checkKeypress(){
        int dx = 0;
        int dy = 0;
        if(Greenfoot.isKeyDown("W"))
        {
            dy -= speed;
        }
        if(Greenfoot.isKeyDown("S"))
        {
            dy += speed;
        }
        if(Greenfoot.isKeyDown("A"))
        {
            dx -= speed;
        }
        if(Greenfoot.isKeyDown("D"))
        {
            dx += speed;
        }
        if(Greenfoot.mouseClicked(null)||Greenfoot.isKeyDown("space"))
        {
            shoot();
        }
        setLocation(getX() + dx, getY() + dy);
        
    }
    
    public void pickWeapon() {
        String key = Greenfoot.getKey();

        if(isTouching(Pistol.class)&&(key != null && key.equals("f"))) {
            removeTouching(Pistol.class);
            weapon = 1; //changes to the Pistol
           
            setImage("PlayerPistol.png");
        } else if(isTouching(AR.class)&&(key != null && key.equals("f"))) {
            removeTouching(AR.class);
            weapon = 2; //changes to the AR
            setImage("PlayerAR.png");
        } else if(isTouching(Sniper.class)&&(key != null && key.equals("f"))) {
            removeTouching(Sniper.class);
            weapon = 3; //changes to the Sniepr
            setImage("PlayerSniper.png");
        
        }
        
    }
    
    public void shoot(){
       
       int relTime = 0;
       int dmg = 0;
       int recoil = 0;
       switch(weapon){
           case 1: {
               Pistol p = new Pistol();
               relTime = p.getReloadTime();
               dmg = 35;
               recoil = 5;
           } break;
           case 2: {
               AR ar = new AR();
               relTime = ar.getReloadTime();
               dmg = 33;
               recoil = 15;
           } break;
           case 3: {
               Sniper sniper = new Sniper();
               relTime = sniper.getReloadTime();
               dmg = sniper.getDmg();
               recoil = 1;
           }break;
           
        }
       
       
       if(delay>=relTime){
          
           getWorld().addObject(new Bullet(getRotation()+(Greenfoot.getRandomNumber(recoil)-(recoil/2)),dmg),getX(), getY());
           
           
           delay = 0;
        }
    }
    
    public int getDmg(){
        return 10;
    }
    
    public void takeDmg(int dmg){
        hp = hp-dmg;
        if(hp<=0){
            gameOver();
        }
    }
    
    public void enemiesAlive(){
        if(getObjectsInRange(1200,Enemy.class).size()==0){
            spawnEnemies();
        }
    }
    
    public void spawnEnemies(){
        world1 w1= (world1)getWorld();
        Scoreboard sb = w1.getScoreboard();
        sb.addScore(1);
        wave++;
        
        
        for(int i=0; i<=level*level/2;i++){
            
            getWorld().addObject(new Skeleton(),Greenfoot.getRandomNumber(1200),Greenfoot.getRandomNumber(1200));
        }
        for(int i=0; i<=level+1;i++){
           Zombie z= new Zombie();
            getWorld().addObject(z,Greenfoot.getRandomNumber(1200),Greenfoot.getRandomNumber(1200)); 
        }
        for(int i=0; i<=level-4;i++){
           
            getWorld().addObject(new Dragon(),Greenfoot.getRandomNumber(1200),Greenfoot.getRandomNumber(1200)); 
        }
        getWorld().addObject(new Barrel(),Greenfoot.getRandomNumber(1200),Greenfoot.getRandomNumber(1200)); 
        level++;
        getWorld().removeObjects(getObjectsInRange(150,Enemy.class));
    }
    public void gameOver(){
        Greenfoot.stop();
        getWorld().removeObjects(getObjectsInRange(1200, Actor.class));
        GameOver gameOver = new GameOver(wave);
        getWorld().addObject(gameOver, 600,600);
        getWorld().removeObject(this);
    }
}
danpost danpost

2018/6/7

#
Make this line 17 of the Explosion class:
World world = getWorld();
Then on line 29 and 30, change getWorld() to world.
fabipfolix fabipfolix

2018/6/7

#
Thank you it worked. I just get one last Error, everytime the explosion expands: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.rangeCheck(ArrayList.java:653) at java.util.ArrayList.get(ArrayList.java:429) at Explosion.expand(Explosion.java:30) at Explosion.act(Explosion.java:16) at greenfoot.core.Simulation.actActor(Simulation.java:594) at greenfoot.core.Simulation.runOneLoop(Simulation.java:552) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) Explosion Class:
import greenfoot.*;

public class Explosion extends Enemy
{
    int range = 300;
    int height;
    int width;
    int count=0;
    int touchDmg = 500;
    public Explosion(){
        
    }
    public void act() {
        count++;
        if(count>10){
            expand();
            
        }
    }
    
    public void expand(){
        World world = getWorld();
        
        height = (int)(this.getImage().getHeight()*1.2);
        width = (int)(this.getImage().getWidth()*1.2);
        if(width<range){
            this.getImage().scale(width, height);
            
            if(getObjectsInRange(width, Player.class)!=null){
                Player p = (Player) getObjectsInRange(width,Player.class).get(0);
                p.takeDmg(touchDmg);
            }
                
        } else {
            world.removeObjects(getObjectsInRange(range, Actor.class));
            world.removeObject(this);
            
        }
    }
   
}
danpost danpost

2018/6/7

#
Line 29 is asking the wrong thing. Currently, it is asking if a List object is returned; but, what you need to ask is if the List object that is returned (and one will be returned) is empty or not.
fabipfolix fabipfolix

2018/6/7

#
danpost wrote...
Line 29 is asking the wrong thing. Currently, it is asking if a List object is returned; but, what you need to ask is if the List object that is returned (and one will be returned) is empty or not.
Doesn't that mean it has to be
 if(getObjectsInRange(width, Player.class).get(0)!=null){
But that doesn't work as well.
danpost danpost

2018/6/7

#
fabipfolix wrote...
danpost wrote...
Line 29 is asking the wrong thing. Currently, it is asking if a List object is returned; but, what you need to ask is if the List object that is returned (and one will be returned) is empty or not.
Doesn't that mean it has to be
 if(getObjectsInRange(width, Player.class).get(0)!=null){
But that doesn't work as well.
You cannot blindly check the first element of a list without first checking that a first element exists. Use the isEmpty method of the List class; then get the first element if the list is found not to be empty.
You need to login to post a reply.