Cocos creator 大廳子游戲和熱更新
阿新 • • 發佈:2019-01-13
工作中,需要把cocos creator建立的多個遊戲適配到Android和ios應用中,經過調研,可以利用大廳子游戲模式實現。大廳本身作為一個遊戲工程,可以有載入頁面,和熱載入子游戲。
熱更新:
https://www.jianshu.com/p/9fc813fe9e4c
大廳子游戲:
https://www.jianshu.com/p/fe54ca980384
如何動態載入和更新子游戲:
自從jsb 3.0以來,可以用反射呼叫Android或者ios的程式碼:
const SubgameManager = require('SubgameManager');
cc.Class({
extends: cc.Component,
properties: {
downloadBtn: {
default: null,
type: cc.Node
},
label: {
default: null,
type: cc.Label
},
// defaults, set visually when attaching this script to the Canvas
text: 'Hello, World!'
},
// use this for initialization
onLoad: function () {
var name = 'subgame';
if (cc.sys.OS_ANDROID == cc.sys.os) {
name = jsb.reflection.callStaticMethod("org/cocos2dx/javascript/GameNameProvider", "getName", "()Ljava/lang/String;");
console. log("OS_ANDROID platform provides: " + name);
}
if (cc.sys.OS_IOS == cc.sys.os) {
name = jsb.reflection.callStaticMethod("GameNameProvider", "getName");
console.log("OS_IOS platform provides: " + name);
}
//判斷子游戲有沒有下載
if (SubgameManager.isSubgameDownLoad(name)) {
//已下載,判斷是否需要更新
SubgameManager.needUpdateSubgame(name, (success) => {
if (success) {
this.label.string = "子游戲需要更新";
console.log("子游戲需要更新");
} else {
this.label.string = "子游戲不需要更新";
console.log("子游戲不需要更新");
}
}, () => {
console.log('出錯了');
});
} else {
console.log("子游戲未下載");
this.label.string = "子游戲未下載";
}
this.downloadBtn.on('click', () => {
//下載子游戲/更新子游戲
console.log("downloadBtn clicked");
SubgameManager.downloadSubgame(name, (progress) => {
if (isNaN(progress)) {
progress = 0;
}
this.label.string = "資源下載中 " + parseInt(progress * 100) + "%";
console.log(this.label.string);
}, function(success) {
if (success) {
SubgameManager.enterSubgame(name);
console.log("進入子游戲");
} else {
console.log('下載失敗');
}
});
}, this);
},
// called every frame
update: function (dt) {
},
});
Android程式碼:
package org.cocos2dx.javascript;
public class GameNameProvider {
public static String getName() {
return "subgame";
}
}
iOS程式碼:
#import <Foundation/Foundation.h>
@interface GameNameProvider:NSObject {
}
+ (NSString *)getName;
@end
#import "GameNameProvider.h"
#import <Foundation/Foundation.h>
@implementation GameNameProvider
// request login
+ (NSString *) getName {
return @"subgame";
}
@end