Lua / Corona SDK / 循环生成 / 位置误差 / 帧速率独立动画

在使用 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

)
点赞
用户7026995
用户7026995

如果你可以设置矩形之间的间隔大小,请尝试使用下面的代码:

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)

希望可以帮到你:)

2016-12-09 11:12:26