首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >显示图像按钮以复制应用程序图标- Corona

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

Stack Overflow用户
提问于 2014-06-19 18:22:56
回答 1查看 88关注 0票数 0

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

到目前为止,这是我的代码:

代码语言:javascript
复制
    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

有什么想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-06-20 07:18:19

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

代码语言:javascript
复制
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文档,并看到带有表侦听器的示例。

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24313747

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档