I am making a game using a character that drives along a track made of different "sections" that are randomly picked. It uses physics, most bodies have many vertices and were made with Physics Editor. My implementation is not very good, because I recreate every section every time a new section is needed. This causes a frame rate skip because it has to create these large physics bodies during game play that is "time critical". Each section is 2000 pixels long and I have 7 so far. Could someone tell me a better way to implement this? Thanks!
Here are my functions for picking and creating the random sections:
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)
and an example of a create function
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
The groups are so there are 3 sections present at once. What would be a better way to implement this, eliminating the frame skip? I would be very thankful is someone could help me or point me in the right direction!
通过storyboard.loadScene
预加载场景可能就是您想要的。
My advice would be to load your 7 different kind of pieces when the game is launched (or before a run is actually started), then hide them by setting alpha to 0 for example, and when the game actually starts you always re-use the same 7 pieces instead of recreating them... For example, when you randomly select one, you select from your set of 7 pieces, make alpha to 1 and move it's x / y positions to the accurate spot.
This way you won't re-create it everytime and it will be much faster.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.