Difference between revisions of "Article Tutorials/Object IDs"

From GiderosMobile
 
Line 1: Line 1:
 
__TOC__
 
__TOC__
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.<br><br>
+
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.
 
 
 
<syntaxhighlight lang="lua">
 
<syntaxhighlight lang="lua">
 
local yinyang1 = Sprite.new()
 
local yinyang1 = Sprite.new()
Line 30: Line 29:
 
</syntaxhighlight><br>
 
</syntaxhighlight><br>
  
Here’s the output for comparison:<br>
+
Here’s the output for comparison:
  
 
[[File:Jason-Oakley-Object-IDs-Object-IDs-1.png|thumb|center]]<br>
 
[[File:Jason-Oakley-Object-IDs-Object-IDs-1.png|thumb|center]]<br>
  
You can download the yinyang image from here: [[:File:Yinyang.png]]<br><br>
+
You can download the yinyang image from here: [[:File:Yinyang.png]]
 +
 
  
 
  '''Note: This tutorial was written by [http://bluebilby.com/author/waulokadmin/ Jason Oakley] and was originally available Here: http://bluebilby.com/2013/04/30/gideros-mobile-tutorial-object-ids/'''
 
  '''Note: This tutorial was written by [http://bluebilby.com/author/waulokadmin/ Jason Oakley] and was originally available Here: http://bluebilby.com/2013/04/30/gideros-mobile-tutorial-object-ids/'''

Latest revision as of 10:37, 26 August 2024

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:

Jason-Oakley-Object-IDs-Object-IDs-1.png


You can download the yinyang image from here: File:Yinyang.png


Note: This tutorial was written by Jason Oakley and was originally available Here: http://bluebilby.com/2013/04/30/gideros-mobile-tutorial-object-ids/


Written Tutorials