cocos2dx js 3.2 热更新

时间:2023-02-08 12:51:24

COCOS IDE用手机调试更新是正常的,是预想的结果,但用COCOS IDE打包发布APK,安装到手机上,热更新下载图片、JSON UI什么的都能正常更新替换,但JS脚本没有替换,这是为毛。
更新文件是已经有下载到手机上了
root@hwB199:/ # ls -l /data/data/org.cocos2dx.CocosJSGame/files/
ls -l /data/data/org.cocos2dx.CocosJSGame/files/
-rw------- u0_a113  u0_a113       738 2014-12-18 15:51 project.manifest
drwx------ u0_a113  u0_a113           2014-12-18 15:51 res
drwx------ u0_a113  u0_a113           2014-12-18 15:51 src
-rw------- u0_a113  u0_a113       303 2014-12-18 15:51 version.manifest

res文件夹:
root@hwB199:/ # ls -l /data/data/org.cocos2dx.CocosJSGame/files/res
ls -l /data/data/org.cocos2dx.CocosJSGame/files/res
-rw------- u0_a113  u0_a113    324064 2014-12-18 15:51 HelloWorld.png
-rw------- u0_a113  u0_a113    133659 2014-12-18 15:51 image.png

src文件夹:
root@hwB199:/ # ls -l /data/data/org.cocos2dx.CocosJSGame/files/src
ls -l /data/data/org.cocos2dx.CocosJSGame/files/src
-rw------- u0_a113  u0_a113      2309 2014-12-18 15:51 app.js
-rw------- u0_a113  u0_a113       257 2014-12-18 15:51 resource.js



贴出代码:
project.json


{
    "project_type":"javascript",
    "debugMode":1,
    "showFPS":true,
    "frameRate":60,
    "id":"gameCanvas",
    "renderMode":0,
    "engineDir":"frameworks/cocos2d-html5",
    "modules":[
        "cocos2d",
        "extensions"
    ],
    "jsList":[
        "src/AssetsManager.js"
    ]
}



AssetsManager.js


var __failCount = 0;


var AssetsManagerLoaderScene = cc.Scene.extend({
_am:null,
_progress:null,
_percent:0,
_percentByFile:0,
isHave:false,
run:function(){
if (!cc.sys.isNative) {
this.loadGame();
return;
}


var layer = new cc.Layer();
this.addChild(layer);
this._progress = new cc.LabelTTF.create("0%", "Arial", 12);
this._progress.x = cc.winSize.width / 2;
this._progress.y = cc.winSize.height / 2 + 50;
layer.addChild(this._progress);


// android: /data/data/com.huanle.magic/files/
var storagePath = (jsb.fileUtils ? jsb.fileUtils.getWritablePath() : "./");


this._am = new jsb.AssetsManager("res/project.manifest", storagePath);
this._am.retain();


if (!this._am.getLocalManifest().isLoaded())
{
cc.log("Fail to update assets, step skipped.");
this.loadGame();
}
else
{
var that = this;
var listener = new jsb.EventListenerAssetsManager(this._am, function(event) {
switch (event.getEventCode()){
case jsb.EventAssetsManager.ERROR_NO_LOCAL_MANIFEST:
cc.log("No local manifest file found, skip assets update.");
that.loadGame();
break;
case jsb.EventAssetsManager.UPDATE_PROGRESSION:
that._percent = event.getPercent();
that._percentByFile = event.getPercentByFile();
cc.log(that._percent + "%");


var msg = event.getMessage();
if (msg) {
cc.log(msg);
this.isHave = true;
//cc.sys.cleanScript("src/app.js");
}
break;
case jsb.EventAssetsManager.ERROR_DOWNLOAD_MANIFEST:
case jsb.EventAssetsManager.ERROR_PARSE_MANIFEST:
cc.log("Fail to download manifest file, update skipped.");
that.loadGame();
break;
case jsb.EventAssetsManager.ALREADY_UP_TO_DATE:
case jsb.EventAssetsManager.UPDATE_FINISHED:
cc.log("Update finished.");
                                      //这边用了新增的清除JSB缓存跟游戏重启  都还是没效果    我用错了?
if(this.isHave){
//cc.game.restart();
cc.sys.cleanScript("src/app.js");
}else{
//that.loadGame();
}
that.loadGame();
break;
case jsb.EventAssetsManager.UPDATE_FAILED:
cc.log("Update failed. " + event.getMessage());


__failCount ++;
if (__failCount < 5)
{
that._am.downloadFailedAssets();
}
else
{
cc.log("Reach maximum fail count, exit update process");
__failCount = 0;
that.loadGame();
}
break;
case jsb.EventAssetsManager.ERROR_UPDATING:
cc.log("Asset update error: " + event.getAssetId() + ", " + event.getMessage());
that.loadGame();
break;
case jsb.EventAssetsManager.ERROR_DECOMPRESS:
cc.log(event.getMessage());
that.loadGame();
break;
default:
break;
}
});


cc.eventManager.addListener(listener, 1);
this._am.update();
cc.director.runScene(this);
}


this.schedule(this.updateProgress, 0.5);
},
loadGame:function(){
cc.loader.loadJs(["src/files.js"], function(err){
cc.loader.loadJs(jsFiles, function(err){
cc.director.runScene(new HelloWorldScene());
});
});
},
updateProgress:function(dt){
this._progress.string = "" + this._percent;
},
onExit:function(){
cc.log("AssetsManager::onExit");


this._am.release();
this._super();
}
});





