AnimEvent.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import { _decorator, Component, Animation } from 'cc';
  2. const { ccclass, requireComponent } = _decorator;
  3. @ccclass('AnimEvent')
  4. @requireComponent([Animation])
  5. export class AnimEvent extends Component {
  6. private _animation: Animation = null;
  7. playCallback: Function = null; //开始播放时触发
  8. stopCallback: Function = null; //停止播放时触发
  9. pauseCallback: Function = null; //暂停播放时触发
  10. resumeCallback: Function = null; //恢复播放时触发
  11. lastFrameCallback: Function = null; //假如动画循环次数大于1,当动画播放到最后一帧时触发
  12. finishedCallback: Function = null; //动画完成播放时触发
  13. onLoad() {
  14. this._animation = this.getComponent(Animation);
  15. this._animation.on(Animation.EventType.PLAY, () => {
  16. this.playCallback && this.playCallback(this);
  17. }, this);
  18. this._animation.on(Animation.EventType.STOP, () => {
  19. this.stopCallback && this.stopCallback(this);
  20. }, this);
  21. this._animation.on(Animation.EventType.PAUSE, () => {
  22. this.pauseCallback && this.pauseCallback(this);
  23. }, this);
  24. this._animation.on(Animation.EventType.RESUME, () => {
  25. this.resumeCallback && this.resumeCallback(this);
  26. }, this);
  27. this._animation.on(Animation.EventType.LASTFRAME, () => {
  28. this.lastFrameCallback && this.lastFrameCallback(this);
  29. }, this);
  30. this._animation.on(Animation.EventType.FINISHED, () => {
  31. this.finishedCallback && this.finishedCallback(this);
  32. }, this);
  33. }
  34. play(name?: string) {
  35. this.node.active = true;
  36. this._animation.play(name);
  37. }
  38. stop() {
  39. this._animation.stop();
  40. this.node.active = false;
  41. }
  42. crossFade(name: string, duration?: number) {
  43. this._animation.crossFade(name, duration);
  44. }
  45. pause() {
  46. this._animation.pause();
  47. }
  48. resume() {
  49. this._animation.resume();
  50. }
  51. getState(name: string) {
  52. return this._animation.getState(name);
  53. }
  54. }