[英]Corona SDK random physics landscapes
我正在使用一個角色沿着隨機選取的不同“部分”組成的軌跡行駛的游戲來制作游戲。 它使用物理學,大多數物體具有許多頂點,並且是通過物理學編輯器制作的。 我的實現不是很好,因為每當需要一個新的部分時,我都會重新創建每個部分。 這會導致幀速率跳躍,因為它必須在“時間緊迫”的游戲過程中創建這些大型物理物體。 每個部分的長度為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)
和一個創建函數的例子
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個部分。 消除跳幀的更好方法是什么呢? 如果有人可以幫助我或指出正確的方向,我將非常感激!
通過storyboard.loadScene
預加載場景可能就是您想要的。
我的建議是,在游戲啟動時(或實際開始運行之前)加載7種不同的棋子,然后通過將alpha設置為0來隱藏它們,而當游戲真正啟動時,您始終會重復使用相同的7個片段,而不是重新創建它們...例如,當您隨機選擇一個片段時,從7個片段集中選擇一個,將alpha設置為1,然后將其x / y位置移動到准確的位置。
這樣,您將不會每次都重新創建它,它會更快。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.