类型 函数 返回值 无 修订版 版本 2024.3703 关键字 按需资源,onDemandResources,请求 另请参阅 onDemandResources.setEventListener() onDemandResources.*
这是插件的核心函数。调用它会启动请求,以访问
在尝试使用资源之前,这些资源必须可用。该函数可以同时检查资源的可用性或下载它们。如果所需的资源不可用,则应请求下载它们,并在尝试使用它们之前,等待成功的回调响应。
在成功请求和下载资源后,可以安全地访问它们,直到应用程序终止为止。
即使你可以使用
请记住
onDemandResources.request( tag [, download] [, listener] )
字符串. 要请求的标签
布尔值. 指示如果资源不在本地可用,是否应该开始下载该资源。默认值为 true。如果你只想检查关联的资源是否在本地可用,请传递 false,这样此函数将几乎立即调用
侦听器. 当资源可以访问时或发生错误时,将接收
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 )