加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

cocos2dx lua 热更新

发布时间:2020-12-14 16:44:53 所属栏目:百科 来源:网络整理
导读:最近研究cocos2dx + lua 实现游戏的热更新。基本原理:比如我有一个GameScene,显示"hello world",现在热更新,更新之后显示"hello SB".如果是用c++来开发,那么只用把它作成lib库,更新时替换lib库。用lua的话,更简单,替换相关的lua就可以了。 GameScene.
最近研究cocos2dx + lua 实现游戏的热更新。基本原理:比如我有一个GameScene,显示"hello world",现在热更新,更新之后显示"hello SB".如果是用c++来开发,那么只用把它作成lib库,更新时替换lib库。用lua的话,更简单,替换相关的lua就可以了。
GameScene.lua
[cpp] view plain copy
  1. require"Cocos2d"
  2. require"Cocos2dConstants"
  3. localGameScene=class("GameScene",function()
  4. returncc.Scene:create()
  5. end)
  6. functionGameScene.create()
  7. localscene=GameScene.new()
  8. scene:addChild(scene:createTitle())
  9. returnscene
  10. end
  11. functionGameScene:ctor()
  12. self.visibleSize=cc.Director:getInstance():getVisibleSize()
  13. self.origin=cc.Director:getInstance():getVisibleOrigin()
  14. self.schedulerID=nil
  15. end
  16. functionGameScene:createTitle()
  17. localtitle=cc.Label:createWithTTF("HelloWorld","fonts/arial.ttf",24)
  18. title:setPosition(self.visibleSize.width/2,self.visibleSize.height/2)
  19. returntitle
  20. end

更新的 GameScene.lua
copy
    localtitle=cc.Label:createWithTTF("HelloSB",248)"> end
最关键的更新模块,MenuScene.lua
copy
    localMenuScene=class("MenuScene",248)"> localtitle
  1. functionMenuScene.create()
  2. localscene=MenuScene. scene:addChild(scene:createLayer())
  3. --scene:addChild(scene:createLayerFarm())
  4. --scene:addChild(scene:createLayerMenu())
  5. functionMenuScene:ctor()
  6. self.visibleSize=cc.Director:getInstance():getVisibleSize()
  7. self.origin=cc.Director:getInstance():getVisibleOrigin()
  8. self.schedulerID=nil
  9. functionMenuScene:createLayer()
  10. functionmenuCallback1(tag)
  11. localscene=require("GameScene")
  12. localgameScene=scene.create()
  13. cc.Director:getInstance():replaceScene(gameScene)
  14. functionmenuCallback2(tag)
  15. MenuScene:update()
  16. localmenuLayer=cc.Layer:create()
  17. title=cc.Label:createWithTTF("ChooseScene",24)
  18. title:setPosition(self.visibleSize.width/2,self.visibleSize.height/4*3)
  19. menuLayer:addChild(title)
  20. localmenuLabel1=cc.Label:createWithTTF("GotoScene",248)"> localmenuItem1=cc.MenuItemLabel:create(menuLabel1)
  21. menuItem1:registerScriptTapHandler(menuCallback1)
  22. menuItem1:setPosition(self.visibleSize.width/2,self.visibleSize.height/4*2)
  23. localmenuLabel2=cc.Label:createWithTTF("Update",248)"> localmenuItem2=cc.MenuItemLabel:create(menuLabel2)
  24. menuItem2:registerScriptTapHandler(menuCallback2)
  25. menuItem2:setPosition(self.visibleSize.width/2,self.visibleSize.height/4*1)
  26. localMainMenu=cc.Menu:create()
  27. MainMenu:setPosition(0,0)
  28. MainMenu:addChild(menuItem1)
  29. MainMenu:addChild(menuItem2)
  30. menuLayer:addChild(MainMenu)
  31. returnmenuLayer
  32. functionMenuScene:getAssetsManager()
  33. localfunctiononError(errorCode)
  34. iferrorCode==cc.ASSETSMANAGER_NO_NEW_VERSIONthen
  35. title:setString("nonewversion")
  36. elseiferrorCode==cc.ASSETSMANAGER_NETWORKthen
  37. title:setString("networkerror")
  38. localfunctiononProgress(percent)
  39. localprogress=string.format("downloading%d%%",percent)
  40. title:setString(progress)
  41. localfunctiononSuccess()
  42. title:setString("downloadingok")
  43. localpathToSave=""
  44. pathToSave=createDownloadDir()
  45. localassetsManager=cc.AssetsManager:new("http://qd.baidupcs.com/file/1a531cc6565d95827e0ec3f9fd4af795?fid=138463011-250528-222085911919595&time=1406097600&sign=FDTAXER-DCb740ccc5511e5e8fedcff06b081203-mLvWIkhwaVZCjN%2BpTYZJZ%2BagPR4%3D&to=qb&fm=N,B,T,t&newver=1&expires=1406098200&rt=sh&r=164813188&mlogid=124796854&sh=1&vuk=138463011&vbdid=2986594979&fn=GameScene.zip",
  46. "https://raw.github.com/samuele3hu/AssetsManagerTest/master/version",
  47. pathToSave)
  48. assetsManager:retain()
  49. assetsManager:setDelegate(onError,cc.ASSETSMANAGER_PROTOCOL_ERROR)
  50. assetsManager:setDelegate(onProgress,cc.ASSETSMANAGER_PROTOCOL_PROGRESS)
  51. assetsManager:setDelegate(onSuccess,cc.ASSETSMANAGER_PROTOCOL_SUCCESS)
  52. assetsManager:setConnectionTimeout(3)
  53. returnassetsManager
  54. functionMenuScene:update()
  55. MenuScene:getAssetsManager():update()
  56. returnMenuScene
关键看cc.AssertsMananger。cpp在extensionsassets-manager。
AssetsManager传三个参数,resource.zip路径,version路径,写文件的路径。
然后调用AssetsManager.update,进行下载更新。

注意的是,android时,它是写到Context.getCacheDir()里。所以每次进入游戏时,都会重新更新。需要修改 createDownloadDir()里创建的路径,把它写到sd卡上。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读