I don’t make anything special or hard for CPU in my main game loop. Move objects around the screen, based on my accelerometer data, destroy them if it moves out off bounds and spawn new object every second.
This is simple code of my game loop, but it still freezes:
[lua]local function checkForCoinsDrops()
local currentDropTime = coinsLastDropTime + coinsDropTimeInterval
if currentDropTime < lastTime then
local values = { 1, 1, 1, 1, 2, 2, 2, 5, 5, 10 }
local args = values[math_random( #values )]
local group = game.powerups.coinsGroup
local coin = PowerUpsFactory.create( “coins”, args )
group:insert( coin )
coinsLastDropTime = lastTime
end
end
local function updateGameObjects( deltaX, deltaY, timePassed )
local deltaX, deltaY = deltaX or 0, deltaY or 0
– move objects
for i = gameObjects.numChildren, 1, -1 do
local group = gameObjects[i]
for j = group.numChildren, 1, -1 do
group[j]:update( deltaX, deltaY, timePassed )
end
end
– move effects
for i = effectsGroup.numChildren, 1, -1 do
effectsGroup[i]:translate( deltaX, deltaY )
end
– create new if needed
checkForCoinsDrops()
end
local function updateInGame( timePassed )
– update rocket
local deltaX, deltaY = rocket:update( timePassed, rocketRotationX )
– move background
background:move( deltaX, deltaY )
– move objects
updateGameObjects( deltaX, deltaY, timePassed )
end
local function onFrame( event )
local timePassed = event.time - lastTime
lastTime = lastTime + timePassed
if currentGameState == GAME_STATES.IN_MENU then
updateInMenu( timePassed )
elseif currentGameState == GAME_STATES.IN_GAME then
updateInGame( timePassed )
end
end[/lua]
and
[lua]function object:update( deltaX, deltaY, timePassed )
self:translate( deltaX, deltaY )
– delete if out of bottom bound
if object.y - objectHalfHeight > screenHeight then
object:removeSelf()
object = nil
return
end
– flip coords if too far
local objectPosition = object.x
if objectPosition < leftBound then
object.x = rightBound
– print( “flip flip ?” )
elseif objectPosition > rightBound then
object.x = leftBound
– print( “flip flip ?” )
end
end[/lua] [import]uid: 131398 topic_id: 28011 reply_id: 114170[/import]