Cocos Creator 大厅+子游戏,从入门到进阶!
COCOS
共 12084字,需浏览 25分钟
· 2021-05-08
01
目录
例子展示
手撸工程
2.1 同项目 Bundle,动态加载
2.2 跨项目 Bundle,大厅+子游戏
2.3 跨项目 Bundle,代码互调
踩坑总结
02
例子
![](https://filescdn.proginn.com/4d2277e964d2536e5ce6bbefb75f312c/640d2f9d6c641adbfd3bc6582c1c035c.webp)
![](https://filescdn.proginn.com/365eb1b0321ee469d5c82313570fd93a/407bcdc68c3cd951417c507fdc0b5255.webp)
![](https://filescdn.proginn.com/04ee8826faae0cfef340a49397fa15b3/285406bdd92611ec9420e9c50207b053.webp)
03
手撸详解
![](https://filescdn.proginn.com/b5d2dd7e012a79f26ef8ce457a51eec3/9118e403fbf640316887db5a8c0a6878.webp)
![](https://filescdn.proginn.com/8f7bd9da63b9e39b04850913fce9f00a/2c9f0c36baa3894dac3897b8c3a9d50f.webp)
![](https://filescdn.proginn.com/1f2bede2d950080643fdc55e9773ea3b/b99ad65d2c0df50a3839cd99258d4345.webp)
onClickSceneTo(){
cc.director.loadScene('aaa');
}
![](https://filescdn.proginn.com/43babdca82d1f903ddbf26b3368b87e0/2e4221fa6c611e5044688af383eea059.webp)
![](https://filescdn.proginn.com/229f199cbf75b4a4e24b874efe162e4c/5ebc8ade82cba01116d4eb3f0ba0a7fc.webp)
![](https://filescdn.proginn.com/6a5ca5308812f141c6d4fad151d69f8b/f1d139986ed4c30b70b16f5276d1c421.webp)
onClickLoad(){
cc.assetManager.loadBundle('aaa',(err,bundle)=>{
if(!err){
this.progressBar.progress = 1;
}
});
}
![](https://filescdn.proginn.com/a174831f4d41fa415d111e543fc2f575/c01595c07a05d10d39699e318c0585a1.webp)
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);
}
});
}
}
![](https://filescdn.proginn.com/ba4075bcc61b6d524b84efc65208981f/4922dd5389c55009ce2fa0bc54673de9.webp)
![](https://filescdn.proginn.com/b6b4ac28736f838ed7d2ffaed98f9296/3226677979503d222c1128bc95e3954f.webp)
![](https://filescdn.proginn.com/363670fee8c3cb5541a3e765f895afcb/2df896c2feb194317e4eaa658f4dd10f.webp)
![](https://filescdn.proginn.com/7a18afea4922f56a2e8015e777a1c610/3dc302b0bc971bb79496cef58da9b5fa.webp)
![](https://filescdn.proginn.com/3449ae43dcb6eb316ae097958dd2469b/48d338bc63437dffe72ad308097d1675.webp)
![](https://filescdn.proginn.com/62ad70d06c84e009b30bb0e387b5757d/d302ae90c49d83ce7a7c0d3343a2df9b.webp)
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));
}
});
}
}
![](https://filescdn.proginn.com/77e2f2492b6385c8015c1c0222bacdb1/a5ec53f0ab4fc20a1ec7c5e25e8a6c0e.webp)
![](https://filescdn.proginn.com/2f655c1180d0174480c63dafcffdd23e/f5bce242550cbfde340b016ab0020f6c.webp)
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;
}
}
![](https://filescdn.proginn.com/758408906e23b91f8772da52c40bcc72/9f78d019ee4a0dd74560643e8b57f152.webp)
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); }}
![](https://filescdn.proginn.com/21ed0d3f77c8169507981649a6f87c03/cadf7c8424e02b5817a1859d4a0d815a.webp)
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");
}
}
![](https://filescdn.proginn.com/2fc907caf7ab143a2ad68615cfaf1bea/a209d1f7a51e944a16578c3417ca6b31.webp)
![](https://filescdn.proginn.com/bf6dfb1434d22e99bf1aa3744df47121/9e8565be0e093263b69335979e2fbf52.webp)
04
注意事项
第一是各个 Bundle 中的代码中不要有一样的类名,或者全局变量名,这样的代码会在读取 Bundle 后直接报重名错误。
第二是 Bundle 包代码尽量不要互相引用。如果你的业务需求必须这样做,应该用设置载入优先级解决。但只能解决在同一个项目中的 Bundle 读取,跨项目使用还是得自己控制先后顺序。建议可以把通用代码整合成一个包,在开始的时候读下来。
第三是跨 Bundle 的资源尽量互相保持独立,对象管理只是一方面,关键是有一些不可预期的奇怪错误,往往会从缓存和释放的地方出问题。
05
总结
![](https://filescdn.proginn.com/0f25ea189526a6e47561eb9e2f5cd1b9/a251ca37cd2175c205ff55ce71c75193.webp)
![](https://filescdn.proginn.com/929cb6fddeaa0b07db869470e881b762/a3b4db00247d380de60bf47bfacfe0a6.webp)
![](https://filescdn.proginn.com/274fd0e6e1ce2e68624417a45efa6dd3/d5933e6825d81526b824f089b9ac07e0.webp)
评论