Corona SDK随机物理景观

我正在制作一个游戏,使用一个角色沿着由不同的“部分”组成的轨道行驶,并随机选择这些部分。它使用物理引擎,大多数物体都有许多顶点,并使用Physics Editor创建。我的实现不是很好,因为每次需要新的部分时,我都要重新创建所有部分。这会导致帧速率跳过,因为它必须在“时间关键”的游戏过程中创建这些大型物理体。每个部分的长度为2000像素,到目前为止我有7个部分。有人能告诉我一个更好的实现方法吗?谢谢!

这是我用于选择和创建随机部分的功能:

local secNeeded=false

 local secNum=2

 local totallength=-800

 local function newSec()
 if secNeeded==true then
 local levNum=math.random( 1, 7 )
if secNum==1 then
        display.remove( group1 )
        group1 = nil
        group1=display.newGroup()
        game:insert(group1)
end
if secNum==2 then
        display.remove( group2 )
        group2 = nil
        group2=display.newGroup()
        game:insert(group2)
end
if secNum==3 then
        display.remove( group3 )
        group3 = nil
        group3=display.newGroup()
        game:insert(group3)
end

    if levNum == 1 then
        createRamp()
    end

    if levNum == 2 then
        createdoubleRamp()
    end

    if levNum == 3 then
        createHill()
    end

    if levNum == 4 then
        createRampHill()
    end
    if levNum == 5 then
        createUpHill()
    end
    if levNum == 6 then
        createDownHill()
    end
    if levNum == 7 then
        createTunnel()
    end
 end
 secNum=secNum+1
if secNum==4 then
    secNum=1
end

 end

 local function wheelMid(event)
 --print(totallength)
 --print(wheel.x)
 if wheel.x>totallength then
 secNeeded=true
 newSec()
 end
 end

 Runtime:addEventListener( "enterFrame", wheelMid)

这是一个create函数的示例

        function createHill()

            local mega=display.newGroup()
            local guide = display.newRect(0,0, 2000, 50)
            guide.x=totallength+2000
            guide.y=totalheight
            guide.alpha=0
            mega:insert(guide)

            local ground = display.newImageRect("ground.png", 2000, 600)
            ground.x=guide.x
            ground.y=guide.y+200
            mega:insert(ground)
            physics.addBody(ground, "static", { friction=0.5 })

            local hill= display.newImageRect("hill2.png", 1400, 900)
            hill.x=guide.x+300
            hill.y=guide.y-534
            mega:insert(hill)
            physics.addBody( hill, "static", physicsData:get("hill2") )

                if secNum==1 then
            group1:insert(mega)
            end
            if secNum==2 then
            group2:insert(mega)
            end
            if secNum==3 then
            group3:insert(mega)
            end

            totallength=guide.x
            secNeeded=false

    end

这些组是为了同时出现3个部分。有什么更好的方法来实现这个问题,消除帧跳跃吗?如果有人能帮助我或指点我正确的方向,我将非常感激!

点赞
用户869951
用户869951

使用 storyboard.loadScene 预加载场景可能是您正在寻找的。

2014-03-27 23:33:21
用户2895078
用户2895078

我的建议是在游戏启动时(或实际运行前)加载您的7种不同的部件,然后通过将alpha设置为0等方式进行隐藏,当游戏真正开始时,您始终重新使用同一组7个部件而不是重新创建它们。例如,当您随机选择一个时,您可以从您的7个部件组中选择,将alpha设置为1并将其x / y位置移动到准确的位置。

这样做可以避免每次重新创建并且会更快。

2014-03-27 23:40:59