Corona - 在表格对象中使用触摸事件?
2013-3-4 6:6:21
收藏:0
阅读:100
评论:1
我正在尝试在一个项目中使用这个代码,但我不知道如何给转盘中的每个图标/对象添加触摸事件监听器,如果有人能提供一种快速的解决方法,我将不胜感激。
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)
点赞
评论区的留言会收到邮件通知哦~
推荐文章
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?
- addEventListener 返回 nil Lua
- Lua中获取用户配置主目录的跨平台方法
我无法确定你具体需要的是什么。但是,如果你想为 localGroup 中的所有相同图标添加个性化标记,那么你可以将它们添加为图标数组,并给每个图标添加
specific tag,然后为它们添加个性化的响应方法,如下所示: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] 可以用来识别并设置被点击图标的属性 --]] end for i=1,10 do icons[i]:addEventListener("touch",touchIt) end或者
如果你想将所有分组元素统一标记并触发响应,那么可以为所有图标赋予相同的标记或者添加
userdata标记,以及为所有分组元素添加相同的触摸响应操作(如下所示)。local icons = {} for i=1,10 do icons[i] = display.newImage(i..".png") icons[i].tag = 1 --[[你也可以使用 icons[i].userdata = "icons" (或任何字符串)--]] end local function touchIt(e) print(e.target.tag) -- 对于所有图标,它的值都是1 --[[ icons[e.target.tag] 可以用于识别 是否属于 'icons' 分组 --]] --[[ 或可使用 userdata,如下 --]] print(e.target.userdata) --[[输出是一个字符串, 用于表示分组/元素标识--]] end for i=1,10 do icons[i]:addEventListener("touch",touchIt) end