12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- import EventMgr from "./EventMgr";
- import { GameEvent } from "./GameEvent";
- const {ccclass, property} = cc._decorator;
- @ccclass
- export default class GameCam extends cc.Component {
-
- private camera:cc.Node;
- protected onLoad(): void {
- this.camera = this.node;
- }
- protected start(): void {
- EventMgr.Instance.add_event_listenner(GameEvent.CamShake,this,this.OnCamShake);
- }
- OnCamShake(CamShake: GameEvent, shakeTime: number, OnCamShake: any) {
- this.Shake(shakeTime);
- }
- private Shake(time:number = 0){
- this.StopShake();
- let min = -3;
- let max = 3;
- let me =this;
- let initPos = this.camera.position;
- let callBack = function(){
- let randomX =Util.random(min,max);
- let randomY = Util.random(min,max);
- me.camera.x =initPos.x+ randomX;
- me.camera.y =initPos.y+ randomY;
- }
- let del = cc.delayTime(1/30);
- let cal = cc.callFunc(callBack);
- let seq = cc.sequence(del, cal);
- this.camera.runAction(cc.repeatForever(seq));
- if (time>0) {
- setTimeout(() => {
- me.camera.stopAllActions();
- }, time);
- // Ticker.registerDelay(() => {
- // }, time);
- }
- }
-
- public StopShake(){
- this.camera.stopAllActions();
-
- this.camera.x =0;
- this.camera.y =0;
- }
- }
|