egret屏幕抖动效果

简介: egret屏幕抖动效果
推荐阅读:
/**
 * 屏幕抖动
 * @author shirln
 * @since 2018/11/27
 * 
 * Example:
 * 震动目标obj,1秒内震动20次,震动最大距离20
 * ShakeTool.getInstance().shakeObj(obj, 1, 20, 20);
 */
class ShakeTool {
    private static instance:ShakeTool;   //单例
    private initX:number;                //初始位置
    private initY: number;  
    private target:egret.DisplayObject;  //震动目标
    private maxDis: number;              //震动距离
    private count: number = 0;           //计时器次数
    private rate: number;                //一秒震动次数
    private timer:egret.Timer = new egret.Timer(1000);
    
    public static getInstance():ShakeTool{
        if(this.instance == null){
            this.instance = new ShakeTool();
        }
        return this.instance;
    }
    
    /**
     * 震动显示对象
     * @param        target    震动目标对象
     * @param        time      震动持续时长(秒)
     * @param        rate      震动频率(一秒震动多少次)
     * @param        maxDis    震动最大距离
     */
    public shakeObj(target: egret.DisplayObject,time: number,rate: number,maxDis: number): void {
        this.stop();
        this.target = target;
        this.initX = target.x;
        this.initY = target.y;
        this.maxDis = maxDis;
        this.count = time * rate;
        this.rate = rate;
    
        this.timer.delay = 1000/rate;
        this.timer.repeatCount = this.count;
        this.timer.addEventListener(egret.TimerEvent.TIMER,this.shaking, this);
        this.timer.addEventListener(egret.TimerEvent.TIMER_COMPLETE, this.shakeComplete, this);
        this.timer.reset();
        this.timer.start();
    }
    
    private shaking(): void {
        egret.Tween.removeTweens(this.target);
        this.target.x = this.initX - this.maxDis + Math.random()*this.maxDis*2;
        this.target.y = this.initY - this.maxDis +  Math.random()*this.maxDis*2;
        egret.Tween.get(this.target).to({x:this.initX, y:this.initY},999/this.rate);    
    }
    
    private shakeComplete(): void {
        if(this.target){
            egret.Tween.removeTweens(this.target);
            this.target.x = this.initX;
            this.target.y = this.initY;
            this.target = null;
        }
        this.timer.removeEventListener(egret.TimerEvent.TIMER,this.shaking,this);
        this.timer.removeEventListener(egret.TimerEvent.TIMER_COMPLETE,this.shakeComplete,this);
    }

    /**停止震动 */
    public stop(){
        this.shakeComplete();
    }

}

调用方法:

 //震动目标obj,1秒内震动20次,震动最大距离20
 ShakeTool.getInstance().shakeObj(obj, 1, 20, 20);
相关文章
|
3天前
|
前端开发 小程序
监听手机屏幕旋转 横屏 竖屏 dome
监听手机屏幕旋转 横屏 竖屏 dome
|
11月前
【Three.js入门】处理动画、尺寸自适应、双击进入/退出全屏(Clock跟踪时间,Gsap动画库,自适应画面,进入/退出全屏)
【Three.js入门】处理动画、尺寸自适应、双击进入/退出全屏(Clock跟踪时间,Gsap动画库,自适应画面,进入/退出全屏)
|
iOS开发
IOS手指控制图片的缩放
IOS手指控制图片的缩放
46 0
|
Go Android开发
基于Qt的音乐播放器(二)切换歌曲,调节音量,调节语速,暂停
基于Qt的音乐播放器(二)切换歌曲,调节音量,调节语速,暂停
基于Qt的音乐播放器(二)切换歌曲,调节音量,调节语速,暂停
|
Android开发
RecycleView 不显示、显示不全及滑动卡顿
RecycleView 出现的不显示或显示不全。ScrollView中嵌套RecycleView滑动出现卡顿。
539 0
|
JavaScript 前端开发
js实现移动端图片预览:手势缩放, 手势拖动,双击放大...
原文:js实现移动端图片预览:手势缩放, 手势拖动,双击放大... 前言本文将介绍如何通过js实现移动端图片预览,包括图片的 预览模式,手势缩放,手势拖动,双击放大等基本功能; 扫码查看示例效果: 代码地址http://pangyongsheng.github.io/imgPreview/ 一、功能介绍   图片预览主要有以下几个功能点组成: 监听图片点击事件,进入图片预览模式 自定义手势事件, (双指缩放,滑动,双击。
3139 0
flutter弹起键盘页面布局超限问题
引子 相信大家在写flutter的过程中都遇到过一个问题,就是键盘弹起的啥时候超限报下面这个错 BOTTOM OVERFLOWED BY 17 PIXELS
212 0