const ArtistManager = require('../utils/ArtistManager'); cc.Class({ extends: cc.Component, properties: { nickLabel: cc.Label, runMan: cc.Node, artistSkeleton: sp.Skeleton, direction: { get: function () { if (!this._direction) { this._direction = 1; } return this._direction; }, set: function(value) { this._direction = value; } }, _accSpeed: 50, _maxX: 270, }, init(artistData, direction) { this.artistData = artistData; if (!this.isAlready) { this.direction = direction; this.node.x = Math.random()*(Math.random()>0.5?1:-1) * this.artistRandom(-150, 150); this.walkDistance = this.artistRandom(250, 350); } this.walkAnimation = []; this.standAnimation = []; this.isStand = false; this.artistSkeleton.clearTracks(); let name = 60000 + parseInt(this.artistData.starId); this.starName = name; this.node.active = false; // 根据明星id来加载不同骨骼 ArtistManager.loadStarSpine(this.artistData.starId) .then((skeletonData) => { //判断当前明星的名字是否与骨骼的name一样,因资源为异步加载,可能会出现明星显示不对的情况 if (this.starName == skeletonData._name) { this.artistSkeleton.skeletonData = skeletonData; var sd = skeletonData.getRuntimeData(true); if (sd) { /** * walk1为普通行走,如果有walk2则为特殊行走动作 * stand为特殊站立动作 */ this.walkAnimation = sd.animations.filter(item => item.name.indexOf('walk') != -1).map(animation => animation.name) || []; this.standAnimation = sd.animations.filter(item => item.name.indexOf('stand') != -1).map(animation => animation.name) || []; this.artistSkeleton.setAnimation(0, this.walkAnimation[0], true); this.isAlready = true; this.node.active = true; } else { this.isAlready = false; this.node.active = false; } } }).catch((err) => { console.log(err); this.isAlready = false; this.node.active = false; }); }, onLoad () { this.isAlready = false; this.node.y = 20; this.isStand = false; this.totalDistance = 0; }, artistRandom(start, end){ return Math.floor(Math.random() * (end - start) + start); }, update (dt) { if (this.isStand) { return; } // 每一帧移动多少距离, 这是固定的 let distance = this._accSpeed * dt; // console.log('distance ' + distance); this.totalDistance += distance; //骨骼移动到一定距离后做出特定动作 if (Math.floor(this.totalDistance) >= this.walkDistance) { this.totalDistance = 0; this.walkDistance = this.artistRandom(250, 350); if (this.standAnimation.length > 0) { // stand this.isStand = true; //特殊明星含有文字动画处理 if (this.direction < 0 && this.standAnimation.length > 1) { this.runMan.scaleX = 1; this.artistSkeleton.setAnimation(0, this.standAnimation[1], false); } else { this.artistSkeleton.setAnimation(0, this.standAnimation[0], false); } this.artistSkeleton.setCompleteListener(() => { this.isStand = false; this.artistSkeleton.completeListener = null; this.artistSkeleton.setAnimation(0, this.walkAnimation[0], true); }); return; } else { // walk if (this.walkAnimation.length > 0) { let walk = Math.floor(Math.random() * this.walkAnimation.length); if (walk === 0) { this.artistSkeleton.setAnimation(0, this.walkAnimation[walk], true); } else { if (walk < this.walkAnimation.length) { this.artistSkeleton.setAnimation(0, this.walkAnimation[walk], false); this.artistSkeleton.setCompleteListener(() => { this.artistSkeleton.setAnimation(0, this.walkAnimation[0], true); this.artistSkeleton.completeListener = null; }); } } } } } // 判断方向小于0为从左向右走 if (this.direction < 0) { this.runMan.scaleX = -1; if ((this.node.x + distance) > this._maxX) { this.direction = 1; if (this.walkAnimation.length > 0) { this.artistSkeleton.setAnimation(0, this.walkAnimation[0], true); } } else { this.node.x = this.node.x + distance; } } else { this.runMan.scaleX = 1; if ((this.node.x - distance) < -this._maxX) { this.direction = -1; if (this.walkAnimation.length > 0) { this.artistSkeleton.setAnimation(0, this.walkAnimation[0], true); } } else { this.node.x = this.node.x - distance; } } }, });