Problem with load many large images

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:

[lua]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[/lua]

Until image is between 16-19, my game is crashed.

Please help [import]uid: 107019 topic_id: 21370 reply_id: 321370[/import]

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.
[import]uid: 108660 topic_id: 21370 reply_id: 84611[/import]

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;
[lua]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)[/lua]

Peach :slight_smile:
[import]uid: 52491 topic_id: 21370 reply_id: 84615[/import]