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

2012/12/11

why does this not work?

theuberjew theuberjew

2012/12/11

#
import greenfoot.*; public class Star extends Actor { private int size=5; public Star() { size=Greenfoot.getRandomNumber(2); } private boolean flag = false; public void act() { move(); checkForRocket(); death(); } public void move() { setLocation(getX()+2,getY()); } public void death() { if (getX()==599) { getWorld().removeObject(this); } } { setImage("mario-star.png"); } public void checkForRocket() { Rocket theRocket = null; theRocket = (Rocket)getOneIntersectingObject(Rocket.class); if(theRocket != null) { if (flag == false) { flag = true; getWorld().removeObject(this); } } } } This is the error 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:655) at greenfoot.Actor.getOneIntersectingObject(Actor.java:904) at Star.checkForRocket(Star.java:44) at Star.act(Star.java:21) at greenfoot.core.Simulation.actActor(Simulation.java:507) at greenfoot.core.Simulation.runOneLoop(Simulation.java:470) at greenfoot.core.Simulation.runContent(Simulation.java:204) at greenfoot.core.Simulation.run(Simulation.java:194) 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:655) at greenfoot.Actor.getOneIntersectingObject(Actor.java:904) at Star.checkForRocket(Star.java:44) at Star.act(Star.java:21) at greenfoot.core.Simulation.actActor(Simulation.java:507) at greenfoot.core.Simulation.runOneLoop(Simulation.java:470) at greenfoot.core.Simulation.runContent(Simulation.java:204) at greenfoot.core.Simulation.run(Simulation.java:194) 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:655) at greenfoot.Actor.getOneIntersectingObject(Actor.java:904) at Star.checkForRocket(Star.java:44) at Star.act(Star.java:21) at greenfoot.core.Simulation.actActor(Simulation.java:507) at greenfoot.core.Simulation.runOneLoop(Simulation.java:470) at greenfoot.core.Simulation.runContent(Simulation.java:204) at greenfoot.core.Simulation.run(Simulation.java:194) 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:655) at greenfoot.Actor.getOneIntersectingObject(Actor.java:904) at Star.checkForRocket(Star.java:44) at Star.act(Star.java:21) at greenfoot.core.Simulation.actActor(Simulation.java:507) at greenfoot.core.Simulation.runOneLoop(Simulation.java:470) at greenfoot.core.Simulation.runContent(Simulation.java:204) at greenfoot.core.Simulation.run(Simulation.java:194) 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:655) at greenfoot.Actor.getX(Actor.java:157) at Star.death(Star.java:31) at Star.act(Star.java:21) at greenfoot.core.Simulation.actActor(Simulation.java:507) at greenfoot.core.Simulation.runOneLoop(Simulation.java:470) at greenfoot.core.Simulation.runContent(Simulation.java:204) at greenfoot.core.Simulation.run(Simulation.java:194) 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:655) at greenfoot.Actor.getX(Actor.java:157) at Star.death(Star.java:31) at Star.act(Star.java:21) at greenfoot.core.Simulation.actActor(Simulation.java:507) at greenfoot.core.Simulation.runOneLoop(Simulation.java:470) at greenfoot.core.Simulation.runContent(Simulation.java:204) at greenfoot.core.Simulation.run(Simulation.java:194) 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:655) at greenfoot.Actor.getX(Actor.java:157) at Star.death(Star.java:31) at Star.act(Star.java:21) at greenfoot.core.Simulation.actActor(Simulation.java:507) at greenfoot.core.Simulation.runOneLoop(Simulation.java:470) at greenfoot.core.Simulation.runContent(Simulation.java:204) at greenfoot.core.Simulation.run(Simulation.java:194) 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:655) at greenfoot.Actor.getX(Actor.java:157) at Star.death(Star.java:31) at Star.act(Star.java:21) at greenfoot.core.Simulation.actActor(Simulation.java:507) at greenfoot.core.Simulation.runOneLoop(Simulation.java:470) at greenfoot.core.Simulation.runContent(Simulation.java:204) at greenfoot.core.Simulation.run(Simulation.java:194) 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:655) at greenfoot.Actor.getX(Actor.java:157) at Star.death(Star.java:30) at Star.act(Star.java:20) at greenfoot.core.Simulation.actActor(Simulation.java:507) at greenfoot.core.Simulation.runOneLoop(Simulation.java:470) at greenfoot.core.Simulation.runContent(Simulation.java:204) at greenfoot.core.Simulation.run(Simulation.java:194) 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:655) at greenfoot.Actor.getX(Actor.java:157) at Star.death(Star.java:30) at Star.act(Star.java:20) at greenfoot.core.Simulation.actActor(Simulation.java:507) at greenfoot.core.Simulation.runOneLoop(Simulation.java:470) at greenfoot.core.Simulation.runContent(Simulation.java:204) at greenfoot.core.Simulation.run(Simulation.java:194) 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:655) at greenfoot.Actor.getX(Actor.java:157) at Star.death(Star.java:30) at Star.act(Star.java:20) at greenfoot.core.Simulation.actActor(Simulation.java:507) at greenfoot.core.Simulation.runOneLoop(Simulation.java:470) at greenfoot.core.Simulation.runContent(Simulation.java:204) at greenfoot.core.Simulation.run(Simulation.java:194) 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:655) at greenfoot.Actor.getX(Actor.java:157) at Star.death(Star.java:30) at Star.act(Star.java:20) at greenfoot.core.Simulation.actActor(Simulation.java:507) at greenfoot.core.Simulation.runOneLoop(Simulation.java:470) at greenfoot.core.Simulation.runContent(Simulation.java:204) at greenfoot.core.Simulation.run(Simulation.java:194) 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:655) at greenfoot.Actor.getX(Actor.java:157) at Star.death(Star.java:30) at Star.act(Star.java:20) at greenfoot.core.Simulation.actActor(Simulation.java:507) at greenfoot.core.Simulation.runOneLoop(Simulation.java:470) at greenfoot.core.Simulation.runContent(Simulation.java:204) at greenfoot.core.Simulation.run(Simulation.java:194) 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:655) at greenfoot.Actor.getX(Actor.java:157) at Star.death(Star.java:30) at Star.act(Star.java:20) at greenfoot.core.Simulation.actActor(Simulation.java:507) at greenfoot.core.Simulation.runOneLoop(Simulation.java:470) at greenfoot.core.Simulation.runContent(Simulation.java:204) at greenfoot.core.Simulation.run(Simulation.java:194) 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:655) at greenfoot.Actor.getX(Actor.java:157) at Star.death(Star.java:30) at Star.act(Star.java:20) at greenfoot.core.Simulation.actActor(Simulation.java:507) at greenfoot.core.Simulation.runOneLoop(Simulation.java:470) at greenfoot.core.Simulation.runContent(Simulation.java:204) at greenfoot.core.Simulation.run(Simulation.java:194) 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:655) at greenfoot.Actor.getX(Actor.java:157) at Star.death(Star.java:30) at Star.act(Star.java:20) at greenfoot.core.Simulation.actActor(Simulation.java:507) at greenfoot.core.Simulation.runOneLoop(Simulation.java:470) at greenfoot.core.Simulation.runContent(Simulation.java:204) at greenfoot.core.Simulation.run(Simulation.java:194) 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:655) at greenfoot.Actor.getX(Actor.java:157) at Star.death(Star.java:30) at Star.act(Star.java:20) at greenfoot.core.Simulation.actActor(Simulation.java:507) at greenfoot.core.Simulation.runOneLoop(Simulation.java:470) at greenfoot.core.Simulation.runContent(Simulation.java:204) at greenfoot.core.Simulation.run(Simulation.java:194) 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:655) at greenfoot.Actor.getX(Actor.java:157) at Star.death(Star.java:30) at Star.act(Star.java:20) at greenfoot.core.Simulation.actActor(Simulation.java:507) at greenfoot.core.Simulation.runOneLoop(Simulation.java:470) at greenfoot.core.Simulation.runContent(Simulation.java:204) at greenfoot.core.Simulation.run(Simulation.java:194) 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:655) at greenfoot.Actor.getX(Actor.java:157) at Star.death(Star.java:30) at Star.act(Star.java:20) at greenfoot.core.Simulation.actActor(Simulation.java:507) at greenfoot.core.Simulation.runOneLoop(Simulation.java:470) at greenfoot.core.Simulation.runContent(Simulation.java:204) at greenfoot.core.Simulation.run(Simulation.java:194) 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:655) at greenfoot.Actor.getX(Actor.java:157) at Star.death(Star.java:30) at Star.act(Star.java:20) at greenfoot.core.Simulation.actActor(Simulation.java:507) at greenfoot.core.Simulation.runOneLoop(Simulation.java:470) at greenfoot.core.Simulation.runContent(Simulation.java:204) at greenfoot.core.Simulation.run(Simulation.java:194)
bbwf bbwf

