123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- 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;
- // 根据明星id来加载不同骨骼
- ArtistManager.loadStar(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;
- }
- }).catch((err) => {
- console.log(err);
- });
- },
- 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;
- 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;
- this.artistSkeleton.setAnimation(0, this.walkAnimation[0], true);
- } else {
- this.node.x = this.node.x - distance;
- }
- }
- },
- });
|