Hi, I’m a new user and I am still learning how to code.
My question is : how would you handle touch events on specific objects?
From what I could understand in the examples provided, the touch event is offen handled the following way (pseudo code) :
if user touches the screen and the coordinates of the touch event are whithin a certain area, then object was touched.
I would like to know if I can create listeners so that :
if object is touched then do something.
The difference is that Codea would handle the touch event for me, I would put a few buttons on the screen (gamepad) and I wouldn’t have to check touch event coordinates each time.
Can you tell me how to achieve that in Codea?
Thank you!
I do this by having a meta-touch handler that gathers all the touches. It then queries each object that it knows about to see if they were the one touched (only at the start of the touch). Then each object defines a callback function and the touch handler calls that function with all of the touches associated to that object.
In Visual Studio language implementations, Microsoft often hides the event loop, so that all you see are the responses to events – which can be handy. However, I think I prefer the clear execution you get in Codea. If nothing else you’re never in doubt about order of events.
Oh, and Andrew’s touch handler goes a long way in neat ending your code.