Button 显示图像按钮以复制应用程序图标-Corona SDK

Button 显示图像按钮以复制应用程序图标-Corona SDK,button,user-interface,lua,coronasdk,Button,User Interface,Lua,Coronasdk,我需要显示图像,以便它们模仿各种“主页”上iOS应用程序图标的布局。我编写了代码来显示各种图像;但是,我不知道如何返回与单击事件中的每个单独图像对应的值 以下是我目前的代码: plates = { "fat-burning-foods.jpg", "fresh_food.jpg", "fat-burning-foods.jpg", "star.png", "fat-burning-foods.jpg", "star.png", "fat-burning-foods.jpg", "fres

我需要显示图像,以便它们模仿各种“主页”上iOS应用程序图标的布局。我编写了代码来显示各种图像;但是,我不知道如何返回与单击事件中的每个单独图像对应的值

以下是我目前的代码:

    plates = {
"fat-burning-foods.jpg",
"fresh_food.jpg",
"fat-burning-foods.jpg",
"star.png",
"fat-burning-foods.jpg",
"star.png",
"fat-burning-foods.jpg",
"fresh_food.jpg",
"fat-burning-foods.jpg",
"star.png",
"fat-burning-foods.jpg",
"fresh_food.jpg",
"fat-burning-foods.jpg",
"star.png"
}


-- The PlateId will have corosponding indexes with the plates array. It will be used to query plate information 

    plateId = {1,2,3,4}
plateIdRef = {}
index = 1
platesIsh = {1,2,3,4,5,6,7,8}
local anX = 20
local anY = 120
for i=1, #plates do

 local bufferY = 20
 if index == 4 then
bufferY = 110
anX = 20
elseif index > 4 then
    bufferY = 110

    end
if index == 7 then
    bufferY = 200
    anX = 20

elseif index > 7 then
    bufferY = 200

    if index == 10 then
            bufferY = 290
            anX = 20

        elseif index > 10 then
            bufferY = 290



end

    end


    local dummyVar = math.random()
dummyVar = display.newImageRect(plates[index],80, 80)
sceneGroup:insert(dummyVar)
dummyVar.x = anX + 30
dummyVar.y = anY + bufferY
table.insert(plateIdRef, index)
function dummyVar:touch( event )
if event.phase == "began" then
local alert = native.showAlert( "Corona", event.target, { "OK", "Learn More" } )
    --print( "You touched "..dummyVar)
    return true
end
    end

    dummyVar:addEventListener( "touch", dummyVar )
anX = anX + 110
index = index + 1

end

有什么想法吗?

好的,举个例子看看它是如何工作的:

local plates = {}

local function plateTouch( self, event )
    local phase = event.phase
    if phase == "ended" then
        print( "You touched ".. self.id)
    end
    return true
end

for i=1, 3 do
    plates[i] = display.newImageRect("test.png",80, 80)
    plates[i].id = "plate " .. i
    plates[i].x = 90 * i
    plates[i].y = 90
    plates[i].touch = plateTouch
    plates[i]:addEventListener( "touch", plates[i] )
end
创建每个板时,您可以定义一个
id
属性,以了解按下的按钮,还可以添加所需的任何其他属性

您可以看到,也可以看到带有表侦听器的示例

另一种方法可以是面向对象的解决方案,您可以定义一个类来创建新的板材,但可能首先您需要理解这个示例