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);
相关文章
|
8月前
|
Unix Shell 数据处理
怎样使用Cython提升Python的性能
**Cython是Python的性能增强工具,用于提升Python代码的速度。它允许声明变量类型并调用C库。安装Cython使用`pip install Cython`。Cython语法接近Python,但通过类型声明优化性能。编译Cython代码需创建setup.py文件,然后运行`python setup.py build_ext --inplace`。通过Cython,可以直接优化Python代码和调用C函数,平衡速度与灵活性。**
205 2
|
Python
Python 技巧篇-用print打印输出但不换行方法
Python 技巧篇-用print打印输出但不换行方法
3212 0
Python 技巧篇-用print打印输出但不换行方法
|
6月前
|
存储 Java 图形学
UNITY性能优化☀️一、GC介绍与Unity内存管理方法
UNITY性能优化☀️一、GC介绍与Unity内存管理方法
|
XML Java 数据格式
【Lua基础 第2章】lua遍历table的方式、运算符、math库、字符串操作方法
lua遍历table的方式、运算符、math库、字符串操作方法
754 0
【Lua基础 第2章】lua遍历table的方式、运算符、math库、字符串操作方法
|
前端开发 Android开发
Starting a Gradle Daemon, 1 incompatible and 2 stopped Daemons could not be reus
Starting a Gradle Daemon, 1 incompatible and 2 stopped Daemons could not be reus
2083 0
|
移动开发 数据可视化 JavaScript
谈一谈|小白如何使用egret
谈一谈|小白如何使用egret
266 0
|
Web App开发 移动开发 前端开发
HTML5 移动端页面适配 iOS 系统刘海屏
HTML5 移动端页面适配 iOS 系统刘海屏
1333 0
HTML5 移动端页面适配 iOS 系统刘海屏
|
存储 自然语言处理 搜索推荐
luncne 教程
Lucene是一套用于全文检索和搜寻的开源程序库,提供了一个简单却强大的应用程序接口(API),能够做全文索引和搜寻,在Java开发环境里Lucene是一个成熟的免费开放源代码工具
luncne 教程
|
JSON 数据格式 缓存
HttpClient获取并解析JSON数据
package com.example.testjsonandget; import java.io.BufferedReader; import java.
1362 0
|
定位技术 数据格式
高德地图开发智慧社区网格化数据格式产生的无法单击事件的解决方案
高德地图开发智慧社区网格化数据格式产生的无法单击事件的解决方案
168 0

热门文章

最新文章