前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >as3 shake动画

as3 shake动画

作者头像
meteoric
发布2018-11-16 17:42:18
7090
发布2018-11-16 17:42:18
举报
文章被收录于专栏:游戏杂谈游戏杂谈游戏杂谈

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

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

主要改变影片剪辑的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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2012-11-27 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档