1
0

ArtistMan.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. const ArtistManager = require('../utils/ArtistManager');
  2. cc.Class({
  3. extends: cc.Component,
  4. properties: {
  5. nickLabel: cc.Label,
  6. runMan: cc.Node,
  7. artistSkeleton: sp.Skeleton,
  8. direction: {
  9. get: function () {
  10. if (!this._direction) {
  11. this._direction = 1;
  12. }
  13. return this._direction;
  14. },
  15. set: function(value) {
  16. this._direction = value;
  17. }
  18. },
  19. _accSpeed: 50,
  20. _maxX: 270,
  21. },
  22. init(artistData, direction) {
  23. this.artistData = artistData;
  24. if (!this.isAlready) {
  25. this.direction = direction;
  26. this.node.x = Math.random()*(Math.random()>0.5?1:-1) * this.artistRandom(-150, 150);
  27. this.walkDistance = this.artistRandom(250, 350);
  28. }
  29. this.walkAnimation = [];
  30. this.standAnimation = [];
  31. this.isStand = false;
  32. this.artistSkeleton.clearTracks();
  33. let name = 60000 + parseInt(this.artistData.starId);
  34. this.starName = name;
  35. // 根据明星id来加载不同骨骼
  36. ArtistManager.loadStar(this.artistData.starId)
  37. .then((skeletonData) => {
  38. //判断当前明星的名字是否与骨骼的name一样,因资源为异步加载,可能会出现明星显示不对的情况
  39. if (this.starName == skeletonData._name) {
  40. this.artistSkeleton.skeletonData = skeletonData;
  41. var sd = skeletonData.getRuntimeData(true);
  42. if (sd) {
  43. /**
  44. * walk1为普通行走,如果有walk2则为特殊行走动作
  45. * stand为特殊站立动作
  46. */
  47. this.walkAnimation = sd.animations.filter(item => item.name.indexOf('walk') != -1).map(animation => animation.name) || [];
  48. this.standAnimation = sd.animations.filter(item => item.name.indexOf('stand') != -1).map(animation => animation.name) || [];
  49. }
  50. this.artistSkeleton.setAnimation(0, this.walkAnimation[0], true);
  51. this.isAlready = true;
  52. }
  53. }).catch((err) => {
  54. console.log(err);
  55. });
  56. },
  57. onLoad () {
  58. this.isAlready = false;
  59. this.node.y = 20;
  60. this.isStand = false;
  61. this.totalDistance = 0;
  62. },
  63. artistRandom(start, end){
  64. return Math.floor(Math.random() * (end - start) + start);
  65. },
  66. update (dt) {
  67. if (this.isStand) { return; }
  68. // 每一帧移动多少距离, 这是固定的
  69. let distance = this._accSpeed * dt;
  70. // console.log('distance ' + distance);
  71. this.totalDistance += distance;
  72. //骨骼移动到一定距离后做出特定动作
  73. if (Math.floor(this.totalDistance) >= this.walkDistance) {
  74. this.totalDistance = 0;
  75. this.walkDistance = this.artistRandom(250, 350);
  76. if (this.standAnimation.length > 0) { // stand
  77. this.isStand = true;
  78. //特殊明星含有文字动画处理
  79. if (this.direction < 0 && this.standAnimation.length > 1) {
  80. this.runMan.scaleX = 1;
  81. this.artistSkeleton.setAnimation(0, this.standAnimation[1], false);
  82. } else {
  83. this.artistSkeleton.setAnimation(0, this.standAnimation[0], false);
  84. }
  85. this.artistSkeleton.setCompleteListener(() => {
  86. this.isStand = false;
  87. this.artistSkeleton.completeListener = null;
  88. this.artistSkeleton.setAnimation(0, this.walkAnimation[0], true);
  89. });
  90. return;
  91. } else { // walk
  92. if (this.walkAnimation.length > 0) {
  93. let walk = Math.floor(Math.random() * this.walkAnimation.length);
  94. if (walk === 0) {
  95. this.artistSkeleton.setAnimation(0, this.walkAnimation[walk], true);
  96. } else {
  97. if (walk < this.walkAnimation.length) {
  98. this.artistSkeleton.setAnimation(0, this.walkAnimation[walk], false);
  99. this.artistSkeleton.setCompleteListener(() => {
  100. this.artistSkeleton.setAnimation(0, this.walkAnimation[0], true);
  101. this.artistSkeleton.completeListener = null;
  102. });
  103. }
  104. }
  105. }
  106. }
  107. }
  108. // 判断方向小于0为从左向右走
  109. if (this.direction < 0) {
  110. this.runMan.scaleX = -1;
  111. if ((this.node.x + distance) > this._maxX) {
  112. this.direction = 1;
  113. this.artistSkeleton.setAnimation(0, this.walkAnimation[0], true);
  114. } else {
  115. this.node.x = this.node.x + distance;
  116. }
  117. } else {
  118. this.runMan.scaleX = 1;
  119. if ((this.node.x - distance) < -this._maxX) {
  120. this.direction = -1;
  121. this.artistSkeleton.setAnimation(0, this.walkAnimation[0], true);
  122. } else {
  123. this.node.x = this.node.x - distance;
  124. }
  125. }
  126. },
  127. });