Gideros Mobile tutorial – Object IDs

In your game you will have many objects and you will want to know which one was touched or clicked. From here on, I will be giving example code for […]

In your game you will have many objects and you will want to know which one was touched or clicked. From here on, I will be giving example code for Touch events, but if you are writing for the desktop, you can make the changes mentioned previously to detect Mouse Input.

local yinyang1 = Sprite.new()
local yinyang2 = Sprite.new()
local yinyangimg1 = Bitmap.new(Texture.new("images/yinyang.png"))
local yinyangimg2 = Bitmap.new(Texture.new("images/yinyang.png"))
yinyang1.id = 1
yinyang2.id = 2
yinyang1:addChild(yinyangimg1)
yinyang2:addChild(yinyangimg2)
local function imagetouch(sprite, event)
if sprite:hitTestPoint(event.touch.x, event.touch.y) then
print("touched: ID=" .. sprite.id)
end
end
yinyang1:setPosition(50,50)
yinyang2:setPosition(200,200)
yinyang1:addEventListener(Event.TOUCHES_END, imagetouch, yinyang1)
yinyang2:addEventListener(Event.TOUCHES_END, imagetouch, yinyang2)
stage:addChild(yinyang1)
stage:addChild(yinyang2)

Here’s the output for comparison:

Object IDs

 

 

 

 

 

 

 

 

 

 

Tags:
Jason Oakley

About Jason Oakley