电晕 - 触摸事件在表格对象中?

时间:2013-03-02 08:20:40

标签: events lua listener corona lua-table

我正在尝试在项目中使用它,但我无法弄清楚如何将旋转事件监听器放置到旋转木马中的每个图标/对象,如果有人可以快速回答如何做到这一点我'我很欣赏它。

    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)

1 个答案:

答案 0 :(得分:1)

我无法完全理解你真正需要的东西。但是,如果您想为localGroup中的所有相同图标添加单独的触摸,那么您可以将图标添加为图标数组,并为每个图标添加specific tag,并且可以进行单独触摸,如下所示:

 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

如果您想要将所有组元素标识为相同并进行触摸,那么您可以对所有图标使用same tag /给userdata,并且可以对所有组元素进行相同的触摸操作(如下所示) )。

 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