GameCam.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import EventMgr from "./EventMgr";
  2. import { GameEvent } from "./GameEvent";
  3. const {ccclass, property} = cc._decorator;
  4. @ccclass
  5. export default class GameCam extends cc.Component {
  6. private camera:cc.Node;
  7. protected onLoad(): void {
  8. this.camera = this.node;
  9. }
  10. protected start(): void {
  11. EventMgr.Instance.add_event_listenner(GameEvent.CamShake,this,this.OnCamShake);
  12. }
  13. OnCamShake(CamShake: GameEvent, shakeTime: number, OnCamShake: any) {
  14. this.Shake(shakeTime);
  15. }
  16. private Shake(time:number = 0){
  17. this.StopShake();
  18. let min = -3;
  19. let max = 3;
  20. let me =this;
  21. let initPos = this.camera.position;
  22. let callBack = function(){
  23. let randomX =Util.random(min,max);
  24. let randomY = Util.random(min,max);
  25. me.camera.x =initPos.x+ randomX;
  26. me.camera.y =initPos.y+ randomY;
  27. }
  28. let del = cc.delayTime(1/30);
  29. let cal = cc.callFunc(callBack);
  30. let seq = cc.sequence(del, cal);
  31. this.camera.runAction(cc.repeatForever(seq));
  32. if (time>0) {
  33. setTimeout(() => {
  34. me.camera.stopAllActions();
  35. }, time);
  36. // Ticker.registerDelay(() => {
  37. // }, time);
  38. }
  39. }
  40. public StopShake(){
  41. this.camera.stopAllActions();
  42. this.camera.x =0;
  43. this.camera.y =0;
  44. }
  45. }