スポンサーサイト

-------- | --:--

上記の広告は1ヶ月以上更新のないブログに表示されています。
新しい記事を書く事で広告が消せます。

Tweenerで画像回転

2008-07-26 | 18:34

左側のトランジッション名をクリックするとそれにあわせて画像が回転します。
package {
    import flash.display.*;
    import flash.events.*;
    import flash.text.*;
    import flash.utils.*;

    import caurina.transitions.Tweener;

    [SWF(width="600", height="400", frameRate="10", backgroundColor="#ffffff")]
        public class RollTweener extends Sprite{

            [Embed(source="lena.jpg")]
                private var myImage:Class;
            private var bm:Bitmap = new myImage();
            private var ro:RollObject;

            private var transitions:Array = ["linear",
                "easeInSine", "easeOutSine", "easeInOutSine",
                "easeInQuad", "easeOutQuad", "easeInOutQuad",
                "easeInCubic", "easeOutCubic", "easeInOutCubic",
                "easeInQuart", "easeOutQuart", "easeInOutQuart",
                "easeInQuint", "easeOutQuint", "easeInOutQuint",
                "easeInExpo", "easeOutExpo", "easeInOutExpo",
                "easeInCirc", "easeOutCirc", "easeInOutCirc",
                "easeInElstic", "easeOutElstic", "easeInOutElstic",
                "easeInBack", "easeOutBack", "easeInOutBack",
                "easeInBounce", "easeOutBounce", "easeInOutBounce" ];

            public function RollTweener():void
            {
                stage.align = StageAlign.TOP_LEFT;
                stage.scaleMode = StageScaleMode.NO_SCALE;

                bm.x = 200;
                bm.y = 100;
                ro = new RollObject(bm);
                addChild(bm);

                RollUtil.init();

                for(var i:int = 0 ; i < transitions.length ; i++){

                    var tf:TextField = new TextField();
                    tf.text = transitions[i];
                    tf.addEventListener(MouseEvent.MOUSE_DOWN, function(e:*):void{
                            slerp(e.target);
                            });
                    addChild(tf);
                    tf.y = i*12;
                }
            }

            private function slerp(tf:TextField):void{
                Tweener.addTween(ro, {time:4, _t:Math.PI*2, transition:tf.text,
                        onComplete:function():void{
                        ro.onComplete();
                        }
                        });
            }
        }
} 

import flash.display.*;
internal class RollObject{
    public var t:Number=0;
    public var bm:Bitmap;
    public var height:Number;

    public function RollObject(o:Bitmap){
        bm = o;
        height = o.height;
    }

    public function onComplete():void{
        t = 0;
    }
}

import flash.geom.Matrix;
import caurina.transitions.Tweener;
internal class RollUtil{
    public static function init(): void {
        Tweener.registerSpecialProperty("_t", _get_t, _set_t, ["t"]);
    }

    protected static function _get_t(obj:Object, p_parameters:Array, p_extra:Object = null):Number
    {
        return obj.t;
    }

    protected static function _set_t(obj:Object, rad:Number, p_parameters:Array, p_extra:Object = null):void
    {
        trace(rad);

        // 回転軸のベクトル
        var vx:Number = 1;
        var vy:Number = 0;
                        
        // 回転後の基底を求めて変形行列に設定
        var m:Matrix = obj.bm.transform.matrix;
        m.a = (1-Math.cos(rad))*vx*vx + Math.cos(rad);
        m.b= (1-Math.cos(rad))*vx*vy ;
        m.c = (1-Math.cos(rad))*vx*vy;
        m.d = (1-Math.cos(rad))*vy*vy + Math.cos(rad);

        // 変形行列の固有値を1にするため(大きさを変えないため)の係数
        var s:Number = vx / (m.a*vx + m.b*vy);
        m.a *= s;
        m.b *= s;
        m.c *= s;
        m.d *= s;
        obj.bm.transform.matrix = m;

        var h:Number = obj.height/2;
        trace(h);
        obj.bm.y = h - h*Math.cos(rad) + 100;
    }
}

スポンサーサイト
上記広告は1ヶ月以上更新のないブログに表示されています。新しい記事を書くことで広告を消せます。