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

我需要展示图片,以便它们在不同的“主页”上模仿 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"
}

-- PlateId 将与 plates 数组的索引对应。它将用于查询 Plate 信息

    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

有什么想法吗?

点赞
用户2360222
用户2360222

下面是一个例子,看看它是如何工作的:

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属性来知道按下了哪个按钮,你还可以添加任何其他你需要的属性。

你可以查看 touch 事件的 API 文档 并查看使用表侦听器 的示例。

另一种解决方案可能是面向对象的解决方案,其中你定义一个类来创建新的盘子,但是可能首先需要理解这个示例。

2014-06-20 07:18:19