LevelHomePropItem.js 3.8 KB

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