as3 shake动画

/***********/

/**************/

主要改变影片剪辑的x、y和rotation这三个属性。

import flash.events.MouseEvent;
import flash.events.Event;


stop();

var posX:Number = image_mc.x;
var posY:Number = image_mc.y;

image_mc.addEventListener(MouseEvent.MOUSE_OVER, onMouseOverHandler);
image_mc.addEventListener(MouseEvent.MOUSE_OUT, onMouseOutHandler);


function onMouseOverHandler(evt:MouseEvent):void
{
    bindShakeHandler();
}

function onMouseOutHandler(evt:MouseEvent):void
{
    unBindShakeHandler();
    
    image_mc.x = posX;
    image_mc.y = posY;
    image_mc.rotation = 0;
}

function bindShakeHandler():void
{
    unBindShakeHandler();
    
    image_mc.addEventListener(Event.ENTER_FRAME, shakeIt);    
}

function unBindShakeHandler():void
{
    image_mc.removeEventListener(Event.ENTER_FRAME, shakeIt);    
}

function shakeIt(evt:Event):void
{
    image_mc.x = posX + (Math.floor(Math.random() * 4));
    image_mc.y = posY + (Math.floor(Math.random() * 4));
    image_mc.rotation = Math.random() * 4;
}

第二个效果,使用的是TweenMax的插件。

因为使用的是最新的TweenMax,所有这里的as文件是被修改过的。

