motion-streak-ctrl.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import { _decorator, Component, Node, MotionStreak, Texture2D, Animation, Color } from 'cc';
  2. const { ccclass, type } = _decorator;
  3. @ccclass('MotionStreakCtrl')
  4. export class MotionStreakCtrl extends Component {
  5. @type(MotionStreak)
  6. motionStreak: MotionStreak | null = null;
  7. @type(Texture2D)
  8. newTexture: Texture2D | null = null;
  9. @type(Animation)
  10. animationCom: Animation | null = null;
  11. private _changed: boolean = true;
  12. private _oldTexture: Texture2D | null = null;
  13. private _colorChanged: boolean = false;
  14. private _newColor = Color.CYAN;
  15. private _oldColor = Color.WHITE;
  16. onLoad () {
  17. this._changed = true;
  18. this._oldTexture = this.motionStreak!.texture;
  19. this._colorChanged = false;
  20. }
  21. onClick () {
  22. if (this._changed) {
  23. this.setMotionStreak(2, 3, 20, this.newTexture!);
  24. }
  25. else {
  26. this.setMotionStreak(0.5, 1, 30, this._oldTexture!);
  27. }
  28. this._changed = !this._changed;
  29. }
  30. colorChange () {
  31. this._colorChanged = !this._colorChanged;
  32. if (this._colorChanged) {
  33. this.motionStreak!.color = this._newColor;
  34. } else {
  35. this.motionStreak!.color = this._oldColor;
  36. }
  37. }
  38. setMotionStreak (fadeTime: number, minSeg: number, stroke: number, texture: Texture2D) {
  39. this.motionStreak!.fadeTime = fadeTime;
  40. this.motionStreak!.minSeg = minSeg;
  41. this.motionStreak!.stroke = stroke;
  42. this.motionStreak!.texture = texture;
  43. }
  44. lateUpdate () {
  45. if (!this.animationCom!.getState('move_around').isPlaying) {
  46. this.animationCom!.play();
  47. }
  48. }
  49. onDisable () {
  50. this.animationCom!.stop();
  51. }
  52. }