cocos android 热更新,Cocos creator 大厅子游戏和热更新
工作中,需要把cocos creator創(chuàng)建的多個(gè)游戲適配到Android和ios應(yīng)用中,經(jīng)過(guò)調(diào)研,可以利用大廳子游戲模式實(shí)現(xiàn)。大廳本身作為一個(gè)游戲工程,可以有加載頁(yè)面,和熱加載子游戲。
如何動(dòng)態(tài)加載和更新子游戲:
自從jsb 3.0以來(lái),可以用反射調(diào)用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);
}
//判斷子游戲有沒(méi)有下載
if (SubgameManager.isSubgameDownLoad(name)) {
//已下載,判斷是否需要更新
SubgameManager.needUpdateSubgame(name, (success) => {
if (success) {
this.label.string = "子游戲需要更新";
console.log("子游戲需要更新");
} else {
this.label.string = "子游戲不需要更新";
console.log("子游戲不需要更新");
}
}, () => {
console.log('出錯(cuò)了');
});
} 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("進(jìn)入子游戲");
} 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
@interface GameNameProvider:NSObject {
}
+ (NSString *)getName;
@end
#import "GameNameProvider.h"
#import
@implementation GameNameProvider
// request login
+ (NSString *) getName {
return @"subgame";
}
@end
總結(jié)
以上是生活随笔為你收集整理的cocos android 热更新,Cocos creator 大厅子游戏和热更新的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: php项目index页面空白,如何解决v
- 下一篇: dns 320 linux,linux