/*
Copyright (c) 2012 Jonas Volger

Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
and associated documentation files (the "Software"), to deal in the Software without restriction, 
including without limitation the rights to use, copy, modify, merge, publish, distribute, 
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or 
substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 
BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.greensock.plugins
{
    import com.greensock.TweenLite;
    import com.greensock.plugins.TweenPlugin;
    
    import flash.utils.Dictionary;
    
    public class ShakeEffect extends TweenPlugin
    {
        public static const API:Number = 2.0;
        
        protected var _target:Object;
        protected var numShakes:Number = 3.0;
        protected var _tween:TweenLite;
        
        private var amplitude:Number;
        private var decrease:Number;
        
        private var lastValueAdditions:Dictionary = new Dictionary;
        private var currentValueAdditions:Dictionary = new Dictionary;
        
        private var props:Array = new Array;
        private var strengths:Dictionary = new Dictionary;
        
        public function ShakeEffect() {
            super();
            this._propName = "shake";
            this._overwriteProps = [];
        }
        
        override public function _onInitTween(target:Object, value:*, tween:TweenLite):Boolean {
            
            if (!(value is Object))
                throw new Error("invalid arguments!");
            for (var val:* in value)
            {
                if (val == "numShakes")
                {
                    if (value[val] is Number)
                        numShakes = value[val];
                    else
                        throw new Error("invalid arguments!");
                }
                else //if (val == "targetProperty")
                {
                    if (!target.hasOwnProperty(val)) {
                        throw new Error("invalid arguments!");
                    }
                    props.push(val);
                    strengths[val] = value[val];
                    lastValueAdditions[val] = 0;
                    currentValueAdditions[val] = 0;
                    //this.overwriteProps.push(val);
                }
            }
            _tween = tween;
            _target = target;
            
            
            return true;
        }
        
        override public function _kill(lookup:Object):Boolean {
            var i:int = this._overwriteProps.length;
            while (i--) {
                if (this._overwriteProps[i] in lookup) {
                    _target[this._overwriteProps[i]] = _target[this._overwriteProps[i]] - lastValueAdditions[this._overwriteProps[i]];
                    this._overwriteProps.splice(i,1);
                    //return;
                }
            }
            //super.killProps(lookup);
            
            return super._kill(lookup);
        }
        
        
        override public function setRatio(n:Number):void {
            amplitude = Math.sin( (n * (2*Math.PI)) * numShakes  );
            decrease = 1-n;
            for each (var prop:String in props)
            {
                currentValueAdditions[prop] = (strengths[prop]*amplitude*decrease);
                _target[prop] = _target[prop] - lastValueAdditions[prop] + currentValueAdditions[prop];
                lastValueAdditions[prop] = currentValueAdditions[prop];
            }
            
        }
    }
}

添加了一个属性“shake”,其中“numShakes”为在指定的时间内需要震动的次数。

import com.greensock.TweenMax;
import com.greensock.plugins.ShakeEffect;
import com.greensock.plugins.TweenPlugin;

import flash.events.MouseEvent;
import flash.events.Event;
import com.greensock.TweenLite;

var bool:Boolean = TweenPlugin.activate([ShakeEffect]);

image_mc.addEventListener(MouseEvent.MOUSE_OVER, onMouseOverHandler);
image_mc.addEventListener(MouseEvent.MOUSE_OUT, onMouseOutHandler);

var tween:TweenLite;
var posX:Number = image_mc.x;
var posY:Number = image_mc.y;


function onMouseOverHandler(evt:MouseEvent):void
{
    onMouseOutHandler();
    
    tween = TweenMax.to(image_mc, 100, {shake:{x:4, y: 4, rotation:4, numShakes:1000*1000}});
}

function onMouseOutHandler(evt:MouseEvent=null):void
{
    if (tween)
    {
        tween.kill();
        tween = null;
    }
    
    image_mc.x = posX;
    image_mc.y = posY;
    image_mc.rotation = 0;
}

参考链接:

Shake Plugin for TweenMax

Advanced vibration effect using the AS3

Shaking effect

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏JavaQ

深入理解Spring系列之七:web应用自动装配Spring配置

在《深入理解Spring系列之一:开篇》的示例代码中使用如下方式去加载Spring的配置文件并初始化容器。 ApplicationContext applica...

36650
来自专栏函数式编程语言及工具

Akka(8): 分布式运算:Remoting-远程查找式

  Akka是一种消息驱动运算模式,它实现跨JVM程序运算的方式是通过能跨JVM的消息系统来调动分布在不同JVM上ActorSystem中的Actor进行运算,...

45890
来自专栏逆向与安全

nProtect APPGuard安卓反外挂分析

1.最近在学习手游保护方面的技术,本文是学习过程中分析某反外挂的一点记录,高手莫要见笑,有不对的地方还请指教,首先简单通过资源目录中文件名做基本了解,

53000
来自专栏用户画像

jQuery validate

8410
来自专栏陈满iOS

iOS开发·runtime原理与实践: 关联对象篇(Associated Object)(应用场景:为分类添加“属性”,为UI控件关联事件Block体,为了不重复获得某种数据)

分类(category)与关联对象(Associated Object)作为objective-c的扩展机制的两个特性:分类,可以通过它来扩展方法;Associ...

51620
来自专栏王磊的博客

javascript数字格式化通用类——accounting.js使用

简介 accounting.js 是一个非常小的JavaScript方法库用于对数字,金额和货币进行格式化。并提供可选的Excel风格列渲染。它没有依赖任何JS...

73160
来自专栏面朝大海春暖花开

spring源码分析 contextConfigLocation属性的位置

49110
来自专栏码匠的流水账

聊聊spring-data-redis的连接池的校验

spring-data-redis/2.0.10.RELEASE/spring-data-redis-2.0.10.RELEASE-sources.jar!/o...

74010
来自专栏游戏杂谈

flex+php截图Demo

一、直接将byteArray转为bitmap通过loader(flash.display.Loader)显示在舞台上;

13010
来自专栏菩提树下的杨过

Flash/Flex学习笔记(34):AS3中的自定义事件

类似C#中自定义事件需要一个自定义的EventArgs子类一样,AS3也需要开发者自定义一个Event类的子类,这里我们假设一种场景:设计一个Person(人物...

21570

扫码关注云+社区

领取腾讯云代金券