ArtistMan.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. if (this.runMan) {
  18. }
  19. }
  20. },
  21. _accSpeed: 50,
  22. _maxX: 270,
  23. },
  24. init(artistData) {
  25. this.artistData = artistData;
  26. this.nickLabel.string = artistData.nick;
  27. this.animList = []
  28. // this.artistData.jobId 固定为2吧, 暂时只有舞者的骨骼动画
  29. ArtistManager.loadArtist(false, this.artistData.gender, 2)
  30. .then((skeletonData) => {
  31. this.artistSkeleton.skeletonData = skeletonData;
  32. this.artistSkeleton.setAnimation(0, 'walk1', true);
  33. }).catch((err) => {
  34. console.log(err);
  35. });
  36. },
  37. onLoad () {
  38. this.node.y = 20;
  39. this.isStand = false;
  40. this.totalDistance = 0;
  41. },
  42. update (dt) {
  43. if (this.isStand) { return; }
  44. // 每一帧移动多少距离, 这是固定的
  45. let distance = this._accSpeed * dt;
  46. this.totalDistance += distance;
  47. if (Math.floor(this.totalDistance) == 340) {
  48. this.totalDistance = 0;
  49. let random = Math.floor(Math.random() * 2);
  50. if (random === 0) { // walk
  51. let animKeyArray = ['walk1', 'walk2'];
  52. let walk = Math.floor(Math.random() * 2);
  53. if (walk === 0) {
  54. this.artistSkeleton.setAnimation(0, animKeyArray[walk], true);
  55. } else {
  56. this.artistSkeleton.setAnimation(0, animKeyArray[walk], false);
  57. this.artistSkeleton.setCompleteListener(() => {
  58. this.artistSkeleton.setAnimation(0, 'walk1', true);
  59. this.artistSkeleton.completeListener = null;
  60. });
  61. }
  62. } else { // stand
  63. let animKeyArray = ['stand1', 'stand2'];
  64. let stand = Math.floor(Math.random() * 2);
  65. this.artistSkeleton.setAnimation(0, animKeyArray[stand], false);
  66. this.artistSkeleton.setCompleteListener(() => {
  67. this.isStand = false;
  68. this.artistSkeleton.completeListener = null;
  69. this.artistSkeleton.setAnimation(0, 'walk1', true);
  70. });
  71. this.isStand = true;
  72. return;
  73. }
  74. }
  75. // 判断方向
  76. if (this.direction < 0) {
  77. this.runMan.scaleX = -1;
  78. if ((this.node.x + distance) > this._maxX) {
  79. this.direction = 1;
  80. this.artistSkeleton.setAnimation(0, 'walk1', true);
  81. } else {
  82. this.node.x = this.node.x + distance;
  83. }
  84. } else {
  85. this.runMan.scaleX = 1;
  86. if ((this.node.x - distance) < -this._maxX) {
  87. this.direction = -1;
  88. this.artistSkeleton.setAnimation(0, 'walk1', true);
  89. } else {
  90. this.node.x = this.node.x - distance;
  91. }
  92. }
  93. },
  94. });