LevelHomePropItem.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. const HomeApi = require("../net/HomeApi");
  2. const DWTool = require("../utils/DWTool");
  3. cc.Class({
  4. extends: cc.Component,
  5. properties: {
  6. propSprite: cc.Sprite,
  7. nameRichText: cc.RichText,
  8. },
  9. init(buildingId, propData, pickupCallback) {
  10. this.buildingId = buildingId;
  11. DWTool.loadResSpriteFrame(`./item/${propData.id}`)
  12. .then((spriteFrame) => {
  13. this.propSprite.spriteFrame = spriteFrame;
  14. }).catch((err) => {
  15. console.log(err);
  16. });
  17. this.nameRichText.string = `<b><color=#ffffff>X${propData.count}</c></b>`;
  18. this.pickupCallback = pickupCallback;
  19. },
  20. updateProp(propData) {
  21. console.log(propData);
  22. this.nameRichText.string = `<b><color=#ffffff>X${propData.count}</c></b>`;
  23. },
  24. // LIFE-CYCLE CALLBACKS:
  25. onLoad () {
  26. this.node.y = -26;
  27. this.node.x = 150;
  28. this.node.on(cc.Node.EventType.TOUCH_END, _.debounce(() => {
  29. HomeApi.itemCollect(this.buildingId, () => {
  30. this.pickupCallback();
  31. this.node.destroy();
  32. }, (err) => {
  33. console.log(err);
  34. })
  35. }, 1000, true), this);
  36. },
  37. showAnimation() {
  38. if (this.isPlaying) { return; }
  39. this.node.stopAllActions();
  40. this.node.active = true;
  41. this.isPlayed = true;
  42. this.isPlaying = true;
  43. let jumpHeight = 175;
  44. let duration = 0.3;
  45. let animationArray = [];
  46. let moveAction1 = cc.moveBy(duration, 0, jumpHeight).easing(cc.easeCubicActionOut());
  47. let moveAction2 = cc.moveBy(duration, 0, -95).easing(cc.easeCubicActionIn());
  48. animationArray.push(moveAction1);
  49. animationArray.push(moveAction2);
  50. while(jumpHeight > 0.1) {
  51. jumpHeight = jumpHeight - (jumpHeight / 3);
  52. duration = duration - (duration / 3);
  53. let upAction = cc.moveBy(duration, 0, jumpHeight).easing(cc.easeCubicActionOut());
  54. let downAction = cc.moveBy(duration, 0, -jumpHeight).easing(cc.easeCubicActionIn());
  55. animationArray.push(upAction);
  56. animationArray.push(downAction);
  57. }
  58. let callback = cc.callFunc(() => {
  59. this.isPlaying = false;
  60. });
  61. animationArray.push(callback);
  62. this.node.runAction(cc.sequence(animationArray));
  63. },
  64. updateAnimation() {
  65. if (this.isPlaying) { return; }
  66. this.node.active = true
  67. this.node.y = -28;
  68. this.node.stopAllActions();
  69. this.isPlaying = true;
  70. let upAction = cc.moveBy(0.1, 0, 30);
  71. let downAction = cc.moveBy(0.1, 0, -30);
  72. let callback = cc.callFunc(() => {
  73. this.isPlaying = false;
  74. });
  75. this.coinNode.runAction(cc.sequence(upAction, downAction, callback));
  76. },
  77. });