Laya晃动屏幕特效封装

游戏中有时会用到晃动下屏幕特效,结合网上资源,修改封装了下

/**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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值