2012/12/11

#
Not a clue
vonmeth vonmeth

2012/12/11

#
In checkForRocket(), you have a chance of removing the Star from the world. When this happens, death() still runs after removing the Star. death() attempts to get the Star's location, but the Star is no longer in the world, hence the error.
theuberjew theuberjew

2012/12/11

#
Thank You!
Zamoht Zamoht

2012/12/11

#
I would say that it has something to do with the fact that you use your death() method after the checkForRocket() method. So if the object is removed by the checkForRocket() method the death() will look for the objects position, which is not posible since the object is removed. you could change your act() to this. public void act() { move(); checkForRocket(); if (flag == false) { death(); } } Okay a little late...
danpost danpost

2012/12/11

#
I do not see what might be causing this. The program trace indicates that the Star object being acted on is in the world (or Simulation.actActor would not be in the trace). The 'move' method does not remove the object from the world; and, therefore, the 'checkForRocket' method should not error. I do, however, see some extraneous code in the class; namely:
{
            setImage("mario-star.png");
           
        }
Remove it and see if it then runs OK.
theuberjew theuberjew

2012/12/12

#
it worked thanks zamoht. ^.^
theuberjew theuberjew

2012/12/12

#
i need to add a score to this any ideas on how to go about this?
danpost danpost

2012/12/12

