Wintermuteai1
Wintermuteai1

Reputation: 21

Corona - Touch Events In table objects?

I am trying to use this in a project, but I cannot figure out how to place a touch event listener to each of the icons/objects in the carousel, If someone could provide a quick answer of how to do that I'd appreciate it.

    local NUM_ITEMS=20;  
    local radiusX= 200;  
    local radiusY= 40;  
    local centerX = 240;  
    local centerY = 160;  
    local speed = 0.05;  
    local perspective = 3;    

    local carousel = display.newGroup()
    local icons = {}

    local function zSort(myTable, myGroup)

            table.sort(myTable,  
                    function(a, b)
                        return a.depth < b.depth -- depth is your custom field
                    end
            )
            for i = 1, #myTable do
                    myGroup:insert(myTable[i].img)
            end

    end

function Icon(i)
        local this = {}
        local icon = display.newImage(carousel, "images/icon"..i..".png")
        this.angle = (i-1) * math.rad(360/NUM_ITEMS);  
        this.img = icon
        return this
end

function update(event)

        local icon
        local sin = math.sin
        local cos = math.cos

        for i=1, NUM_ITEMS, 1 do

                icon = icons[i]
                local img = icon.img

                img.x = cos(icon.angle) * radiusX + centerX
                img.y = sin(icon.angle) * radiusY + centerY


                local s = (img.y - perspective) / (centerX + radiusY - perspective)
                img.xScale = s*0.25
                img.yScale = s*0.25

                icon.angle = (icon.angle  + speed) --%math.rad(360)

                icon.depth = s

        end

        zSort(icons, carousel)

end

for i=1, NUM_ITEMS, 1 do
        local icon = Icon(i)
        icons[i] = icon 
end

function onTouch(event)
        if(event.phase=="moved") then
                speed = (event.x - centerX) / 2000;  
        end
end

Runtime:addEventListener("enterFrame",update)
Runtime:addEventListener("touch", onTouch)

Upvotes: 0

Views: 1506

Answers (1)

Krishna Raj Salim
Krishna Raj Salim

Reputation: 7390

I can't exactly understood what you really need. But if you want to add individual touch to all same icons in a localGroup, then you can add the icons as an icon array and give specific tag to each and can give individual touch, as follows:

 local icons = {}
 for i=1,10 do
   icons[i] = display.newImage(i..".png")
   icons[i].tag = i
 end

 local function touchIt(e)
   print(e.target.tag) 
   --[[ icons[e.target.tag] can be used to identify 
        and set properties to the touched icon --]]
 end
 for i=1,10 do
   icons[i]:addEventListener("touch",touchIt)
 end

OR

if you want to identify all group elements as the same and give touch, then you can use same tag/ give userdata to all icons and can give same touch action to all group elements(as follows).

 local icons = {}
 for i=1,10 do
   icons[i] = display.newImage(i..".png")
   icons[i].tag = 1 --[[ you can also use icons[i].userdata = "icons" 
                         (or any string)--]]
 end

 local function touchIt(e)
   print(e.target.tag) -- It willo be 1 for all icons
                       --[[ icons[e.target.tag] can be used to identify 
                            whether if it is under 'icons' --]]

  --[[ or use userdata as follows --]]
  print(e.target.userdata)--[[ out put is a string 
                             identifying the group/element--]]
 end
 for i=1,10 do
   icons[i]:addEventListener("touch",touchIt)
 end

Upvotes: 1

Related Questions