Lua / Corona SDK / Loop Spawning / Positional Difference / Framerate独立动画

时间:2016-12-08 18:27:39

标签: loops timer lua position corona

因此,在构建使用Corona SDK的移动游戏时,我最近遇到了一个问题,我无法解决,这是在循环中生成显示对象时的位置差异。我得到了一些帮助,发现这必须与帧速率独立动画有关。但是我现在面对这个:

虽然我在这里使用帧速率独立动画,但这也会产生同样的问题。通过增加循环的速度来强调这一点,如下面的代码所示。你对此有何看法?

local loopSpeed =  306
local loopTimerSpeed = 1000
local gapTable = {}
local gapLoopTimer
local frameTime
local gap

--enterFrame for time only

    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

--loop speed function

local function setLoopSpeed(factor)

    loopSpeed = loopSpeed * factor
    loopTimerSpeed = loopTimerSpeed / factor

end

--set the loop speed

    setLoopSpeed(3)

--loop to create gaps

local function createGap()

    gap = display.newRect(1, 1, 308, 442)
    gap.time = system.getTimer()
    gap.anchorX = 1
    gap.anchorY = 0

    --animation

        gap.rate = loopSpeed
        gap.enterFrame = enterFrame
        Runtime:addEventListener("enterFrame", gap)

    --fill table for cleaning up

        table.insert(gapTable, gap)

    --cleaning up

        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

)

1 个答案:

答案 0 :(得分:0)

如果您可以设置rects之间的差距,请尝试使用以下代码

local gapTable = {}
local gapWidth = 50
local runtime = 0
local speed = 20
local gap

local function getDeltaTime()
    local temp = system.getTimer()  -- Get current game time in ms
    local dt = (temp-runtime) / (1000/display.fps)  -- 60 fps or 30 fps as base
    runtime = temp  -- Store game time
    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)

    --cleaning up
    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) 

希望这会有所帮助:)