I need to load a number of assets and it takes more than a few seconds so I want to put up a loading bar showing progress. This would be a graphical image (not just a rectangle).
The problem is that I cannot get the screen to update in between loading each chunk of data. I have tried a number of approaches, but none seem to work. Looking at other similar queries on the subject has not yielded any solutions to this issue.
Typically you would handle this with a callback to update the screen, but Corona does not appear to have any explicit function to “update” the screen. What I thought would work within the structure of Corona’s architecture was using the timer.performWithDelay() function, but this does not work. Here is a sample of what I tried:
[lua]-- Sample Load screen.
– This *should* show a meter bar advancing as each chunk is loaded.
– Unfortunately it doesn’t seem to yield to the Corona SDK to let it update the display.
module(…, package.seeall)
local bUseListener = true
local nLoadIndex
local nLoadCount
local nMeterX = 161 – final X position of the meter bar when at 100 percent
local nMeterY = 508 – Y position of the meter bar
local nMeterW = 480 – Width of my meter bar image
local nMeterH = 30 – Height of my meter bar image
– These scale factors are because my source content is 800x600
– By using these I can change display modes (devices) and everything scales correctly
local nScaleX = display.contentWidth/800
local nScaleY = display.contentHeight/600
– Initialize the load variables
function LoadInit()
nLoadIndex = 1
nLoadCount = 20 – This is the number of “chunks” of data
end
– Load the next “chunk” of data. Each chunk is an sprite sheet
function LoadChunk()
– This is where I load my data. I have removed the calls to
– sprite.newSpriteSheetFromData since they aren’t necessary
– to show the display issue
– Now get ready for the next chunk, if any
nLoadIndex = nLoadIndex + 1
end
– Handle any finalization of the data here
function LoadComplete()
end
– Main function - MUST return a display.newGroup()
function new()
local splashGroup = display.newGroup()
local loadingImage, loadingBar, loadingMask
loadingImage = display.newImageRect( “load_bkgd.png”, display.contentWidth, display.contentHeight )
splashGroup:insert( loadingImage )
loadingImage.x = display.contentCenterX
loadingImage.y = display.contentCenterY
loadingBar = display.newImageRect( “load.png”, math.floor( nMeterW * nScaleX ), math.floor( nMeterH * nScaleY ) )
splashGroup:insert( loadingBar )
loadingBar:setReferencePoint( display.TopLeftReferencePoint )
loadingBar.x = math.floor( (nMeterX-nMeterW) * nScaleX )
loadingBar.y = math.floor( nMeterY * nScaleY )
loadingMask = display.newImageRect( “load_mask.png”, math.floor( 800 * nScaleX + 0.5 ), math.floor( 99 * gWS.nScaleY + 0.5 ) )
splashGroup:insert( loadingMask )
loadingMask:setReferencePoint( display.TopLeftReferencePoint )
loadingMask.x = 0
loadingMask.y = math.floor( (600-99)* nScaleY + 0.5)
– This was originally called via a Runtime event for “enterFrame”. It makes no
– difference if you change the code to do it that way or call it explicitly, you
– still do not see the bar advance until all of the disk I/O is done.
function updateBar()
local nPercent = math.floor( 100 * nLoadIndex / nLoadCount )
print( "Percent = ", nPercent )
loadingBar.x = loadingBar.x + math.floor( (nMeterX-nMeterW + (nMeterW*nPercent/100)) * nScaleX )
if ( nPercent >= 100 ) then
loadingBar.x = math.floor( nMeterX * nScaleX )
end
end
function myLoadChunk()
– Load a chunk of data
LoadChunk()
if ( bUseListener == false ) then
– update the loading bar. See the comments above
– regarding using a event to trigger this
updateBar()
end
– Are there any chunks remaining? If so, set a timer to
– load the next one. This *should* be non-blocking, but
– if you actually load data from a file in LoadChunk() this
– never yields to the SDK so it can update the screen.
if ( nLoadIndex <= nLoadCount ) then
timer.performWithDelay( 100, myLoadChunk )
else
if ( bUseListener == true ) then
Runtime:removeEventListener( “enterFrame”, updateBar )
end
– Done, so finish any data stuff
LoadComplete()
– Onward and outward
director:changeScene( “cSceneGame” )
end
end
– Set everything up so we can start going
LoadInit()
– Start it going
myLoadChunk()
if ( bUseListener == true ) then
Runtime:addEventListener( “enterFrame”, updateBar )
end
clean = function()
if loadingImage then
display.remove( loadingImage )
loadingImage = nil
end
if loadingBar then
display.remove( loadingBar )
loadingBar = nil
end
if loadingMask then
display.remove( loadingMask )
loadingMask = nil
end
end
– MUST return a display.newGroup()
return splashGroup
end[/lua] [import]uid: 16734 topic_id: 16253 reply_id: 316253[/import]
[import]uid: 3826 topic_id: 16253 reply_id: 60519[/import]