zoukankan      html  css  js  c++  java
  • 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;
    }

     
     
    参考链接:

  • 相关阅读:
    [转]项目管理---敏捷开发思想---带来相当愉快的项目开发过程
    [转] 项目管理---项目经理如何应对客户的需求变更?
    [转]C# 线程知识--使用Task执行异步操作
    [转]细说ASP.NET的各种异步操作
    [转]oracle表分区详解
    解决Asp.net 部署后弹出登陆框
    SVN客户端TortoiseSVN安装配置图文教程
    .net版本区别及发展历程
    CLR via c#读书笔记九:字符、字符串和文本处理
    CLR via c#读书笔记九:接口
  • 原文地址:https://www.cnblogs.com/meteoric_cry/p/2790857.html
Copyright © 2011-2022 走看看