Lua / Corona SDK / 循环生成 / 位置误差 / 帧速率独立动画
2016-12-8 18:27:39
收藏:0
阅读:73
评论:1
在使用 Corona SDK 开发移动游戏时,我最近遇到了一个问题,即在循环生成显示对象时遇到的位置误差问题。我得到了一些帮助,并发现这必须与帧速率独立动画有关。但现在我面临着这个问题:
尽管我在这里使用了帧速率独立动画,但这也会产生同样的问题。增加循环的速度(代码如下所示)会进一步强调这个问题。你对此有什么想法?
local loopSpeed = 306
local loopTimerSpeed = 1000
local gapTable = {}
local gapLoopTimer
local frameTime
local gap
--只用于时间的enterFrame
local function frameTime(event)
frameTime = system.getTimer()
end
--enterFrame
local function enterFrame(self, event)
local deltaTime = frameTime - self.time
print(deltaTime/1000)
self.time = frameTime
local speed = self.rate * deltaTime / 1000
self:translate(speed, 0)
end
--循环速度函数
local function setLoopSpeed(factor)
loopSpeed = loopSpeed * factor
loopTimerSpeed = loopTimerSpeed / factor
end
--设置循环速度
setLoopSpeed(3)
--循环生成间隔
local function createGap()
gap = display.newRect(1, 1, 308, 442)
gap.time = system.getTimer()
gap.anchorX = 1
gap.anchorY = 0
--动画
gap.rate = loopSpeed
gap.enterFrame = enterFrame
Runtime:addEventListener("enterFrame", gap)
--用于清理的表
table.insert(gapTable, gap)
--清理
for i = #gapTable, 1, -1 do
local thisGap = gapTable[i]
if thisGap.x > display.contentWidth + 500 then
display.remove(thisGap)
table.remove(gapTable, i)
Runtime:removeEventListener("enterFrame", thisGap)
end
thisGap = nil
end
end
Runtime:addEventListener("enterFrame", frameTime)
gapLoopTimer = timer.performWithDelay(
loopTimerSpeed,
createGap,
0
)
点赞
评论区的留言会收到邮件通知哦~
推荐文章
- Lua 虚拟机加密load(string.dump(function)) 后执行失败问题如何解决
- 我想创建一个 Nginx 规则,禁止访问
- 如何将两个不同的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 代码?

如果你可以设置矩形之间的间隔大小,请尝试使用下面的代码:
local gapTable = {} local gapWidth = 50 local runtime = 0 local speed = 20 local gap local function getDeltaTime() local temp = system.getTimer() -- 获取当前游戏时间(毫秒) local dt = (temp-runtime) / (1000/display.fps) -- 以 60 fps 或 30 fps 为基础 runtime = temp -- 存储游戏时间 return dt end local function enterFrame() local dt = getDeltaTime() for i=1, #gapTable do gapTable[i]:translate(speed * dt, 0) end end local function createGap() local startX = 0 if #gapTable > 0 then startX = gapTable[#gapTable].x - gapWidth - gapTable[#gapTable].width end gap = display.newRect(startX, 1, 308, 442) gap.anchorX, gap.anchorY = 1, 0 table.insert(gapTable, gap) -- 清理 for i=#gapTable, 1, -1 do if gapTable[i].x > display.contentWidth + 500 then local rect = table.remove(gapTable, i) if rect ~= nil then display.remove(rect) rect = nil end end end end timer.performWithDelay(100, createGap, 0) Runtime:addEventListener("enterFrame", enterFrame)希望可以帮到你:)