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

2021/2/22

Actor not in the World

Genota Genota

2021/2/22

#
When I shoot at a Enemy it gets removed with the projectile... so far so good. BUT if I shoot outside my World/Vision, an Error comes up: 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:713) at greenfoot.Actor.getOneIntersectingObject(Actor.java:964) at Projectile.remove(Projectile.java:141) at Projectile.act(Projectile.java:74) at greenfoot.core.Simulation.actActor(Simulation.java:567) at greenfoot.core.Simulation.runOneLoop(Simulation.java:530) at greenfoot.core.Simulation.runContent(Simulation.java:193) at greenfoot.core.Simulation.run(Simulation.java:183) My question is: How I fix that? Projectile Code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Projectile here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Projectile extends Mover
{
    /**
     * Act - do whatever the Projectile wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    private int speed = 10;
    private int vSpeed = 0;
    private int accel = 1;
    private int jumpHeight= -20;
    private boolean jumping = false;
    int deathCount = 0;
    boolean isDying = false;
    
    private GreenfootImage run1 = new GreenfootImage("fireball1.png");
    private GreenfootImage run2 = new GreenfootImage("fireball2.png");
    private GreenfootImage run3 = new GreenfootImage("fireball3.png");
    private GreenfootImage run4 = new GreenfootImage("fireball4.png");
    private GreenfootImage run5 = new GreenfootImage("fireball5.png");
    private GreenfootImage run6 = new GreenfootImage("fireball6.png");
    private GreenfootImage run7 = new GreenfootImage("fireball7.png");
    private GreenfootImage run8 = new GreenfootImage("fireball8.png");
    private GreenfootImage run9 = new GreenfootImage("fireball9.png");
    private GreenfootImage run10 = new GreenfootImage("Fireball_explosion1.png");
    private GreenfootImage run11 = new GreenfootImage("Fireball_explosion2.png");
    private GreenfootImage run12 = new GreenfootImage("Fireball_explosion3.png");
    private GreenfootImage run13 = new GreenfootImage("Fireball_explosion4.png");
    private GreenfootImage run14 = new GreenfootImage("Fireball_explosion5.png");
    private GreenfootImage run15 = new GreenfootImage("Fireball_explosion6.png");
    private GreenfootImage run16 = new GreenfootImage("Fireball_explosion7.png");
    private GreenfootSound sound = new GreenfootSound("Projectile_sound.mp3");
    private int frame = 1;
    private int animationCounter = 0;
    public Projectile()
    {
        
    }
    public void act() 
    {
        if(isDying)
        {
         deathCount++;
         if(1 == deathCount)
         setImage("Fireball_explosion1.png");
         else if(2 == deathCount)
         setImage("Fireball_explosion2.png");
         else if(3 == deathCount)
         setImage("Fireball_explosion3.png");
         else if(4 == deathCount)
         setImage("Fireball_explosion4.png");
         else if(5 == deathCount)
         setImage("Fireball_explosion5.png");
         else if(6 == deathCount)
         setImage("Fireball_explosion6.png");
         else if(7 == deathCount)
         setImage("Fireball_explosion7.png");
         else
         getWorld().removeObject(this);
         return;
        }
        if (isTouching(Enemy.class))
        getWorld().removeObject(this);
        move(10.0);
        moveAround();
        animationCounter ++;
        remove();
    }    
    public void moveAround()
    {
        if(animationCounter % 8 == 0)
          animatefireball();
    }
    public void turnToMouse()
    {
        turnTowards(15000,0);
    }
    public void animatefireball()
    {
        if (frame == 1)
        {
            setImage(run1);
        }
        else if(frame ==2)
        {
            setImage(run2);
        }
        else if(frame ==3)
        {
            setImage(run3);
        } 
        else if(frame ==4)
        {
            setImage(run4);
        } 
        else if(frame ==5)
        {
            setImage(run5);
        } 
        else if(frame ==6)
        {
            setImage(run6);
        } 
        else if(frame ==7)
        {
            setImage(run7);
        } 
        else if(frame ==8)
        {
            setImage(run8);
        } 
        else if(frame ==9)
        {
            setImage(run9);
            frame = 1;
            return; 
        }
        
        frame ++;
    }
    public void remove()
    {
     Actor walls = getOneIntersectingObject(Ground.class);
     if(getX() <=1 || getX() >= getWorld().getWidth() -1)
     {
         getWorld().removeObject(this);
        }
        else if(walls != null)
        {
            deathCount = 0;
            isDying = true;
            sound.play();
        }
     Actor Enemy1 = getOneIntersectingObject(Enemy.class);
     if(Enemy1 != null)
     {
        deathCount = 0;
        isDying = true;
        sound.play();
     }
     }
}
Enemy1 Code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Enemy1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */


public class Enemy1 extends Enemy
{
    int speed = -3;
    int count = 0;
    int health = 1;
    boolean hitByProjectile = false;
    /**
     * Act - do whatever the Enemy1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        count++;
        moveAround();
        hitByProjectile();
    } 
    public void moveAround()
    {
        if(count < 120)
        setLocation(getX() + speed,getY());
        else
        {
         speed = - speed; 
         getImage().mirrorHorizontally();
         count = 0;
        }
    }
    public void hitByProjectile()
    {
        if(isTouching(Projectile.class)&& !hitByProjectile)
        {
         health--;
         hitByProjectile = true;
        }
        else if(!isTouching(Projectile.class))
        {
         hitByProjectile = false;   
        }
        if(health <=0)
        {
         getWorld().removeObject(this);   
        }
    }
}
danpost danpost

2021/2/22

#
Insert the following after line 133 in Projectile class:
return;
You need to login to post a reply.