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

2014/1/22

Remove object when ate another object.

casper4444 casper4444

2014/1/22

#
Hello, I have an object Arrow that has to eat Enemy1, Enemy2, Enemy3 and Enemy4. It can be shot from a person as many times as you can, but if it has eaten one of those enemies, it has to remove itself. My Arrow class is a subclass of Weapons. Now I get this error called: java.lang.IllegalStateException: Actor not in world. In my 'tryToEat' method, I have:
1
2
3
4
5
if (canSee(Enemy1.class))
       {
           eat(Enemy1.class);
           getWorld().removeObjects(getWorld().getObjects(Arrow.class));
       }
+ this for Enemy2, 3 & 4 And in my Weapons class I have:
1
2
3
4
5
public boolean canSee(Class clss)
{
    Actor actor = getOneObjectAtOffset(0, 0, clss);
    return actor != null;       
}
1
2
3
4
5
6
7
public void eat(Class clss)
{
    Actor actor = getOneObjectAtOffset(0, 0, clss);
    if(actor != null) {
        getWorld().removeObject(actor);
    }
}
Anyone know how to solve this?
danpost danpost

2014/1/22

#
Once one of the first three 'if' blocks eat an enemy (Enemy1, Enemy2, or Enemy3 object) and the arrow is removed from the world, the following 'if' will cause an exception because the arrow needs to be in the world and it is no longer in it. I never did like the combination of 'canSee' and 'eat'. The 'eat' method must get any actor at the offset (0, 0) again, if the 'canSee' method was used first. I would prefer the 'eat' method return the boolean value itself and dispense with the 'canSee' method. I changed the name of the method to 'eating' so as not to be confused with the 'eat' method:
1
2
3
4
5
6
7
8
9
10
public boolean eating(Class clss)
{
    Actor actor = getOneObjectAtOffset(0, 0, clss);
    if (actor != null)
    {
        getWorld().removeObject(actor);
        return true;
    }
    return false;
}
Using this method, you can then do this:
1
2
3
4
if (eating(Enemy1.class) || eating(Enemy2.class) || eating(Enemy3.class) || eating(Enemy4.class))
{
    getWorld().removeObject(this);
}
Once an enemy object is eaten the end result of the condition will always be true and, therefore, the following conditions are ignored and the block of code is executed.
gnvrajareddy gnvrajareddy

2014/1/23

#
Open the code editor of the enemy class and write the following in the act method. Actor a=getOneObjectAtoffset(0,0,arrow.class) if(a!=null) getWorld().removeObject(this);
theyoshiguyREAL theyoshiguyREAL

2016/10/22

#
whats wrong with my code, im trying to make my alligator eat a turtle when the alligator runs over the turtle. this is the alligators code import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class alligator here. * * @author (your name) * @version (a version number or a date) */ public class alligator extends Actor { /** * Act - do whatever the alligator wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if(Greenfoot.isKeyDown("right")) move(3); turn(90); if(Greenfoot.isKeyDown("down")) move(3); turn(90); if(Greenfoot.isKeyDown("left")) move(3); turn(90); if(Greenfoot.isKeyDown("up")) move(3); turn(90); } actor turtle; turtle = getOneObjectAtOffset(0, 0, turtle.class); if (turtle != null) { World world; world = get world(); world.removeobject(turtle } }
danpost danpost

2016/10/22

#
@theyoshiguyREAL, you should not reopen old discussion threads like this; but, start a new one dealing with your issue. However, the lines starting with:
1
actor turtle;
to the second to last line posted are not within a method. Try moving the line above it, the '}', to below that set of code (so that you have three consecutive closing brackets at the end and said code is within the act methodd.
You need to login to post a reply.