LevelHomePropItem.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. const HomeApi = require("../net/HomeApi");
  2. const DWTool = require("../utils/DWTool");
  3. const GameModule = require("../utils/GameModule");
  4. cc.Class({
  5. extends: cc.Component,
  6. properties: {
  7. propSprite: cc.Sprite,
  8. nameText: cc.Label,
  9. },
  10. init(buildingId, propData, pickupCallback) {
  11. this.buildingId = buildingId;
  12. this.propData = propData;
  13. console.log(this.propData);
  14. DWTool.loadResSpriteFrame(`./item/${this.propData.picId}`)
  15. .then((spriteFrame) => {
  16. this.propSprite.spriteFrame = spriteFrame;
  17. }).catch((err) => {
  18. console.log(err);
  19. });
  20. this.nameText.string = `x${this.propData.count}`;
  21. this.pickupCallback = pickupCallback;
  22. },
  23. updateProp(propData) {
  24. this.nameText.string = `x${propData.count}`;
  25. },
  26. // LIFE-CYCLE CALLBACKS:
  27. onLoad () {
  28. // this.node.y = -26;
  29. // this.node.x = 150;
  30. this.node.on(cc.Node.EventType.TOUCH_END, _.debounce(() => {
  31. // 上报领取
  32. HomeApi.itemCollect(this.buildingId, () => {
  33. // 钻石
  34. if(this.propData.id == 10002) {
  35. GameModule.userInfo.updateUserRes({
  36. diamond: this.propData.count
  37. })
  38. }
  39. // 艺人券
  40. if(this.propData.id == 10003) {
  41. GameModule.userInfo.updateUserRes({
  42. ticket: this.propData.count
  43. })
  44. }
  45. this.pickupCallback();
  46. let showFrame = this.propSprite.spriteFrame;
  47. let showText = `${this.propData.name} x ${this.propData.count}`
  48. this.pickupCallback(showFrame, showText);
  49. }, (err) => {
  50. console.log(err);
  51. })
  52. }, 1000, true), this);
  53. },
  54. showAnimation() {
  55. if (this.isPlaying) { return; }
  56. this.node.stopAllActions();
  57. this.node.active = true;
  58. this.isPlayed = true;
  59. this.isPlaying = true;
  60. let jumpHeight = 175;
  61. let duration = 0.3;
  62. let animationArray = [];
  63. let moveAction1 = cc.moveBy(duration, 0, jumpHeight).easing(cc.easeCubicActionOut());
  64. let moveAction2 = cc.moveBy(duration, 0, -95).easing(cc.easeCubicActionIn());
  65. animationArray.push(moveAction1);
  66. animationArray.push(moveAction2);
  67. while(jumpHeight > 0.1) {
  68. jumpHeight = jumpHeight - (jumpHeight / 3);
  69. duration = duration - (duration / 3);
  70. let upAction = cc.moveBy(duration, 0, jumpHeight).easing(cc.easeCubicActionOut());
  71. let downAction = cc.moveBy(duration, 0, -jumpHeight).easing(cc.easeCubicActionIn());
  72. animationArray.push(upAction);
  73. animationArray.push(downAction);
  74. }
  75. let callback = cc.callFunc(() => {
  76. this.isPlaying = false;
  77. });
  78. animationArray.push(callback);
  79. this.node.runAction(cc.sequence(animationArray));
  80. },
  81. updateAnimation() {
  82. if (this.isPlaying) { return; }
  83. this.node.active = true
  84. this.node.y = -28;
  85. this.node.stopAllActions();
  86. this.isPlaying = true;
  87. let upAction = cc.moveBy(0.1, 0, 30);
  88. let downAction = cc.moveBy(0.1, 0, -30);
  89. let callback = cc.callFunc(() => {
  90. this.isPlaying = false;
  91. });
  92. this.propSprite.node.runAction(cc.sequence(upAction, downAction, callback));
  93. },
  94. });