QMoveAction.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import QEasing, { EaseType } from "./QEasing";
  2. const { ccclass, property } = cc._decorator;
  3. @ccclass
  4. export default class QMoveAction extends QEasing {
  5. @property()
  6. delay: number = 0;
  7. @property(cc.Vec2)
  8. delPos: cc.Vec2 = cc.Vec2.ZERO;
  9. @property()
  10. duration: number = 0;
  11. @property()
  12. interval: number = 0;
  13. @property()
  14. loop: boolean = false;
  15. @property()
  16. revert: boolean = false;
  17. private _tween: cc.Tween = null;
  18. private _originPos: cc.Vec3 = cc.Vec3.ZERO;
  19. onLoad() {
  20. this._originPos = cc.v3(this.node.x, this.node.y);
  21. this.node.position = cc.v3(this._originPos.x - this.delPos.x, this._originPos.y - this.delPos.y);
  22. let widget: cc.Widget = this.getComponent(cc.Widget);
  23. if (widget) {
  24. widget.enabled = false;
  25. }
  26. let delayTween: cc.Tween = cc.tween().delay(this.delay);
  27. let actionTween: cc.Tween = cc.tween();
  28. actionTween.call(() => {
  29. this.node.position = cc.v3(this._originPos.x - this.delPos.x, this._originPos.y - this.delPos.y);
  30. });
  31. if (this.revert) {
  32. actionTween.by(this.duration * 0.5, { position: cc.v3(this.delPos.x, this.delPos.y) }, { easing: this._getEase() });
  33. actionTween.by(this.duration * 0.5, { position: cc.v3(-this.delPos.x, -this.delPos.y) }, { easing: this._getEase() });
  34. } else {
  35. actionTween.by(this.duration, { position: cc.v3(this.delPos.x, this.delPos.y) }, { easing: this._getEase() });
  36. }
  37. actionTween.delay(this.interval);
  38. this._tween = cc.tween(this.node);
  39. this._tween.then(delayTween);
  40. this._tween.then(actionTween);
  41. if (this.loop) {
  42. this._tween.repeatForever();
  43. }
  44. }
  45. onEnable() {
  46. this.node.position = cc.v3(this._originPos.x - this.delPos.x, this._originPos.y - this.delPos.y);
  47. this._tween.start();
  48. }
  49. }