files.js


var jsFiles = [
               "src/app.js",
               "src/resource.js"
               ];



app.js




var HelloWorldLayer = cc.Layer.extend({
    sprite:null,
    ctor:function () {
        //////////////////////////////
        // 1. super init first
        this._super();


        /////////////////////////////
        // 2. add a menu item with "X" image, which is clicked to quit the program
        //    you may modify it.
        // ask the window size
        var size = cc.winSize;


        // add a "close" icon to exit the progress. it's an autorelease object
        var closeItem = new cc.MenuItemImage(
            res.CloseNormal_png,
            res.CloseSelected_png,
            function () {
                cc.log("Menu is clicked!");
            }, this);
        closeItem.attr({
            x: size.width - 20,
            y: 20,
            anchorX: 0.5,
            anchorY: 0.5
        });


        var menu = new cc.Menu(closeItem);
        menu.x = 0;
        menu.y = 0;
        this.addChild(menu, 1);


        /////////////////////////////
        // 3. add your codes below...
        // add a label shows "Hello World"
        // create and initialize a label
        var helloLabel = new cc.LabelTTF("Hello World ", "Arial", 38);
        // position the label on the center of the screen
        helloLabel.x = size.width / 2;
        helloLabel.y = 0;
        // add the label as a child to this layer
        this.addChild(helloLabel, 5);


        // add "HelloWorld" splash screen"
        this.sprite = new cc.Sprite(res.HelloWorld_png);
        this.sprite.attr({
            x: size.width / 2,
            y: size.height / 2,
            scale: 0.5,
            rotation: 180
        });
        this.addChild(this.sprite, 0);


        this.sprite.runAction(
            cc.sequence(
                cc.rotateTo(2, 0),
                cc.scaleTo(2, 1, 1)
            )
        );
        helloLabel.runAction(
            cc.spawn(
                cc.moveBy(2.5, cc.p(0, size.height - 40)),
                cc.tintTo(2.5,255,125,0)
            )
        );
        return true;
    }
});


var HelloWorldScene = cc.Scene.extend({
    onEnter:function () {
        this._super();
        var layer = new HelloWorldLayer();
        this.addChild(layer);
    }
});



resource.js


var res = {
    HelloWorld_png : "res/HelloWorld.png",
    CloseNormal_png : "res/CloseNormal.png",
    CloseSelected_png : "res/CloseSelected.png"
};


var g_resources = [];
for (var i in res) {
    g_resources.push(res);
}





project.manifest


{
    "packageUrl" : "http://mut.25qp.com/res",
    "remoteManifestUrl" : "http://mut.25qp.com/res/project.manifest",
    "remoteVersionUrl" : "http://mut.25qp.com/res/version.manifest",
    "version" : "1.0.0",
    "groupVersions" : {
        "1" : "1.0.0"
    },
    "engineVersion" : "3.1",
    "searchPaths" : [
    ]
}



服务端文件


version.manifest


{
    "packageUrl" : "http://mut.25qp.com/res",
    "remoteManifestUrl" : "http://mut.25qp.com/res/project.manifest",
    "remoteVersionUrl" : "http://mut.25qp.com/res/version.manifest",
    "version" : "1.0.0",
    "groupVersions" : {
        "1" : "1.0.2"
    },
    "engineVersion" : "3.2"
}





project.manifest


{
    "packageUrl" : "http://mut.25qp.com/res",
    "remoteManifestUrl" : "http://mut.25qp.com/res/project.manifest",
    "remoteVersionUrl" : "http://mut.25qp.com/res/version.manifest",
    "version" : "1.0.0",
    "groupVersions" : {
        "1" : "1.0.2"
    },
    "engineVersion" : "3.2",
    "assets" : {
        "update1" : {
            "path" : "src/app.zip",
            "md5" : "f6bf54e5a0d42c963cc5be81bf9db6b5",
            "compressed" : true,
            "group" : "1"
        },
        "update2" : {
            "path" : "res/ui.zip",
            "md5" : "f6bf54e5a0d42c9fgcc5be81bf9db6g5",
            "compressed" : true,
            "group" : "1"
        }
    },
    "searchPaths" : [
    ]
}







咨询过panda,说试看看配置文件、searchPath搜索路径是否有问题   ,可能我理解的不够深入,  配置文件我也不懂怎么试了,searchPath的话,下载目录不是默认是优先搜索的吗?