cocosCreator
Posted chzh999
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了cocosCreator相关的知识,希望对你有一定的参考价值。
在新公司开始做客户端了,之前做客户端的少,大多数时间做的是服务器。看了两天的cocosCreator,做一些总结,把遇到的问题和自己不懂的地方记录下来。
1 //获取龙骨动画 2 let armatureDisplay = this.getComponent(dragonBones.ArmatureDisplay); 3 let armature = armatureDisplay.armature(); 4 if (armature) { 5 this._armature = armature; 6 //取得这个Armature动画列表中的第一个动画的名字 7 var curAnimationName: string = armature.animation.animationList[0]; 8 var animation: dragonBones.Animation = armature.animation; 9 // animation.gotoAndStopByProgress(curAnimationName, 0.5); 10 } 11 12 if (this._armature) { 13 var animation: dragonBones.Animation = this._armature.animation; 14 animation.gotoAndStopByProgress(‘clock‘, usedTime / this._duration); 15 }
1 //基本的类格式,导入文件,添加属性 2 import * as Common from "../Common"; 3 import { default as DataMng } from "../Data/DataMng"; 4 5 const { ccclass, property } = cc._decorator; 6 7 @ccclass 8 export default class MenuClass extends cc.Component { 9 //属性的声明 10 @property(cc.Label) 11 label: cc.Label = null; 12 13 @property(cc.Node) 14 BeginButton: cc.Node = null; 15 16 @property(cc.Node) 17 RellNode: cc.Node = null; 18 19 // LIFE-CYCLE CALLBACKS: 20 21 _isShowBeginBtn = true; 22 23 onLoad() { 24 25 this.registerDragonBoneEvent(); 26 this.initGame(); 27 } 28 29 start() { 30 31 } 32 33 initGame() { 34 // 获取用户信息 35 let userInfo: Common.IUserInfo = { 36 userId: 1, 37 userName: ‘全胜‘, 38 userRole: 1 39 }; 40 41 let gameType = 0; 42 43 if (gameType == Common.EGameType.courseware) { 44 if (userInfo && userInfo.userRole == Common.EUserRole.student) { 45 this._isShowBeginBtn = false; 46 } 47 } 48 // this.showBeginBtn(); 49 DataMng.loadBalls() 50 } 51 52 loadMain() { 53 // 加载游戏主界面,实现界面的跳转 54 cc.director.loadScene(‘game‘, () => { 55 console.log("主界面加载完成"); 56 }) 57 } 58 59 registerDragonBoneEvent() { 60 let self = this; 61 if (!this.RellNode) return; 62 let armatureDisplay = this.RellNode.getComponent(dragonBones.ArmatureDisplay); 63 64 console.log({ armatureDisplay }); 65 armatureDisplay.playTimes = 0; 66 67 function animationEventHandler(event): void { 68 console.log({ event }); 69 self.showBeginBtn(); 70 } 71 if (armatureDisplay) { 72 armatureDisplay.addEventListener(dragonBones.EventObject.COMPLETE, animationEventHandler) 73 } 74 } 75 76 showBeginBtn() { 77 if (!this._isShowBeginBtn) { 78 return; 79 } 80 console.log(‘显示开始按钮‘); 81 // let action = cc.easeElasticIn(1); 82 // cc.show();
//执行动画 83 let sq = cc.sequence(cc.delayTime(3), cc.fadeTo(0.5, 255)); 84 this.BeginButton.runAction(sq); 85 this.BeginButton.active = true; 86 } 87 88 // update (dt) {} 89 }
//加载资源,返回Promise对象 function loadResource(url: string, type: any): Promise<any> { return new Promise((resolved, rejected) => { cc.loader.loadRes(url, type, (err, resource) => { if (err) { rejected(err) } resolved(resource) }) }) }
1 //加载资源 2 function loadBalls(): Promise<any> { 3 return loadResource(‘data/balls‘, cc.JsonAsset) 4 .then((data) => { 5 let list: IBallInfo[] = data.json; 6 let allPromise: Promise<any>[] = []; 7 for (let index = 0; index < list.length; index++) { 8 const element = list[index]; 9 balls[element.id] = element; 10 let promise = loadResource(`game/balls/${element.subject}/${element.icon}`, cc.SpriteFrame) 11 .then((sf) => { 12 element.ballsf = sf; 13 return loadResource(`game/pans/${element.subject}/${element.icon}`, cc.SpriteFrame) 14 .then((panSf) => { 15 element.pansf = panSf; 16 }); 17 }) 18 allPromise.push(promise); 19 } 20 return Promise.all(allPromise) 21 .then(() => { 22 console.log(‘--balls:--‘, balls) 23 return list; 24 }) 25 }) 26 }
以上是关于cocosCreator的主要内容,如果未能解决你的问题,请参考以下文章