The snippet below shows how to react to both click and touch events in Gideros Mobile.
Notice that touch events are meant for mobile devices and allow us to handle single and multi touches.
On the other hand, mouse events are meant for desktops but you can use them to handle single touchs in mobiles too.
-- A simple function to handle touch events.
local function touched(obj, event)
if obj:hitTestPoint(event.touch.x, event.touch.y) then
local x = event.touch.x
local y = event.touch.y
print('touched: X = ' .. x .. ' Y = ' .. y)
-- A simple function to handle mouse events.
local function clicked(obj, event)
local x = event.x
local y = event.y
print('clicked: X = ' .. x .. ' Y = ' .. y)
-- Create a Bitmap object. Note that we are passing to its constructor
-- a Texture object and that it receives the full path of the image as
-- we see under Files folder
local einstein = Bitmap.new(Texture.new('ra_einstein.png'))
-- put it at position x = 80, y = 0.
-- You need to listen to events of your interest. You can pick just
-- what you need in your game. I'm adding both types of events for
-- the sake of demonstration.
einstein:addEventListener(Event.TOUCHES_END, touched, einstein)
einstein:addEventListener(Event.MOUSE_UP, clicked, einstein)
-- add it to stage. It os a global variable of type Stage. You never instantiate it yourself.
In picture below you will see in Output view the text we print when image is clicked.
The complete project can be downloaded from Plicatibu's github repository.
Read the comments in the main.lua file to see instructions on how to set properties of both project and Gideros player.
Previous post in this series of snippets: How to Load An Image in Gideros
Next post in this series of snippets: How to Move An Image in Gideros