Cocos2d-x 3.2 Lua example AssetsManagerTest (Resource Manager)

Source: Internet
Author: User

Cocos2d-x 3.2 Lua example AssetsManagerTest (Resource Manager)
Cocos2d-x 3.2 Lua example AssetsManagerTest (Resource Manager)
This blog introduces an example of the use of AssetsManager in Lua, a class provided by Cocos2d-x. The effect is as follows:

The example given by the Cocos2d-x is AssetsManagerTest, three menu items will be found when entering: enterresetupdateenter is into the scene, reset is to delete the local version, reset, update is to update the resource file.

I use LDT to open the lua-tests Test Project:

Find the AssetsManagerTest directory under the src directory and view the following code (I have commented on it): >>> AsetsManagerModule. lua

-- [[Resource Manager Module] -- local AssetManagerModule ={} -- [[newScene] -- function AssetManagerModule. newScene (backfunc) -- Obtain the screen size. local winSize = cc. director: getInstance (): getWinSize () -- create a new scenario: local newScene = cc. scene: create () -- create a new layer local layer = cc. layer: create () -- updates the local function backToUpdate () local scene = backfunc () if scene ~ = Nil then cc. director: getInstance (): replaceScene (scene) end -- create a rollback menu cc. menuItemFont: setFontName ("Arial") cc. menuItemFont: setFontSize (24) local backMenuItem = cc. menuItemFont: create ("Back") -- placed in the approximate position backMenuItem: setPosition (cc. p (VisibleRect: rightBottom (). x-50, VisibleRect: rightBottom (). y + 25) -- register the listener method backMenuItem: registerScriptTapHandler (backToUpdate) -- create the menu local backMenu = cc. menu: create () backMenu: setPosition (0, 0) backMenu: addChild (backMenuItem) layer: addChild (backMenu, 6) -- create the label local helloLabel = cc. label: createWithTTF ("Hello World", s_arialPath, 38) helloLabel: setAnchorPoint (cc. p (0.5, 0.5) -- helloLabel: setPosition (cc. p (winSize. width/2, winSize. height-40) layer: addChild (helloLabel, 5) -- creates an sprite. Here is a background image local sprite = cc. sprite: create ("Images/background.png") sprite: setAnchorPoint (cc. p (0.5, 0.5) -- The Anchor center sprite: setPosition (cc. p (winSize. width/2, winSize. height/2) layer: addChild (sprite, 0) newScene: addChild (layer) -- add to scenario cc. director: getInstance (): replaceScene (newScene) -- replace scene end -- return Module return AssetManagerModule

>>> AssetsManagerTest. lua
-- Obtain the target platform local targetPlatform = cc. application: getInstance (): getTargetPlatform () local lineSpace = 40 -- line spacing local itemTagBasic = 1000 local menuItemNames = {"enter", "reset", "update ",} -- Obtain the screen size. local winSize = cc. director: getInstance (): getWinSize () -- Update layer local function updateLayer () -- first create a layer local layer = cc. layer: create () local support = false -- determine whether iphone, ipad, win32, android, or mac if (cc) is supported. PLATFOR M_ OS _IPHONE = targetPlatform) or (cc. PLATFORM_ OS _IPAD = targetPlatform) or (cc. PLATFORM_ OS _WINDOWS = targetPlatform) or (cc. PLATFORM_ OS _ANDROID = targetPlatform) or (cc. PLATFORM_ OS _MAC = targetPlatform) then support = true end -- if the Platform is not supported if not support then print ("Platform is not supported! ") Return layer end local isUpdateItemClicked = false -- whether the update item is clicked local assetsManager = nil -- resource manager object local pathToSave =" "-- save path local menu = cc. menu: create () -- menu Menu menu: setPosition (cc. p (0, 0) -- set the menu position to cc. menuItemFont: setFontName ("Arial") -- set the menu font style cc. menuItemFont: setFontSize (24) -- set the font size -- used to update the label local progressLable = cc. label: createWithTTF ("", s_arialPath, 30) progressLable: setAnchorPoint (cc. p (0.5, 0.5) progressLable: setPosition (cc. p () layer: addChild (progressLable) -- Download directory pathToSave = createDownloadDir () -- Download error callback local function onError (errorCode) -- no new version if errorCode = cc. ASSETSMANAGER_NO_NEW_VERSION then progressLable: setString ("no new version") elseif errorCode = cc. ASSETSMANAGER_NETWORK then -- network error progressLable: setString ("network error") end -- progress Update callback local function onProgress (percent) -- display download progress local progress = string. format ("downloading % d %", percent) progressLable: setString (progress) end -- callback local function onSuccess () progressLable: setString ("downloading OK ") end -- Obtain the resource manager local function getAssetsManager () if nil = assetsManager then -- create a resource manager. The first parameter is the zip package, and the second parameter is the version file, the third parameter is the storage path assetsManager = cc. assetsManager: new (" "," ", PathToSave) -- retain ownership. This method increases the reference count of the Ref object. assetsManager: retain () -- sets a series of assetsManager: setDelegate (onError, cc. ASSETSMANAGER_PROTOCOL_ERROR) assetsManager: setDelegate (onProgress, cc. ASSETSMANAGER_PROTOCOL_PROGRESS) assetsManager: setDelegate (onSuccess, cc. ASSETSMANAGER_PROTOCOL_SUCCESS) assetsManager: setConnectionTimeout (3) -- set connection timeout end return assetsManager end -- update local function update (sender) ProgressLable: setString ("") -- call the update method of AssetsManager getAssetsManager (): update () end -- reset the local function reset (sender) progressLable: setString ("") -- delete the download path deleteDownloadDir (pathToSave) -- delete the getAssetsManager (): deleteVersion () -- create the download path createDownloadDir () end -- reload the module local function reloadModule (moduleName) package. loaded [moduleName] = nil return require (moduleName) end -- enter local fu Nction enter (sender) -- if the update button is not clicked, if not isUpdateItemClicked then local realPath = pathToSave .. "/package" addSearchPath (realPath, true) end -- reload the module assetsManagerModule = reloadModule ("src/AssetsManagerTest/AssetsManagerModule") assetsManagerModule. newScene (AssetsManagerTestMain) end -- callback method local callbackFuncs = {enter, reset, update,} -- menu callback method local function menuCallback (tag, menuItem) lo Cal scene = nil local nIdx = menuItem: getLocalZOrder ()-itemTagBasic local ExtensionsTestScene = CreateExtensionsTestScene (nIdx) if nil ~ = ExtensionsTestScene then cc. director: getInstance (): replaceScene (ExtensionsTestScene) end -- traverse to add three menu items for I = 1, table. getn (menuItemNames) do local item = cc. menuItemFont: create (menuItemNames [I]) item: registerScriptTapHandler (callbackFuncs [I]) -- register and click the callback address -- set the position item: setPosition (winSize. width/2, winSize. height-I * lineSpace) if not support then item: setEnabled (false) end menu: addChil D (item, itemTagBasic + I) end local function onNodeEvent (msgName) if nil ~ = AssetsManager then -- release resource assetsManager: release () assetsManager = nil end -- Registration layer click callback method layer: registerScriptHandler (onNodeEvent) layer: addChild (menu) return layerend --------------------------------------- AssetsManager Test --------------------------------------- function AssetsManagerTestMain () local scene = cc. scene: create () scene: addChild (updateLayer () scene: addChild (CreateBackMenuItem () return sceneend

The following figure is taken from the official website:

The AssetsManager class provides the above methods. The following describes the methods one by one:
The constructor has three parameters: zip, version file network address, and download and save path.
CheckStoragePath: Check the storage path
CheckUpdate: checks for updates and returns the bool value.
CreateDirectZ success? "Target =" _ blank "class =" keylink "> keys/keys + s/keys + keys/C1NjOxLz + PGJyIC8 + PC9zdHJvbmc + keys/keys + PC9zdHJvbmc + keys/zPGJyIC8 + response/response + zsS8/response + PC9zdHJvbmc + response/I18rUtMvRy/response + PC9zdHJvbmc + response + PC9zdHJvbmc + response + wre + response /keys + keys/keys + PGJyIC8 + keys + PGJyIC8 + keys + PC9zdHJvbmc + keys + PGJyIC8 + keys + PGJyIC8 + keys/C1Ni4/keys + pgltzybzcm9" "Alt =" "/>

Readers can take a look at the above Code, here the Cocos2d-x is just to give a simple example of using AssetsManager to hot update the program, but does not provide a complete solution. Later I will also conduct research on Lua to Cocos2d-x Client hot update this part, have the opportunity to share with you this knowledge.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.