Events 表对象中的电晕触摸事件?

Events 表对象中的电晕触摸事件?,events,lua,listener,coronasdk,lua-table,Events,Lua,Listener,Coronasdk,Lua Table,我试图在一个项目中使用它,但我不知道如何将触摸事件侦听器放置到旋转木马中的每个图标/对象,如果有人能快速回答如何做到这一点,我将不胜感激 local NUM_ITEMS=20; local radiusX= 200; local radiusY= 40; local centerX = 240; local centerY = 160; local speed = 0.05; local perspective =

我试图在一个项目中使用它,但我不知道如何将触摸事件侦听器放置到旋转木马中的每个图标/对象,如果有人能快速回答如何做到这一点,我将不胜感激

    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)
localnum\u ITEMS=20;
局部半径x=200;
局部半径y=40;
本地centerX=240;
局部中心y=160;
当地速度=0.05;
局部视角=3;
本地转盘=display.newGroup()
本地图标={}
本地函数zSort(myTable,myGroup)
table.sort(myTable,
功能(a、b)
返回a.depth
我无法确切理解您真正需要什么。但是,如果您想将单独触摸添加到本地组中的所有相同图标,则可以将图标添加为图标数组,并为每个图标添加
特定标记,并提供单独触摸,如下所示:

 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

如果要将所有组元素标识为相同并进行触摸,则可以对所有图标使用
相同标记
/give
用户数据
,并对所有组元素进行相同的触摸操作(如下所示)

 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