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

2017/5/1

Actor disappear upon hitting world edge error?

wowi132 wowi132

2017/5/1

#
I am making a bullet, that needs to disappear upon hitting the world edge. The code will be linked below - when i execute it, the bullet disappears upon hitting both X-axis world edges, as well as the bottom Y-axis world edge. But it just gets stuck upon hitting the top Y-axis world edge. Can someone help me why this is happening, and how i make it disappear upon hitting the top Y-axis wolrd edge aswell? thanks
public void WorldEdge()
     {
     if (atWorldEdge())
      {
          getWorld().removeObject(this);
    }
}
public boolean atWorldEdge()
{
    if(getX() < 10 || getX() > getWorld().getWidth()-10)
    {
        return true;
    }
    if(getX() < 10 || getY() > getWorld().getHeight()-10)
    {
       return true;
    }
    else 
    {
        return false;
    }
danpost danpost

2017/5/1

#
You have only three different conditions shown in the 'atWorldEdge' method (the first condition in both line 10 and 14 are the same).
wowi132 wowi132

2017/5/1

#
Thank you very much, it is solved now :)
You need to login to post a reply.