NoticeViewScript.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { _decorator, Component, Node, Vec2, director, Vec3 } from 'cc';
  2. import { NoticeManager } from './NoticeManager';
  3. const { ccclass, property } = _decorator;
  4. @ccclass('NoticeViewScript')
  5. export class NoticeViewScript extends Component {
  6. /**
  7. * 类型
  8. */
  9. type:string;
  10. /**
  11. * 开始位置
  12. */
  13. startPos:Vec3;
  14. /**
  15. * 持续时间
  16. */
  17. time:number;
  18. /**
  19. * 移动距离
  20. */
  21. dis:number;
  22. /**
  23. * 数据
  24. */
  25. data:any;
  26. /**
  27. * 0 纵向 1横向
  28. */
  29. moveType:number=0;
  30. protected startTime:number;
  31. protected endPos:Vec2;
  32. start () {
  33. }
  34. StartMove(data?:any):void{
  35. if(this.endPos==null){
  36. this.endPos=new Vec2();
  37. }
  38. this.data=data;
  39. this.startTime=director.getCurrentTime();
  40. if(this.moveType==0){
  41. this.endPos.x=this.startPos.x+this.dis;
  42. this.endPos.y=this.startPos.y;
  43. }else{
  44. this.endPos.x=this.startPos.x;
  45. this.endPos.y=this.startPos.y+this.dis;
  46. }
  47. this.OnStartMove();
  48. }
  49. protected OnStartMove():void{
  50. }
  51. update (deltaTime: number) {
  52. let currentTime:number=director.getCurrentTime();
  53. let passTime:number=currentTime-this.startTime;
  54. let disX:number=this.endPos.x-this.startPos.x;
  55. let disY:number=this.endPos.y-this.startPos.y;
  56. let value:number=passTime/this.time;
  57. this.node.setPosition(this.startPos.x+disX*value,this.startPos.y+disY*value,0);
  58. if(passTime>this.time){
  59. //结束
  60. NoticeManager.recycle(this);
  61. }
  62. }
  63. }