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

2015/3/18

How to remove my arrow?

GuilhermeGomes GuilhermeGomes

2015/3/18

#
Hey everyone ,greenfoot newbie here. I'm making a game that consists on hunting birds with a bow and arrows. The thing is, I want the arrow to be removed when it touches the edge of the world. Here is my code.
{
    public void act()
    {
 // code that removes the arrow
        if (isAtEdge())getWorld().removeObject(this);
        else move(30);
        arrowHit(null);
    }

    public void arrowHit(Class clss)
    { 
// code that removes the bird
  
        if(isTouching(Bird.class))
        {
            removeTouching(Bird.class);
            getWorld().removeObject(this);
        }
    }    
}
when i compile and throw an arrow to the edge it gives me this 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.
If i change the if (isAtEdge())getWorld().removeObject(this); to if (isAtEdge())move(0); it works fine but the arrow gets stuck in the edge and its not want i want. Any advice?
danpost danpost

2015/3/18

#
Put curly brackets around the else part (lines 6 and 7 above):
else
{
    move(30);
    arrowHit(null);
}
BTW, 'arrowHit' does not use 'clss' anywhere within its code. You can change it to:
public void arrowHit()
and just call it with:
arrowHit();
Although its name is a bit vague -- maybe 'killBird' might be a better name, which implies that any bird hit is removed.
GuilhermeGomes GuilhermeGomes

2015/3/18

#
the full code for arrowHit is this
public void arrowHit(Class clss)
    { 
        if(isTouching(Bird.class))
        { 
            removeTouching(Bird.class);
            getWorld().removeObject(this);
        }
        else if(isTouching(Target.class))
        {
            removeTouching(Target.class);
            getWorld().removeObject(this);
        }
        else if(isTouching(Enemy.class))
        {
            removeTouching(Enemy.class);
            getWorld().removeObject(this);
        }
        else if(isTouching(ParedeH.class))
            getWorld().removeObject(this);
        else if(isTouching(ParedeV.class))
            getWorld().removeObject(this);
    }
i put Class clss because i want to hit several things in different worlds. I kinda "solved" the problem programming invisible walls (ParedeV and ParedeH - walls in portuguese :p) around the world i want.
danpost danpost

2015/3/18

#
GuilhermeGomes wrote...
thei put Class clss because i want to hit several things in different worlds.
Still, I do not see where 'clss' is used anywhere within the method. Removing it will not change what the method does.
GuilhermeGomes GuilhermeGomes

2015/3/18

#
You're right thank you! I'll upload my game as soon as i get it done
You need to login to post a reply.