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

2016/3/16

Help ... Actor not in world... T_T

ryvetra ryvetra

2016/3/16

#
i really don't know about this , i have another greenfoot Object with the same code , and it works. but this code somehow didn't work in my new project , please help
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Bullet here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Bullet extends WorldEdge
{
    /**
     * Act - do whatever the Bullet wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move(10);
        destroy();
        checkout1();
    }    
    private void destroy()
    {
        Actor rock = getOneIntersectingObject(Rock.class);
        if(getWorld() != null && rock != null)
        {
            World myWorld = getWorld();
            myWorld.removeObject(rock);
            myWorld.removeObject(this);
            return;
        }
    }
    public void checkout1()
    {
        if (this.atWorldEdge1() ) {
            getWorld().removeObject(this);
            return;
        }
    }
    public boolean atWorldEdge1()
    {
        if(getWorld() != null && getX() < 0 || getX() > getWorld().getWidth() - 0)
            return true;            
        if(getWorld() != null && getY() < 0 || getY() > getWorld().getHeight() - 0)
            return true;
        else
            return false;
    }
}
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:695) at greenfoot.Actor.getX(Actor.java:157) at Bullet.atWorldEdge1(Bullet.java:54) at Bullet.checkout1(Bullet.java:47) at Bullet.act(Bullet.java:22) 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)
davmac davmac

2016/3/16

#
Unfortunately the code you posted isn't exactly the same as the code which generated the error (it says the error was on line 54, but you have only posted 48 lines). However, I believe the problem is these lines (41-44):
        if(getWorld() != null && getX() < 0 || getX() > getWorld().getWidth() - 0)
            return true;            
        if(getWorld() != null && getY() < 0 || getY() > getWorld().getHeight() - 0)
            return true;
&& "binds tighter" than ||, so you need parentheses to make sure you don't call getX() or getY() when the object has been removed from the world:
        if(getWorld() != null && (getX() < 0 || getX() > getWorld().getWidth() - 0))
            return true;            
        if(getWorld() != null && (getY() < 0 || getY() > getWorld().getHeight() - 0))
            return true;
ryvetra ryvetra

2016/3/16

#
that error i screenshot before i delete unnecessary comment ,so .. yeah .. anyway ... it works .. but after i add a new code in method destroy() , it happen again i shot the enemy.class , it remove .but when i shot rock.class , the error "Actor not in the wolrd" come back.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Bullet here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Bullet extends WorldEdge
{

    private static final int ENEMY_DELAY = 100;
    private int enemyDelayCount;
    /**
     * Act - do whatever the Bullet wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move(10);
        destroy();
        checkout();
        checkNewEnemy();
        //return;
    }    

    private void destroy()
    {
        Actor rock = getOneIntersectingObject(Rock.class);
        if(getWorld() != null && (rock != null))
        {
            World myWorld = getWorld();
            myWorld.removeObject(rock);
            myWorld.removeObject(this);
        }
        Actor enemy = getOneIntersectingObject(Enemy.class);
        if(getWorld() != null && (enemy != null))
        {
            World myWorld = getWorld();
            myWorld.removeObject(enemy);
            myWorld.removeObject(this);
        }
    }

    private void newEnemy()
    {
        enemyDelayCount = ENEMY_DELAY;
    }

    private void checkNewEnemy()
    {
        if (enemyDelayCount > 0){
            enemyDelayCount--;
            if(enemyDelayCount == 0){
                createNewEnemy();

            }
        }
    }

    private void createNewEnemy()
    {
        Enemy newEnemy = new Enemy();        
        World world = getWorld();

        int worldWidth = world.getWidth();
        int worldHeight = world.getHeight();
        int x = Greenfoot.getRandomNumber(worldWidth);
        int y = Greenfoot.getRandomNumber(worldHeight);;

        world.addObject(newEnemy, x ,y);
    }
}
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:695) at greenfoot.Actor.getOneIntersectingObject(Actor.java:942) at Bullet.destroy(Bullet.java:36) at Bullet.act(Bullet.java:21) 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)
ryvetra ryvetra

2016/3/16

#
an also , i make a method that will pop up new Enemy at Random place if it get Shot same with the other sceneio (trick and turtle) . it compiled , but the Enemy didn't pop up after that .
Super_Hippo Super_Hippo

2016/3/16

#
Line 36 doesn't work if line 34 was executed. In other words, you can't find intersecting objects of an object which isn't in the world anymore. To avoid that, you can either place line 36 after line 29 or add 'return;' after line 34. For the last thing, show the method
ryvetra ryvetra

2016/3/16

#
thank you .. it works\ the method is in that code too , newEnemy() checkNewEnemy() createNew Enemy()

    private static final int ENEMY_DELAY = 10;
    private int enemyDelayCount;

public void act() 
    {
        move(10);
        destroy();
        checkout();
        checkNewEnemy();
        //return;
    }    
 private void newEnemy()
    {
        enemyDelayCount = ENEMY_DELAY;
    }

    private void checkNewEnemy()
    {
        if (enemyDelayCount > 0){
            enemyDelayCount--;
            if(enemyDelayCount == 0){
                createNewEnemy();

            }
        }
    }

    private void createNewEnemy()
    {
        Enemy newEnemy = new Enemy();        
        World world = getWorld();

        int worldWidth = world.getWidth();
        int worldHeight = world.getHeight();
        int x = Greenfoot.getRandomNumber(worldWidth);
        int y = Greenfoot.getRandomNumber(worldHeight);;

        world.addObject(newEnemy, x ,y);
    }
danpost danpost

2016/3/16

#
Call the 'destroy' method last in the 'act' method or add the following line after the call to 'destroy' in the 'act' method:
if (getWorld() == null) return;
Super_Hippo Super_Hippo

2016/3/16

#
Well, you can just call the 'checkEnemy'-method before the 'destroy'-method. But... Answer these questions: - Why does the 'Bullet' class extends a 'WorldEdge' class? A bullet is not a WorldEdge. Why is the WorldEdge a class at all? - Why does a bullet create new enemies?
danpost danpost

2016/3/16

#
What code do you have in the WorldEdge class?
ryvetra ryvetra

2016/3/16

#
it stil didn't pop up... @Supper_Hippo - i make a super class in every class , because every class need atWorldEdge method - i am following the code from trick and turtle scenario ..
ryvetra ryvetra

2016/3/16

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class SmoothMover here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class WorldEdge extends Actor
{
    /**
     * Act - do whatever the SmoothMover wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
    }        
    public void checkout()
    {
        if (this.atWorldEdge() ) {
            getWorld().removeObject(this);
            return;
        }
    }
    public boolean atWorldEdge()
    {
        if(getWorld() != null && (getX() < 5 || getX() > getWorld().getWidth() - 5))
            return true;            
        if(getWorld() != null && (getY() < 5 || getY() > getWorld().getHeight() - 5))
            return true;
        else
            return false;
    }
    public boolean canSee(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0, 0, clss);
        return actor != null;        
    }
}
its the same from class "animal" in "trick the turtle" scenario
danpost danpost

2016/3/16

#
It does no good to add a return statement at the end of a method (like as in the 'checkout' method). The reason for adding a return statement is to prevent later code within the same method from executing. When there is no later code, the method will be exited from anyway. Any and all code that may be executed after a 'removeObject(this)' line is executed, whether in the same method or in other methods, should be avoided -- and must be avoided if any method calls require the actor still be in a world. Just for clarification, you said:
- i make a super class in every class , because every class need atWorldEdge method
Is this saying that you have a separate super class for each of these classes? or you use the same super class for all of them?
ryvetra ryvetra

2016/3/16

#
i use the same super class ...
You need to login to post a reply.