Cocos Creator 大厅+子游戏,从入门到进阶!
COCOS
共 12084字,需浏览 25分钟
·
2021-05-08 07:43
01
目录
例子展示
手撸工程
2.1 同项目 Bundle,动态加载
2.2 跨项目 Bundle,大厅+子游戏
2.3 跨项目 Bundle,代码互调
踩坑总结
02
例子
03
手撸详解
onClickSceneTo(){
cc.director.loadScene('aaa');
}
onClickLoad(){
cc.assetManager.loadBundle('aaa',(err,bundle)=>{
if(!err){
this.progressBar.progress = 1;
}
});
}
const {ccclass, property} = cc._decorator;
export default class MainScript1 extends cc.Component {
progressBar:cc.ProgressBar = null;
target1:cc.Node = null;
target2:cc.Node = null;
private _bundle:cc.AssetManager.Bundle;
start () {
this.progressBar.progress = 0;
this.target1.active = this.target2.active = false;
}
onClickLoad(){
cc.assetManager.loadBundle('aaa',(err,bundle)=>{
if(!err){
this._bundle = bundle;
this.progressBar.progress = 1;
this.target1.active = this.target2.active = true;
}
});
}
onClickSceneTo(){
cc.director.loadScene('aaa');
}
onClickLoadPrefab(s:cc.Event.EventTouch){
this._bundle.load('res/spineboy',cc.Prefab,(err,asset:cc.Prefab)=>{
if(!err){
this.target1.addChild(cc.instantiate(asset));
s.currentTarget.active = false;
}
});
}
onClickLoadSpriteFrame(s:cc.Event.EventTouch){
this._bundle.load('res/button',cc.Texture2D,(err,tex:cc.Texture2D)=>{
if(!err){
s.currentTarget.active = false;
const node = new cc.Node();
node.addComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(tex);
this.target2.addChild(node);
}
});
}
}
const {ccclass, property} = cc._decorator;
export default class MainScript2 extends cc.Component {
progressBar:cc.ProgressBar = null;
target1:cc.Node = null;
private _bundle:cc.AssetManager.Bundle;
start () {
this.progressBar.progress = 0;
this.target1.active = false;
}
onClickLoad(){
const options = {
version:"08f26",
onFileProgress:(n,t)=>{
this.progressBar.progress = n / t;
}
}
cc.assetManager.loadBundle('http://127.0.0.1:8080/Game1',
options,
(err,bundle)=>{
if(!err){
this._bundle = bundle;
this.target1.active = true;
}
});
}
onClickSceneTo(e:cc.Event.EventTouch){
e.currentTarget.active = false;
this._bundle.load("prefab/Game1Stage",cc.Prefab,(err,asset:cc.Prefab)=>{
if(!err){
this.target1.addChild(cc.instantiate(asset));
}
});
}
}
export interface IMainController {
outString(str: string): void;
}
import { IMainController } from "./IMainController";
const {ccclass, property} = cc._decorator;
export default class MainControllerScript extends cc.Component implements IMainController {
(cc.Label)
outLabel:cc.Label = null;
outString(str: string): void {
this.outLabel.string = str;
}
}
declare interface IMainController {
outString(str: string): void;
}
declare interface Window{
debugMainCtrl:IMainController;
}
class DebugMainController implements IMainController{
outString(str: string): void {
console.warn('Method is debug,str is ' + str);
}
}
if(!window.debugMainCtrl){
window.debugMainCtrl = new DebugMainController();
}
const {ccclass, property} = cc._decorator; default class Game2Logic extends cc.Component { actor:dragonBones.ArmatureDisplay = null; start () { this.node.on(cc.Node.EventType.TOUCH_END,this.onTouchEnd,this); this.node.on("ActorAnimationPlay",this.onActorAnimationPlay,this); } onDestroy(){ this.node.off(cc.Node.EventType.TOUCH_END,this.onTouchEnd,this); this.node.off("ActorAnimationPlay",this.onActorAnimationPlay,this); } private onActorAnimationPlay(aniname:string){ this.actor.playAnimation(aniname,-1); } private index = 0; private onTouchEnd(){ const arr = this.actor.getAnimationNames("ubbie"); const aniName = arr[this.index % arr.length]; this.node.emit("ActorAnimationPlay",aniName); this.index += 1; let mainCtrl:IMainController = cc.director.getScene().getComponentInChildren('MainControllerScript'); if(!mainCtrl){ mainCtrl = window.debugMainCtrl; } mainCtrl.outString(aniName); }}
const {ccclass, property} = cc._decorator;
export default class MainScript3 extends cc.Component {
progressBar:cc.ProgressBar = null;
target1:cc.Node = null;
private _bundle:cc.AssetManager.Bundle;
start () {
this.progressBar.progress = 0;
this.target1.active = false;
}
onClickLoad(){
const options = {
version:"78969",
onFileProgress:(n,t)=>{
this.progressBar.progress = n / t;
}
}
cc.assetManager.loadBundle('http://127.0.0.1:8080/Game2',
options,
(err,bundle)=>{
if(!err){
this._bundle = bundle;
this.target1.active = true;
}
});
}
onClickSceneTo(e:cc.Event.EventTouch){
// cc.director.loadScene('Game1');
e.currentTarget.active = false;
this._bundle.load("prefab/Game2Stage",cc.Prefab,(err,asset:cc.Prefab)=>{
if(!err){
this.target1.addChild(this._gameStage = cc.instantiate(asset));
}
});
}
private _gameStage:cc.Node;
onClickActonWalk(){
this._gameStage.emit("ActorAnimationPlay","walk");
}
onClickActonStand(){
this._gameStage.emit("ActorAnimationPlay","stand");
}
}
04
注意事项
第一是各个 Bundle 中的代码中不要有一样的类名,或者全局变量名,这样的代码会在读取 Bundle 后直接报重名错误。
第二是 Bundle 包代码尽量不要互相引用。如果你的业务需求必须这样做,应该用设置载入优先级解决。但只能解决在同一个项目中的 Bundle 读取,跨项目使用还是得自己控制先后顺序。建议可以把通用代码整合成一个包,在开始的时候读下来。
第三是跨 Bundle 的资源尽量互相保持独立,对象管理只是一方面,关键是有一些不可预期的奇怪错误,往往会从缓存和释放的地方出问题。
05
总结
评论