Laya 屏幕抖动
Laya 屏幕抖动
@author ixenos 2020-05-13 12:07:37
1 package 2 { 3 4 import laya.maths.Rectangle; 5 import laya.ui.Box; 6 7 public class ShakeCamera 8 { 9 private static var _ins:ShakeCamera; 10 11 private var _camera:Box; 12 13 private var _shakeTime:Number = 0; 14 private var _extent:Number = 0;//幅度 15 16 private var _enableShake:Boolean = true; 17 18 public function ShakeCamera() 19 { 20 } 21 22 public static function get ins():ShakeCamera 23 { 24 if(!_ins){ 25 _ins = new ShakeCamera(); 26 } 27 return _ins; 28 } 29 30 public function init(cam:Box,shakeTime:Number=200,extent:Number=10):void{ 31 _camera = cam; 32 33 _shakeTime = shakeTime; 34 _extent = extent; 35 36 enableShake = false; 37 } 38 39 public function update():void{ 40 if(enableShake && _camera){ 41 if(_shakeTime > 0){ 42 _shakeTime -= Laya.timer.delta; 43 if(_shakeTime <= 0){ 44 _camera.pos(0,0); 45 enableShake = false; 46 }else{ 47 // _camera.pos(-_extent*0.5+_extent*Math.random(),0); 48 var posY:int = Math.floor(-_extent*0.5+_extent*Math.random()); 49 _camera.pos(0,posY); 50 } 51 } 52 } 53 } 54 55 public function get enableShake():Boolean 56 { 57 return _enableShake; 58 } 59 60 public function set enableShake(value:Boolean):void 61 { 62 _enableShake = value; 63 64 if(_enableShake){ 65 Laya.timer.clear(this,update); 66 Laya.timer.frameLoop(1,this,update); 67 }else{ 68 Laya.timer.clear(this,update); 69 } 70 } 71 72 73 } 74 }