Reputation: 3652
I'm actually quite familiar with handling touch events in Android. I however have now hard-coded everything. I mean alot of comparing like if (e.getX()<0.1*v.getX)
, for both game-objects and for the hud.
Now i'm actually looking for proper handling touch-events. I also tried storing both the x and y values onTouch, however this gets messy too, especially when I also have to test for ACTION_DOWN or ACTION_MOVE etc...
so anyone knows the best 'architecture or something for this?
Upvotes: 1
Views: 1855
Reputation: 5924
Your game probably has a main loop on a different thread since in Android you're supposed to stay off the event thread. If this is the case, I would set up a separate InputBuffer
class to receive and store incoming touch events. When I have done this, InputBuffer
was the OnTouchListener
for the SurfaceView
.
When there is an incoming event, InputBuffer
just makes a copy of it and returns. This keeps the event thread free and helps prevent ANRs.
Then in the main loop, game objects who need to know about user input have access to InputBuffer
so they can get info about the stored event. This moves your input processing code out of the view and into the game objects where it makes more sense.
Depending on the speed of your game, you may also want to put in a block so that incoming events don't update the stored copy while the game objects are updating. Instead, the stored copy is updated after the main loop updates, and this way all game objects are working with the same event.
Upvotes: 2