Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Add touch event to multiple objects — Gideros Forum

Add touch event to multiple objects

MastoschMastosch Member
edited February 2013 in General questions
Hy,
I have many rectangles(objects) created in table.

local rectTable = {}
for i = 1, 5, 1 do
rectTable[i] = {}
for j = 1, 5, 1 do
rectTable[i][j] = Shape.new()
rectTable[i][j]:setLineStyle(3, 0x000000)
...

How can i add touch event to each object, so i could manage them individually.
Tried couple things, nothing seems to work.

Likes: Mastosch

+1 -1 (+1 / -0 )Share on Facebook

Comments

  • ar2rsawseenar2rsawseen Maintainer
    edited February 2013 Accepted Answer
    --handling touches start
    local handleTouches = function(self, e)
        --self references to rectangle
        --e event object with x and y cooridnates
        if self:hitTestPoint(e.touch.x, e.touch.y) then --if object was touched
            --your code goes here
        end
    end
    for j = 1, 5, 1 do
        rectTable[i][j] = Shape.new()
        rectTable[i][j]:setLineStyle(3, 0x000000)
        --addEventListener(event type, function to handle event, object reference to pass to function)
        rectTable[i][j]:addEventListener(Event.TOUCHES_BEGIN, handleTouches, rectTable[i][j])
        stage:addChild(rectTable[i][j])
    end
    Hope this helps ;)
Sign In or Register to comment.