java - Can an object remove itself? How? -


i'm trying write simple ball game, , there's several turns (ie., ball lives). ball "dies" when passes bottom border of screen. have far works, doesn't seem proper way things:

if (ball.gety() > bottomofscreen) {   ball.die();   remove(ball); } 

the die() method fades ball's colour (dark_gray -> pause(50) -> light_gray -> pause(50)), doesn't useful.

the remove(), obviously, gets rid of ball screen, want. makes sense me remove() part of ball's die() method, opposed being separate method call in main program -- i'm not sure how go this?

can object delete itself? and, if can, object suicide better object murder, philosophical/methodological point of view?

thanks!

the object can remove given has sort of reference view rendering mechanism. sample doesn't give enough information i'll exemplify 1 way it:

public class ball {     private viewrenderer view;      public void remove() {        view.remove(this);     } } 

neither suicide nor murder better or worse. depends on design , requirements.

in sample though, murder might preferable since way ball object doesn't need know in context it's being used.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -