我需要显示图像,以便它们模仿各种“主页”页面上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有什么想法吗?
发布于 2014-06-20 07:18:19
好的,看看它是如何工作的一个例子:
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属性以知道按下了哪个按钮,还可以添加任何其他需要的属性。
您可以看到用于触摸事件的API文档,并看到带有表侦听器的示例。
其他方法可以是面向对象的解决方案,您可以定义一个类来创建新的板块,但首先您可能需要理解这个示例。
https://stackoverflow.com/questions/24313747
复制相似问题