Lua(Corona SDK)中的面向对象编程和事件监听器
2014-1-9 19:4:6
收藏:0
阅读:140
评论:1
我的Corona SDK第一步和第一个问题。试图制作两个可以移动的方块,按照此OOP教程,但这不起作用。我成功地创建了两个盒子,但只有一个是可移动的。当我尝试移动另一个时,它不会移动,但第一个会移动。我想可能是作用域的问题,但无法确定确切的位置。谢谢任何帮助。
tile.lua源代码:
模块(…,包.看到全部)
function new(initX,initY)
显示阶段宽度=显示.stageWidth
显示阶段高度=显示.stageHeight
正方形大小 =(scrnWidth * 0.9)/ 4
本地瓷砖 =显示.newRect(initY,initY,squareSize,squareSize)
function move(direction)
如果方向==“左”则
过渡.moveTo(tile,{x = tile.x + squareSize,y = tile.y,时间 = 200)
elseif direction == “right” then
过渡.moveTo(tile, {x = tile.x - squareSize,y = tile.y,时间 = 200})
elseif 方向==“上”则
过渡.moveTo(tile,{x = tile.x,y = tile.y-squareSize,时间 = 200)
elseif 方向==“下”则
过渡.moveTo(tile,{x = tile.x,y = tile.y + squareSize,时间 = 200)
end
end
功能瓷砖:触摸事件(事件)
如果事件.阶段==“开始”则
显示.getCurrentStage():setFocus(tile )
开始X =事件.X
beginY =事件.Y
end
如果事件.阶段==“结束”然后
结束X =事件.X
endY =事件.Y
显示.getCurrentStage():setFocus(nil)
检查滑动方向();
end
end
函数checkSwipeDirection()
xDistance = math.abs(endX - beginX) -数学。abs将返回给定值的绝对值或非负值。
yDistance = math.abs(endY - beginY)
如果XDistance> YDistance then
如果beginX> endX then
move(“right”,瓷砖)
else
move(“left”,瓷砖)
end
else
如果beginY> endY then
move(“up”,瓷砖)
else
move(“down”,瓷砖)
end
end
end
tile:addEventListener(“touch”,tile)
返回瓷砖
end
我在main.lua中使用以下代码创建对象:
local (require(“tile”)
local tile1 = tileConst.new(100,100)
local tile2 = tileConst.new(200,200)
点赞
评论区的留言会收到邮件通知哦~
推荐文章
- 如何将两个不同的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中获取用户配置主目录的跨平台方法
在你的
tile.lua场景中,在module (..., package.seeall)下方声明一个变量来保存触摸的精灵:local touchedSprite -- 我的临时精灵变量然后在以下函数中将目标精灵分配给上述变量:
function tile:touch(event) touchedSprite = event.target -- 分配目标到变量 ... end现在按照如下方式更改
move函数:function move(direction) if direction == "left" then transition.to(touchedSprite, {x = touchedSprite.x+squareSize,y = touchedSprite.y,time = 200}) elseif direction == "right" then transition.to(touchedSprite, {x = touchedSprite.x-squareSize,y = touchedSprite.y,time = 200}) elseif direction == "up" then transition.to(touchedSprite, {x = touchedSprite.x,y = touchedSprite.y-squareSize,time = 200}) elseif direction == "down" then transition.to(touchedSprite, {x = touchedSprite.x,y = touchedSprite.y+squareSize,time = 200}) end end请注意,我已将
transition.moveTo更改为transition.to,并将你的transition tile更改为touchedSprite(target).继续编码.................. :)