游戏中有时会用到晃动下屏幕特效,结合网上资源,修改封装了下
/**3D晃动管理类 */
export default class shake3D {
private _isShaking: boolean = false;
private _startTime: number = 0;
private _offsetPos: number[] = [0, 0];
private shank_I: number = 0;
private _target: Laya.Sprite3D;
private _orgX: number = 0;
private _orgY: number = 0;
private static instace:shake3D;
constructor() { }
/**返回一个单列 */
public static getInstace():shake3D{
if(!this.instace){
this.instace = new shake3D()
}
return this.instace
}
/**
* 振动
* @param intensity 振动频率(单位:毫秒)
* @param duration 振动时间(单位:毫秒)
* @param radius 振动半径
*
* @example
* 振屏:Mgr.sceneMgr.sceneExt.jscene.camare.shake();
*/
public exe(target: Laya.Sprite3D, intensity: number, duration: number, radius: number) {
if (this._isShaking) {
return;
}
this._isShaking = true;
this._startTime = Laya.timer.currTimer;
this._offsetPos = [0, 0];
this.shank_I = 0;
this._target = target;
this._orgX = target.transform.position.x;
this._orgY = target.transform.position.y;
Laya.timer.loop(intensity, this, this._pos, [duration, radius]);
}
/**清除振动 */
public clear(){
Laya.timer.clearAll(this);
}
private _pos(duration: number, radius: number) {
this._offsetPos[this.shank_I % 2] = (this._offsetPos[this.shank_I % 2] > 0) ? -radius : radius;
this.shank_I++;
console.log('晃动',this._offsetPos)
this._target.transform.localPositionX += this._offsetPos[0];
this._target.transform.localPositionY += this._offsetPos[1];
if (Laya.timer.currTimer - this._startTime >= duration) {
Laya.timer.clear(this, this._pos);
this.shank_I = 0;
this._isShaking = false;
this._target.transform.position.x = this._orgX;
this._target.transform.position.y = this._orgY;
this._target = null;
}
}
}
参考:https://www.cnblogs.com/fohuo/p/12011129.html