class ShakeTool1 {
private static instance: ShakeTool1; //单例
public static getInstance(): ShakeTool1 {
if (this.instance == null) {
this.instance = new ShakeTool1();
}
return this.instance;
}
private checkForMinimumValues: boolean = true;
private minShakeValue: number = 0.001;
private minRotationValue: number = 0.001;
/**屏幕震动 */
public ShakeScreen() {
Main.Instance.getSceneRoot().x = 0
Main.Instance.getSceneRoot().y = 0
ShakeTool1.getInstance().DoShake_Internal(Main.Instance.getSceneRoot(),
2, //震动次数
new egret.Point(0.5, 0.5), //x,y轴的震动系数[-1,1]
new egret.Point(1, 1), //x,y轴的震动量级
0, //旋转量级
8, //震动x.y轴的最大偏移量
50, //如果速度越快,则振幅越剧烈
1 //振幅的递减系数
)
}
/**
* 屏幕震动
* @param camera 震动对象
* @param numberOfShakes 震动次数
* @param seed x,y轴的震动系数[-1,1]
* @param shakeAmount x,y轴的震动量级
* @param rotationAmount 旋转量级 如果小于0表示:不旋转
* @param distance 震动x.y轴的最大偏移量
* @param speed 如果速度越快,则振幅越剧烈
* @param decay 振幅的递减系数
* @param endCallBack 结束回调
*
*/
public DoShake_Internal(cam: egret.DisplayObject, numberOfShakes: number, seed: egret.Point, shakeAmount: egret.Point,
rotationAmount: number, distance: number, speed: number,
decay: number, endCallBack: Function = null) {
if (cam == null)
return
var defaultPointY = cam.x
var defaultPointX = cam.y
var defaultRotation = cam.rotation
// Set random values
var mod1 = seed.x > 0.5 ? 1 : -1;
var mod2 = seed.y > 0.5 ? 1 : -1;
var currentShakes = numberOfShakes
var shakeDistance = distance;
let shakePosition: egret.Point
let shakeRotation: egret.Point
let guiShakePosition: egret.Point
let rotationStrength = 1;
let start1: egret.Point = new egret.Point(0, 0)
let startR: egret.Point = new egret.Point(0, 0)
let startTime = 0
let timeOnEnterFrame = -1
let curTime: number = 0
let resetEvent = () => {
cam.x = defaultPointY
cam.y = defaultPointY
cam.rotation = defaultRotation
cam.removeEventListener(egret.Event.ENTER_FRAME, updateEvent, this)
if (endCallBack != null) {
endCallBack()
}
}
let updateEvent = () => {
var now = egret.getTimer();
var time = timeOnEnterFrame;
var pass = (now - time) / 1000;
if (timeOnEnterFrame == -1) {
timeOnEnterFrame = now;
return
}
else {
timeOnEnterFrame = now;
}
curTime += pass
curTime = curTime * speed
if (this.checkForMinimumValues) {
if (rotationStrength <= this.minRotationValue) {
resetEvent()
return
}
if (distance != 0 && shakeDistance <= this.minShakeValue) {
resetEvent()
return;
}
if (currentShakes <= 0) {
resetEvent()
return;
}
}
if (cam == null) {
return
}
cam.x = defaultPointY + mod1 * Math.sin(curTime) * (shakeAmount.x * shakeDistance)
cam.y = defaultPointX + mod2 * Math.cos(curTime) * (shakeAmount.y * shakeDistance)
if (rotationAmount > 0) {
cam.rotation = defaultRotation + Math.cos(curTime) * (rotationAmount * rotationStrength)
}
if (curTime > Math.PI * 2) {
curTime = 0;
shakeDistance *= (1 - Tools.Mathf.Clamp01(decay));
rotationStrength *= (1 - Tools.Mathf.Clamp01(decay));
currentShakes--;
}
}
cam.addEventListener(egret.Event.ENTER_FRAME, updateEvent, this);
}
}