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

2012/5/14

How do I make an object delete itself after a certain amount of time?

steved steved

2012/5/14

#
I'm making a game and I need to make one of my objects delete itself after a certain amount of time (the same one that i needed to home in on another object). I want it to spawn then do what it does for a certain amount of acts then delete itself. Is there a method that does that for an actor or do I have to do it in the world?
Michionlion Michionlion

2012/5/14

#
just use the getWorld() method and then use the dot operator to call removeObject(Actor obj).
danpost danpost

2012/5/14

#
Not a problem, add an instance integer variable to count act cycles; perform a check to see if it reached a specified limit; if so, 'getWorld().removeObject(this);'. If you code this statement directly in the act method, do one of the following: 1) immediately follow it with a 'return;' statement; or, 2) make sure it is the last statement in the method; If you remove the object in a method that the act method calls: 1) still do one of the previous two actions, but in the called method; and, 2) after the call to that method in the act method, add the line 'if (getWorld() == null) return;' Doing this will ensure that you do not get that nasty ol' 'NullPointerException' error message.
steved steved

2012/5/14

#
I'm still pretty new to java so I'm still not very good at any of this stuff. I put it in the main act method and i get an error saying "cannot find symbol - variable Boss" (the classes name is Boss). I'm not sure how to define it so I just used the classes name. I tried 'Boss boss;' and then making it 'getWorld().removeObject(boss)' instead if '(Boss)'.
danpost danpost

2012/5/14

#
Did you try: 'getWorld().removeObject(this);'?
steved steved

2012/5/14

#
I see, I misunderstood. I thought you meant the name of the object by 'this' i got it working now. Thanks again!
You need to login to post a reply.