Cocos2d-x 3.2 Lua example AssetsManagerTest (Resource Manager), cocos2d3.2lua

Source: Internet
Author: User

Cocos2d-x 3.2 Lua example AssetsManagerTest (Resource Manager), cocos2d3.2lua
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, where three menu items are found:

  • Enter
  • Reset
  • Update
Enter is the scenario, 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 f Unction onProgress (percent) -- displays the 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 ("https://raw.github.c Om/samuele3hu/AssetsManagerTest/master/ "," ", pathToSave) -- retain ownership, which increases the reference count of the Ref object assetsManager: retain () -- set 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 getAssetsManager (): update () end -- reset 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 the local function 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 (AssetsManagerTest Main) end -- callback method local callbackFuncs = {enter, reset, update,} -- menu callback method local function menuCallback (tag, menuItem) local 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.
CreateDirectory: create a directory based on the platform
DeleteVersion: Delete the local version.
DownLoad: downLoad an object
DownloadAndUncompress: Download and decompress the file
GetConnectionTimeout: Get the connection timeout
GetDelegate: obtains the delegate object.
GetPackageUrl: Get the compressed package address
GetStoragePath: Get the storage address
GetVersion: get the version number.
GetVersionFileUrl: get the version file address
SetConnectionTimeout: Set network connection timeout
SetDelegate: Set Delegation
SetPackageUrl: Set the package path
SetSearchPath: set the priority resource search path
SetStoragePath: Set the storage path
SetVersionFileUrl: sets the version file path.
Uncompress: Decompress the file
Update: update

Here we also introduce a delegate class: AssetsManagerDelegateProtocol. Three callback methods are required when downloading updates:

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.

When creating a cocos2D-x project in VS2010, Select Lua support: Support Lua (Hook), an error is reported after generation,

Some library files are not properly loaded. Find the corresponding library file and paste it to the corresponding location. Do not forget to check whether the file has permissions.

How to combine cocos2d-x with Lua to use specific points

The cocos2d-x comes with the lua engine. Look at this. Although it is English, it is easy to understand.
Cocos2d-x NEW Lua Engine README
Main features
Support autorelease CCObject object.
Call Lua function from C ++ (local, global, closure), avoid memory leaks.
Add CCNode: setPosition (x, y), CCNode: getPosition () huge performance boost.
Remove needless class and functions from tolua ++. pkg files, improved performance.
CCMenuItem events handler.
CCNode onEnter/onExit events handler.
CCLayer touch & multi-touches events handler.
Check the two connections.
Scripting and Translating between Programming ages

Related Article

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.