cocos2dx lua 热更新

最近研究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.   
  4. local GameScene = class("GameScene",function()  
  5.     return cc.Scene:create()  
  6. end)  
  7.   
  8. function GameScene.create()  
  9.     local scene = GameScene.new()  
  10.     scene:addChild(scene:createTitle())      
  11.     return scene  
  12. end  
  13.   
  14.   
  15. function GameScene:ctor()  
  16.     self.visibleSize = cc.Director:getInstance():getVisibleSize()  
  17.     self.origin = cc.Director:getInstance():getVisibleOrigin()  
  18.     self.schedulerID = nil  
  19. end  
  20.   
  21. function GameScene:createTitle()  
  22.     local title = cc.Label:createWithTTF("Hello World","fonts/arial.ttf",24)  
  23.     title:setPosition(self.visibleSize.width /2,self.visibleSize.height /2)  
  24.       
  25.     return title  
  26. end  

更新的 GameScene.lua
[cpp]  view plain copy
  1. require "Cocos2d"  
  2. require "Cocos2dConstants"  
  3.   
  4. local GameScene = class("GameScene",function()  
  5.     return cc.Scene:create()  
  6. end)  
  7.   
  8. function GameScene.create()  
  9.     local scene = GameScene.new()  
  10.     scene:addChild(scene:createTitle())     
  11.     return scene  
  12. end  
  13.   
  14.   
  15. function GameScene:ctor()  
  16.     self.visibleSize = cc.Director:getInstance():getVisibleSize()  
  17.     self.origin = cc.Director:getInstance():getVisibleOrigin()  
  18.     self.schedulerID = nil  
  19. end  
  20.   
  21. function GameScene:createTitle()  
  22.     local title = cc.Label:createWithTTF("Hello SB","fonts/arial.ttf",24)  
  23.     title:setPosition(self.visibleSize.width /2,self.visibleSize.height /2)  
  24.       
  25.     return title  
  26. end  
最关键的更新模块,MenuScene.lua
[cpp]  view plain copy
  1. require "Cocos2d"  
  2. require "Cocos2dConstants"  
  3.   
  4. local MenuScene = class("MenuScene",function()  
  5.     return cc.Scene:create()  
  6. end)  
  7.   
  8. local title  
  9.   
  10. function MenuScene.create()  
  11.     local scene = MenuScene.new()  
  12.     scene:addChild(scene:createLayer())  
  13.     --scene:addChild(scene:createLayerFarm())  
  14.     --scene:addChild(scene:createLayerMenu())  
  15.     return scene  
  16. end  
  17.   
  18. function MenuScene:ctor()  
  19.     self.visibleSize = cc.Director:getInstance():getVisibleSize()  
  20.     self.origin = cc.Director:getInstance():getVisibleOrigin()  
  21.     self.schedulerID = nil  
  22. end  
  23.   
  24.   
  25.   
  26. function MenuScene:createLayer()  
  27.   
  28.     function menuCallback1(tag)          
  29.         local scene = require("GameScene")  
  30.         local gameScene = scene.create()  
  31.         cc.Director:getInstance():replaceScene(gameScene)          
  32.     end  
  33.       
  34.     function menuCallback2(tag)  
  35.         MenuScene:update()         
  36.     end  
  37.       
  38.     local menuLayer = cc.Layer:create()  
  39.     title = cc.Label:createWithTTF("Choose Scene","fonts/arial.ttf",24)  
  40.     title:setPosition(self.visibleSize.width /2,self.visibleSize.height /4 * 3)  
  41.     menuLayer:addChild(title)      
  42.       
  43.     local menuLabel1 = cc.Label:createWithTTF("Go to Scene""fonts/arial.ttf", 24)      
  44.     local menuItem1 = cc.MenuItemLabel:create(menuLabel1)  
  45.     menuItem1:registerScriptTapHandler(menuCallback1)  
  46.     menuItem1:setPosition(self.visibleSize.width /2,self.visibleSize.height /4 * 2)  
  47.       
  48.       
  49.     local menuLabel2 = cc.Label:createWithTTF("Update""fonts/arial.ttf", 24)      
  50.     local menuItem2 = cc.MenuItemLabel:create(menuLabel2)  
  51.     menuItem2:registerScriptTapHandler(menuCallback2)  
  52.     menuItem2:setPosition(self.visibleSize.width /2,self.visibleSize.height /4 * 1)  
  53.       
  54.     local MainMenu = cc.Menu:create()  
  55.     MainMenu:setPosition(0,0)  
  56.     MainMenu:addChild(menuItem1)  
  57.     MainMenu:addChild(menuItem2)  
  58.     menuLayer:addChild(MainMenu)   
  59.     
  60.     return menuLayer      
  61. end  
  62.   
  63. function MenuScene:getAssetsManager()  
  64.     local function onError(errorCode)  
  65.         if errorCode == cc.ASSETSMANAGER_NO_NEW_VERSION then  
  66.             title:setString("no new version")  
  67.         elseif errorCode == cc.ASSETSMANAGER_NETWORK then  
  68.             title:setString("network error")  
  69.         end  
  70.     end  
  71.   
  72.     local function onProgress( percent )  
  73.         local progress = string.format("downloading %d%%",percent)  
  74.         title:setString(progress)  
  75.     end  
  76.   
  77.     local function onSuccess()  
  78.         title:setString("downloading ok")  
  79.     end  
  80.   
  81.     local pathToSave          = ""  
  82.     pathToSave = createDownloadDir()    
  83.       
  84.     local assetsManager = 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",  
  85.         "https://raw.github.com/samuele3hu/AssetsManagerTest/master/version",  
  86.         pathToSave)  
  87.     assetsManager:retain()  
  88.     assetsManager:setDelegate(onError, cc.ASSETSMANAGER_PROTOCOL_ERROR )  
  89.     assetsManager:setDelegate(onProgress, cc.ASSETSMANAGER_PROTOCOL_PROGRESS)  
  90.     assetsManager:setDelegate(onSuccess, cc.ASSETSMANAGER_PROTOCOL_SUCCESS )  
  91.     assetsManager:setConnectionTimeout(3)   
  92.     return assetsManager  
  93. end  
  94.   
  95. function MenuScene:update()  
  96.     MenuScene:getAssetsManager():update()      
  97. end  
  98.   
  99. return MenuScene  
关键看cc.AssertsMananger。cpp在extensions\assets-manager。
AssetsManager传三个参数,resource.zip路径,version路径,写文件的路径。
然后调用AssetsManager.update,进行下载更新。

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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值