XButton.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // Learn TypeScript:
  2. // - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
  3. // Learn Attribute:
  4. // - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
  5. // Learn life-cycle callbacks:
  6. // - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html
  7. import { App } from "./App";
  8. import { SoundManager } from "./SoundManager";
  9. const { ccclass, property } = cc._decorator;
  10. @ccclass
  11. export default class XButton extends cc.Component {
  12. onLoad() {
  13. }
  14. private onTouchDown(event) {
  15. this.playOutAnim();
  16. App.SoundManager.playEffect(SoundManager.click);
  17. }
  18. private onTouchUp(event) {
  19. this.playBackAnim();
  20. }
  21. //播放扩展动画
  22. private playOutAnim() {
  23. cc.tween(this.node).set({ scale: 1 }).to(0.2, { scale: 0.9 }).start();
  24. }
  25. //播放收缩动画
  26. private playBackAnim() {
  27. cc.tween(this.node).stop()
  28. cc.tween(this.node).to(0.2, { scale: 1 }).call(() => {
  29. cc.tween(this.node).removeSelf()
  30. }).start();
  31. }
  32. start() {
  33. this.node.on(cc.Node.EventType.TOUCH_END, this.onTouchUp.bind(this), this.node);
  34. this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchUp.bind(this), this.node);
  35. this.node.on(cc.Node.EventType.TOUCH_START, this.onTouchDown.bind(this), this.node);
  36. }
  37. // update (dt) {}
  38. }