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

2017/4/25

what is the problem

ChrisD ChrisD

2017/4/25

#
 Actor Crystal;
    Crystal = getOneObjectAtOffset(0, 0, Crystal.class);
    if (Crystal != null)
    {
        World world;
        world = getWorld();
        world.removeObject(Crystal);
        Level1 level1 = (Level1) getWorld();
        Counter counter = level1.getCounter();
        counter.bumpCount(1);
    }
when i play my game and i am killed by a meteor an error pops up in red text and it links back to the "Crystal = getOneObjectAtOffset(0, 0, Crystal.class);" code and i cant seem to understand the problem and how it can be fixed so when my character dies a game over screen will pop up
ChrisD ChrisD

2017/4/25

#
a terminal window pops up after i die
Super_Hippo Super_Hippo

2017/4/25

#
It would be easier if you would say what the error message is. The problem is probably that this code is executed after the object was removed from the world. You can either check if your object is still in the world before executing this code or you can immediately exit the act method after you removed the object from the world.
ChrisD ChrisD

2017/4/25

#
the error says "java.lang.IllegalStateExcpectations: 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 do not understand why this is popping up as it was fully functional before a couple of hours ago. the Crystal class has been inserted into the world but i do not know what i have done wrong.
ChrisD ChrisD

2017/4/25

#
.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.
Super_Hippo Super_Hippo

2017/4/25

#
Yes, reread my answer. You remove the object from the world (after intersection with a meteor). After it was removed, the act method will continue running until it reaches its end. You have to prevent the execution of any method which requires the object to be in the world when it was removed.
ChrisD ChrisD

2017/4/25

#
That's the thing though, my code is the same as my friends code which are doing the same thing but i get an error and he doesn't, i haven't added anything nor removed anything but it is having errors now. I understand what you are saying but whatever i do to the code it will not fix.
Super_Hippo Super_Hippo

2017/4/25

#
Show the entire act method + methods which are called from it (if any). I doubt that your friend has exactly the same.
ChrisD ChrisD

2017/4/25

#
I am currently not in school at the moment so i will be sending you the code tomorrow. Glad that you will be helping me with my frustrating situation.
ChrisD ChrisD

2017/4/26

#
    public void act() 
    {
      if (Greenfoot.isKeyDown("right"))
    {
        move(5);
    }
    if (Greenfoot.isKeyDown("left"))
    {
        move(-5);
    }
    hitMeteor();
    Actor Crystal;
    Crystal = getOneIntersectingObject(Crystal.class);
    if (Crystal !=null)
    {
        World world;
        world = getWorld();
        world.removeObject(Crystal);
        Level1 level1 = (Level1) getWorld();
        Counter counter = level1.getCounter();
        counter.bumpCount(1);
    }
 }
This is the code for both the movement of the Space Ship and the collecting of the Crystals that will make the score counter go up by 1 when i come in to contact with the crystal
ChrisD ChrisD

2017/4/26

#
have you any reply for any issues that may be in this code or may it be another problem
danpost danpost

2017/4/26

#
Super_Hippo wrote...
Show the entire act method + methods which are called from it (if any). I doubt that your friend has exactly the same.
This would include the 'hitMeteor' method, which probably has 'getWorld().removeObject(this)' in it. After line 11 (the call to 'hitMeteor'), add the following line:
if (getWorld() == null) return;
ChrisD ChrisD

2017/4/26

#
OMG danpost you just helped me soooo much. thank you!
You need to login to post a reply.