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; if (this.runMan) { } } }, _accSpeed: 50, _maxX: 270, }, init(artistData) { this.artistData = artistData; this.nickLabel.string = artistData.nick; this.animList = [] // this.artistData.jobId 固定为2吧, 暂时只有舞者的骨骼动画 ArtistManager.loadArtist(false, this.artistData.gender, 2) .then((skeletonData) => { this.artistSkeleton.skeletonData = skeletonData; this.artistSkeleton.setAnimation(0, 'walk1', true); }).catch((err) => { console.log(err); }); }, onLoad () { this.node.y = 20; this.isStand = false; this.totalDistance = 0; }, update (dt) { if (this.isStand) { return; } // 每一帧移动多少距离, 这是固定的 let distance = this._accSpeed * dt; this.totalDistance += distance; if (Math.floor(this.totalDistance) == 340) { this.totalDistance = 0; let random = Math.floor(Math.random() * 2); if (random === 0) { // walk let animKeyArray = ['walk1', 'walk2']; let walk = Math.floor(Math.random() * 2); if (walk === 0) { this.artistSkeleton.setAnimation(0, animKeyArray[walk], true); } else { this.artistSkeleton.setAnimation(0, animKeyArray[walk], false); this.artistSkeleton.setCompleteListener(() => { this.artistSkeleton.setAnimation(0, 'walk1', true); this.artistSkeleton.completeListener = null; }); } } else { // stand let animKeyArray = ['stand1', 'stand2']; let stand = Math.floor(Math.random() * 2); this.artistSkeleton.setAnimation(0, animKeyArray[stand], false); this.artistSkeleton.setCompleteListener(() => { this.isStand = false; this.artistSkeleton.completeListener = null; this.artistSkeleton.setAnimation(0, 'walk1', true); }); this.isStand = true; return; } } // 判断方向 if (this.direction < 0) { this.runMan.scaleX = -1; if ((this.node.x + distance) > this._maxX) { this.direction = 1; this.artistSkeleton.setAnimation(0, 'walk1', true); } else { this.node.x = this.node.x + distance; } } else { this.runMan.scaleX = 1; if ((this.node.x - distance) < -this._maxX) { this.direction = -1; this.artistSkeleton.setAnimation(0, 'walk1', true); } else { this.node.x = this.node.x - distance; } } }, });