One of my game scene, there are 23 images and size 960x640 for each. All images set visibility to false (object.isVisible = false) but show only first image. When touch on button, next image will be show and current image will be hide. The example code is below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | local bg = {} local step = 1 for i =1 , 23 do bg[i] = display.newImageRect( "bg" .. i .. ".png", 480, 320 ) bg[i].isVisible = false bg[i].x = display.contentWidth / 2 bg[i].y = display.contentHeight / 2 end local button = display.newRect( 0, 0, 100, 100 ) local function onTouchButton( event ) if event.phase == "began" then bg[step].isVisible = false bg[step + 1].isVisible = true step = step + 1 end end button:addEventListener( "touch", onTouchButton ) bg[1].isVisible = true |
Dont load them all at the beginning.
If you load one, then load the next one only in response to a touch, you can use transition.to to make the change smoother.
So, Load A as an image, and display it
Touch occurs
Transition.to (A... ) to fade away using alpha, over perhaps one second
Load B as an image
set B alpha to 0
transition.to (B ....) to fade in over perhaps a second
then: display.remove( A)
and that will free up your memory.
Jeff is correct - you may also want to consider using a memory check function as it can help you manage things better.
Try something like this;
1 2 3 4 5 6 7 8 9 10 | local function monitorMem(event) collectgarbage("collect") print( "\nMemUsage: " .. (collectgarbage("count")/1000) .. " MB") print("Texture Usage " .. system.getInfo( "textureMemoryUsed" ) / 1000000) return true end Runtime:addEventListener("enterFrame", monitorMem) |