Type Function Return value none Revision 2017.3060 Keywords on-demand resources, onDemandResources, request See also onDemandResources.setEventListener() onDemandResources.*
This is the core function of the plugin. Calling it initiates a request to access build.settings.
Resources must be available before you attempt to use them. This function can both check the availability of resources or download them. If the desired resources are not available, you should request their download and wait for a successful callback response before attempting to use them.
Once resources are successfully requested and downloaded, they can be safely accessed until the application is terminated.
Even though you can use
Remember that
onDemandResources.request( tag [, download] [, listener] )
String. Tag for which to request
Boolean. Indicates if downloading of the resource(s) should begin if they are not locally available. Default value is true. If you simply want to check if the associated resources are locally available, pass false and this function will invoke an
Listener. Listener function that will receive an
local odr = require( "plugin.onDemandResources" )
-- On-demand resources listener function
local function odrListener( event )
if ( event .isError ) then
-- Resources are not downloaded; see next example for download usage
end
end
-- Check image resources for second level
odr.request( "imgL2", false, odrListener ) -- Pass 'false' as second argument to check (not download)
local odr = require( "plugin.onDemandResources" )
-- On-demand resources listener function
local function odrListener( event )
if not ( event.isError ) then
print( "Resources for tag '" .. event.tag .. "' downloaded" )
else
print( "ERROR: errorCode = " .. tostring(event.errorCode) )
end
end
-- Request image resources for first level
odr.request( "imgL1", true, odrListener )
local odr = require( "plugin.onDemandResources" )
local composer = require( "composer" )
local widget = require( "widget" )
local levelTag = "assetsL1"
local levelScene = "level1"
-- This function will indicate progress via a widget and go to scene when done
local function downloadResources( tag )
-- Create progress bar
local progressBar = widget.newProgressView( { x=160, y=240, width=120, isAnimated=false } )
progressBar:setProgress( 0 )
-- Runtime listener to update progress bar
local function updateProgress()
progressBar:setProgress( odr.progress(tag) )
end
Runtime:addEventListener( "enterFrame", updateProgress )
-- Now, download resources with "urgent" priority
odr.request( tag, true,
function( event )
-- Remove progress bar
Runtime:removeEventListener( "enterFrame", updateProgress )
progressBar:removeSelf()
progressBar = nil
-- Proceed to level scene
if not ( event.isError ) then
composer.gotoScene( levelScene )
end
end )
odr.setDownloadPriority( tag, "urgent" )
end
-- First, simply check if resources are already downloaded
odr.request( levelTag, false,
function( event )
if not ( event.isError ) then
-- Resources already downloaded; proceed to level scene
composer.gotoScene( levelScene )
else
-- Resources must be downloaded; call download function above
downloadResources( levelTag )
end
end )