科罗纳-向表/数组或表中的项目添加侦听器
2013-7-4 5:7:33
收藏:0
阅读:250
评论:2
我正在尝试为从表格中随机选择的图像添加一个触摸事件,使用以下代码:
easyGame2 = function()
catTable = { 'catwhite', 'catblue', 'catred', 'catyellow', 'catgreen', 'catorange', 'catpink', 'catpurple'}
randomCat = catTable[math.random(#catTable)]
cat = display.newImageRect(randomCat..'.png', 50, 50)
cat.x = math.random(20, display.contentWidth-20)
cat.y = 0
cat.myName = randomCat
transition.to( cat, { rotation = cat.rotation-360, time=2000, y=500,
onComplete=easyGame2})
end
easyGame2()
cat:addEventListener('touch', tapCat )
tapCat = function(event)
if event.phase == 'began' then
score = score + 2
display.remove(cat)
cat = nil
end
end
第一次触摸它有效,然后就好像不再有监听器一样。我如何使它适用于[math.random(#catTable)]中选择的每个图像
我也尝试了这段代码:
local catTable = {}
cat1 = {}
cat1.imgpath = 'images/catwhite.png'
table.insert(catTable, cat1)
--cat1:addEventListener('touch', tapCat ) --causes errors
cat2 = {}
cat2.imgpath = 'images/catblue.png'
table.insert(catTable, cat2)
cat3 = {}
cat3.imgpath = 'images/catred.png'
table.insert(catTable, cat3)
cat4 = {}
cat4.imgpath = 'images/catyellow.png'
table.insert(catTable, cat4)
cat5 = {}
cat5.imgpath = 'images/catgreen.png'
table.insert(catTable, cat5)
cat6 = {}
cat6.imgpath = 'images/catorange.png'
table.insert(catTable, cat6)
cat7 = {}
cat7.imgpath = 'images/catpink.png'
table.insert(catTable, cat7)
cat8 = {}
cat8.imgpath = 'images/catpurple.png'
table.insert(catTable, cat8)
easyGame2 = function()
randomCat = catTable[math.random(#catTable)]
cat = display.newImageRect(randomCat.imgpath, 50, 50)
cat.x = math.random(20, display.contentWidth-20)
cat.y = 0
cat.myName = randomCat
transition.to( cat, { rotation = cat.rotation-360, time=2000, y=450, onComplete=function(self)self.parent:remove(self);self=nil;end})
for i = 1, #catTable do
catTable[i]:addEventListener('touch', tapCat)--causes error
end
end
它会给出错误“181:尝试调用方法'addEventListener'(空值)”
点赞
用户2509364
没关系,我成功解决了,只需进行以下编辑:
randomCat = catTable[math.random(#catTable)]
cat = display.newImageRect(randomCat..'.png', 50, 50)
transition.to( cat, { rotation = cat.rotation-360, time=2000, y=500,onComplete=easyGame2})
变成
randomCat = math.random(#catTable)
cat = display.newImageRect(catTable[randomCat], 50, 50)
cat.type = randomCat
cat.trans = transition.to( cat, { rotation = cat.rotation-360, time=2000, y=550, onComplete=function(self)self.parent:remove(self);self=nil;end})
cat:addEventListener('touch', tapCat )
return cat
end
tapCat = function(event)
catTouch = event.target
transition.cancel (event.target.trans)
if catTouch.type == catnormal then
score = score + 5
else
score = score + 10
end
display.remove(catTouch)
cat = nil
return true
end
感谢Vovahost提供的答案。
2013-06-22 16:48:19
评论区的留言会收到邮件通知哦~
推荐文章
- 如何将两个不同的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中获取用户配置主目录的跨平台方法
在监听器上方放置 tapcat 函数,像这样:
tapCat = function(event) if event.phase == 'began' then score = score + 2 display.remove(cat) cat = nil end end cat:addEventListener('touch', tapCat )