I’m developing a simple game in Android and I want to detect a collision between two sprites and remove them, I have an idea on how the code would be but I don’t know where (in wich event) I would put it… The sprites are moving on the screen, so this “event” can happen at any time, and not just onTouchEvent or something like that.
I just finished to follow this tutorial, there you can find all the code. Now i’m trying to change it. Basically i’m trying to differentiate 2 types of sprites (bad and good) and if a bad sprite touches in a good sprite, the good one dies.
Actually @Nammari is right, you probably have a loop inside your engine that moves the sprites, something for engine to simulate a micro-turn : all sprites move at their speed.
Inside this loop you simply have to test collisions and remove the unnecessary sprites.
All this should happen in a class separated from the activity itself.
Nevertheless, your question also has a deeper interest if you want to tell your activity (your view) that some collision happened (for instance increment a collision counter, make the screen blink, whatever).
And well, that’s not an obvious question and actually it’s a good idea to wonder how to do this in a neat and clean way.
The best answer is that you are gonna enter in a new world : software architectrure. 😉 Yep, it’s not about “coding” anymore, but about clean ways to do it (and beauty and art but that could be a more philosphical matter actually).
In this case, What you should do is put in place an observable observer design pattern :
That’s the basic idea.
Here is a nice introductory example for all that.