#
I must have been looking at the stack trace backwards; thinking the top line was the last error message! LOL
danpost danpost

2012/12/12

#
When you say a score, do you mean a scoring object (something that changes as the score changes) or a final scorboard object (for the end score)?
theuberjew theuberjew

2012/12/12

#
a scoring object when my rocket hits a star i want it to count a score for each one
danpost danpost

2012/12/12

#
Create a new subclass of Actor and call it 'Score'. A simple class code would be:
import greenfoot.*;
import java.awt.Color;

public class Score extends Actor
{
    int score=0;

    public Score()
    {
        updateImage();
    }

    private void updateImage()
    {
        setImage(new GreenfootImage("Score: "+score, 18, Color.white, Color.black));
    }

    public void setScore(int newValue)
    {
        score=newValue;
        updateImage();
    }

    public void add(int addAmount)
    {
        score+=addAmount;
        updateImage();
    }

    public int getScore()
    {
        return score;
    }
}
Then, in the rocket class, do the following
// add the instance field
Score score = new Score();
//  add the new method
public Score getScoreObject()
{
    return score;
}
And finally, in the world class, after creating and adding the rocket to the world
addObject(rocket.getScoreObject(), 100, 25);
The rocket must be created and added to the world in two steps:
Rocket rocket = new Rocket();
addObject(rocket, 200, 350);
Of course, the placement of the objects should be adjusted to suit your needs, but the basic coding should work.
theuberjew theuberjew

2012/12/13

#
Thank you for all your help my program runs fine now
You need to login to post a reply.