Hey all,
I am struggeling with some performance on my parallex background. The idea of the game is something like jetpack joyride.
The parallex background has 4 levels.
Here is my code:
local function backgroundOnUpdate(dt) local speedBg2 = 0.4\*dt\*gameSpeed -- level1\_layer\_midmid3 local speedBg3 = 0.6\*dt\*gameSpeed -- level1\_layer\_midBg4 local speedBg4 = 0.9\*dt\*gameSpeed -- level1\_layer\_mid3 local speedBg5 = 2\*dt\*gameSpeed -- level1\_layer\_bottom3 local countBg2 = 3 local countBg3 = 4 local countBg4 = 4 local countBg5 = 4 for i=1, countBg2 do if (backgroundGroup2[i].isVisible) then backgroundGroup2[i]:translate(-speedBg2, 0) end end for i=1, countBg3 do if (backgroundGroup3[i].isVisible) then backgroundGroup3[i]:translate(-speedBg3, 0) end end for i=1, countBg4 do if (backgroundGroup4[i].isVisible) then backgroundGroup4[i]:translate(-speedBg4, 0) end end for i=1, countBg5 do if (backgroundGroup5[i].isVisible) then backgroundGroup5[i]:translate(-speedBg5, 0) end end if (backgroundGroup2[backgroundGroup2.last].x \< -960-speedBg2) then backgroundGroup2[backgroundGroup2.last].isVisible = false backgroundGroup2.last = backgroundGroup2.last + 1 if (backgroundGroup2.last \> countBg2) then backgroundGroup2.last = 1 end local nextBg = backgroundGroup2.last + 1 if (nextBg \> countBg2) then nextBg = 1 end local newBg = backgroundGroup2[nextBg] newBg.anchorX = 0 newBg.anchorY = 0 newBg.x = 960 - speedBg2 newBg.y = 0-70 newBg.isVisible = true end if (backgroundGroup3[backgroundGroup3.last].x \< -960-speedBg3) then backgroundGroup3[backgroundGroup3.last].isVisible = false backgroundGroup3.last = backgroundGroup3.last + 1 if (backgroundGroup3.last \> countBg3) then backgroundGroup3.last = 1 end local nextBg = backgroundGroup3.last + 1 if (nextBg \> countBg3) then nextBg = 1 end local newBg = backgroundGroup3[nextBg] newBg.anchorX = 0 newBg.anchorY = 1 newBg.x = 960 - speedBg3 newBg.y = contentHeight+70 newBg.isVisible = true end if (backgroundGroup4[backgroundGroup4.last].x \< -960-speedBg4) then backgroundGroup4[backgroundGroup4.last].isVisible = false backgroundGroup4.last = backgroundGroup4.last + 1 if (backgroundGroup4.last \> countBg4) then backgroundGroup4.last = 1 end local nextBg = backgroundGroup4.last + 1 if (nextBg \> countBg4) then nextBg = 1 end local newBg = backgroundGroup4[nextBg] newBg.anchorX = 0 newBg.anchorY = 0 newBg.x = 960 - speedBg4 newBg.y = 0-70 newBg.isVisible = true end if (backgroundGroup5[backgroundGroup5.last].x \< -960-speedBg5) then backgroundGroup5[backgroundGroup5.last].isVisible = false backgroundGroup5.last = backgroundGroup5.last + 1 if (backgroundGroup5.last \> countBg5) then backgroundGroup5.last = 1 end local nextBg = backgroundGroup5.last + 1 if (nextBg \> countBg5) then nextBg = 1 end local newBg = backgroundGroup5[nextBg] newBg.anchorX = 0 newBg.anchorY = 1 newBg.x = 960 - speedBg5 newBg.y = contentHeight+70 newBg.isVisible = true end end
The function is executed on every “EnterFrame” event, also the delta time is implemented.
The images are big (768x512), the average texture size is around 20.
The problem is, when the current background image is going offscreen and the next background image is made visible, that the app stutters, like the redraw time is increasing highly. This is most noticeable on older devices like Xperia U. I also tested it on Nexus 4, even there you got sometimes small stutters (like some microseconds but still noticeable).
I have configured the app on 60 fps.
My conclusion is that the redraw time is increasing highly, but tell me if im wrong.
I also wonder how to proper make smooth and not heavy but still high quality parallex background.
I also wonder if i can somehow lazy precache the images without affecting the redraw time of the screen, so the old not used images could be removed
Some tips?
Many Thanks