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

2012/1/27

Error on this script

craigv craigv

2012/1/27

#
this script is for when my ball makes contact with the pointball, the pointball will be removed, then when the ball reaches the outer limits, it removes itself. I just added the second set of script today (beginning at Actor pointball) , and when i play my game, i get an error message when the ball reaches the outer limits... something that didnt happen before i added the new code. public void act() { move(10.0); { boolean topEdge = getY() == 0; boolean leftEdge = getX() == 0; boolean rightEdge = getX() >= getWorld().getWidth() - 1; boolean bottomEdge = getY() >= getWorld().getHeight() - 1; if (topEdge || leftEdge || rightEdge || bottomEdge) getWorld().removeObject(this); } Actor pointBall; pointBall = getOneObjectAtOffset(0,0,pointBall.class); if (pointBall!=null) { World background; background = getWorld(); background.removeObject(pointBall); } } }
Builderboy2005 Builderboy2005

2012/1/27

#
I know that the problem is, but for future reference, always post the error message you encounter, as well as point out the exact line that causes the error. Without those, it can be extremely difficult to pinpoint the cause of an error. As it is though, the problem is simple. When your ball reaches the outer limit, it removes itself from the world, but the Act() cycle still needs to finish, and when you try to getOneObjectAtOffset(), your ball is no longer in the world, and so it throws an error. A solution would be to insert a return inside of the first if statement so that the act cycle ends as soon as the ball is removed from the world.
darkmist255 darkmist255

2012/1/27

#
What I do for situations such as this is instead of "removeObject.(this)" make it "removeMe = true" Then at the LAST line of act(), put a method called "checkRemove()" which is just
If(removeMe)
{
    getWorld().removeObject(this);
}
Its a bit more complicated but makes things easier the more methods you have.
You need to login to post a reply.