I’m trying to write a simple ball game, and there’s several turns (ie., ball lives). The ball “dies” when it passes the bottom border of the screen. What I have so far works, but doesn’t seem to be the proper way to do things:
if (ball.getY() > bottomOfScreen) {
ball.die();
remove(ball);
}
The die() method basically fades the ball’s colour slowly (dark_gray -> pause(50) -> light_gray -> pause(50)), but doesn’t actually do anything useful.
The remove(), obviously, gets rid of the ball from the screen, which is what I want. It makes sense to me for this remove() to be a part of Ball’s die() method, as opposed to it being a separate method call in the main program — but I’m not sure how to go about this?
Can an object delete itself? And, if it can, is object suicide better than object murder, from a philosophical/methodological point of view?
Thanks!
The object can remove itself given it has some sort of reference to the view rendering mechanism. Your sample doesn’t give enough information so I’ll exemplify one way to do it:
Neither suicide nor murder is better or worse. It depends on your design and requirements.
In this sample though, murder might be preferable since this way the
Ballobject doesn’t need to know in which context it’s being used.