Async image download problem

I have a number of images loaded up by looping over an array. The only thing is that this freezes the device until all 15 images are downloaded and displayed. Async image download would be an excellent solution to this, but I can’t get it to perform reliably within loop code.

Note: Images I’m downloading aren’t ever over 20 KB (200x300 pixels).

Loop code:

for z=1,15 do  
...  
end  

Original Async image download example:

[code]
local function networkListener( event )
if ( event.isError ) then
print ( “Network error - download failed” )
else
myImage = display.newImage( “helloCopy.png”, system.TemporaryDirectory, 60, 40 )
myImage.alpha = 0
transition.to( myImage, { alpha = 1.0 } )
end

print ( "RESPONSE: " … event.response )
end

network.download(
http://developer.anscamobile.com/demo/hello.png”,
“GET”,
networkListener,
“helloCopy.png”,
system.TemporaryDirectory )

– NOTE: files saved to system.TemporaryDirectory are not guaranteed to persist across launches.
– Use system.DocumentsDirectory if you want files to persist.


– Method 2: use display.loadRemoteImage() to get the file and create a display object in one step

local myImage2 = {}
local function networkListener2( event )
if ( event.isError ) then
print ( “Network error - download failed” )
else
event.target.alpha = 0
transition.to( event.target, { alpha = 1.0 } )
end
myImage2 = event.target
print ( "RESPONSE: " … event.response )
end

display.loadRemoteImage(
http://developer.anscamobile.com/demo/hello.png”,
“GET”,
networkListener2,
“helloCopy2.png”,
system.TemporaryDirectory,
60, 280 )
[/code] [import]uid: 21125 topic_id: 6709 reply_id: 306709[/import]

off the top of my head

you are clobbering the listener

try a different approach or once the image finishes call back again

neworklistener(...)  
 ...after image is loaded ... call next image  
  
end  

c [import]uid: 24 topic_id: 6709 reply_id: 23511[/import]

How would you use the

… call next image  

syntax when dealing with an array? Where does the increase in the ‘z’ value occur? Something like this:

[code]

networkListener(…)
…after image is loaded … call next image[z]

end

for z=1,15 do

display.loadRemoteImage(
http://developer.anscamobile.com/demo/hello” … z … “.png”,
“GET”,
networkListener,
“helloCopy-” … z … “.png”,
system.TemporaryDirectory,
60, 280 )

end

[/code] [import]uid: 21125 topic_id: 6709 reply_id: 23552[/import]

use a counter not a for…loop [import]uid: 6645 topic_id: 6709 reply_id: 23654[/import]

use a counter not a for…loop

[lua]local z = 0
local totalImages = 15

local done
local loadNextImage

local function networkListener()

if(z loadNextImage()
else
done()
end

end

function loadNextImage()

z=z+1

display.loadRemoteImage(
http://developer.anscamobile.com/demo/hello” … z … “.png”,
“GET”,
networkListener,
“helloCopy-” … z … “.png”,
system.TemporaryDirectory,
60, 280 )

end

function done()
print(“done”)
end

loadNextImage()[/lua]

[import]uid: 6645 topic_id: 6709 reply_id: 23655[/import]