| /trunk/actionscript/as3/com/vfd/animation/AnimatedClip.as r10 | /trunk/actionscript/as3/com/vfd/animation/AnimatedClip.as r11 | ||
| 1 | package{ | 1 | package com.vfd.animation{ |
|---|---|---|---|
| 2 | /** | 2 | /** |
| 3 | * Animation class. Based off of mc_tween2.as project. | 3 | * Animation class. Based off of mc_tween2.as project. |
| 4 | * @author Jeremy Wischusen <cortex@visualflowdesigns.com> | 4 | * @author Jeremy Wischusen <cortex@visualflowdesigns.com> |
| 5 | */ | 5 | */ |
| 6 | import flash.display.Sprite; | 6 | import flash.display.Sprite; |
| 7 | import fl.transitions.Tween; | 7 | import fl.transitions.Tween; |
| 8 | import fl.motion.Color; | 8 | import fl.motion.Color; |
| 9 | import flash.geom.ColorTransform; | 9 | import flash.geom.ColorTransform; |
| 10 | import fl.transitions.TweenEvent; | 10 | import fl.transitions.TweenEvent; |
| 11 | import flash.events.Event; | 11 | import flash.events.Event; |
| 12 | public class AnimatedClip extends Sprite { | 12 | public class AnimatedClip extends Sprite { |
| 13 | private var originalX:Number; | 13 | private var originalX:Number; |
| 14 | private var originalY:Number; | 14 | private var originalY:Number; |
| 15 | private var xTween:Tween; | 15 | private var xTween:Tween; |
| 16 | private var yTween:Tween; | 16 | private var yTween:Tween; |
| 17 | private var alphaTween:Tween; | 17 | private var alphaTween:Tween; |
| 18 | private var rotationTween:Tween; | 18 | private var rotationTween:Tween; |
| 19 | private var widthTween:Tween; | 19 | private var widthTween:Tween; |
| 20 | private var heightTween:Tween; | 20 | private var heightTween:Tween; |
| 21 | private var eventTween:Tween; | 21 | private var eventTween:Tween; |
| 22 | private var tweenArray:Array; | 22 | private var tweenArray:Array; |
| 23 | private var colorTween:Tween; | 23 | private var colorTween:Tween; |
| 24 | private var colorTrans:ColorTransform; | 24 | private var colorTrans:ColorTransform; |
| 25 | private var tweenToColor:Number; | 25 | private var tweenToColor:Number; |
| 26 | private var lastColor:Number; | 26 | private var lastColor:Number; |
| 27 | private var timerClip:Sprite; | 27 | private var timerClip:Sprite; |
| 28 | private var orbitAngle:Number = 0; | 28 | private var orbitAngle:Number = 0; |
| 29 | private var _orbitVelocity:Number = .5; | 29 | private var _orbitVelocity:Number = .5; |
| 30 | private var _orbitY:Number = stage.stageHeight/2; | 30 | private var _orbitY:Number = stage.stageHeight/2; |
| 31 | private var _orbitX:Number = stage.stageWidth/2; | 31 | private var _orbitX:Number = stage.stageWidth/2; |
| 32 | private var _orbitDistance:Number; | 32 | private var _orbitDistance:Number; |
| 33 | 33 | ||
| 34 | 34 | ||
| 35 | public function AnimatedClip() { | 35 | public function AnimatedClip() { |
| 36 | this.originalX = this.x; | 36 | this.originalX = this.x; |
| 37 | this.originalY = this.y; | 37 | this.originalY = this.y; |
| 38 | this.xTween = new Tween(this, "x", null, this.x, this.x, 1, true); | 38 | this.xTween = new Tween(this, "x", null, this.x, this.x, 1, true); |
| 39 | this.yTween = new Tween(this, "y", null, this.y, this.y, 1, true); | 39 | this.yTween = new Tween(this, "y", null, this.y, this.y, 1, true); |
| 40 | this.alphaTween = new Tween(this, "alpha", null, this.alpha, this.alpha, 1, true); | 40 | this.alphaTween = new Tween(this, "alpha", null, this.alpha, this.alpha, 1, true); |
| 41 | this.rotationTween = new Tween(this, "rotation", null, this.rotation, this.rotation, 1, true); | 41 | this.rotationTween = new Tween(this, "rotation", null, this.rotation, this.rotation, 1, true); |
| 42 | this.widthTween = new Tween(this, "width", null, this.width, this.width, 1, true); | 42 | this.widthTween = new Tween(this, "width", null, this.width, this.width, 1, true); |
| 43 | this.heightTween = new Tween(this, "height", null, this.height, this.height, 1, true); | 43 | this.heightTween = new Tween(this, "height", null, this.height, this.height, 1, true); |
| 44 | this.timerClip = new Sprite(); | 44 | this.timerClip = new Sprite(); |
| 45 | this.colorTween = new Tween(this.timerClip,'alpha',null,0,1,1,true); | 45 | this.colorTween = new Tween(this.timerClip,'alpha',null,0,1,1,true); |
| 46 | this.colorTween.stop(); | 46 | this.colorTween.stop(); |
| 47 | this.tweenArray = new Array(this.xTween, this.yTween, this.alphaTween, this.rotationTween, this.widthTween, this.heightTween,this.colorTween); | 47 | this.tweenArray = new Array(this.xTween, this.yTween, this.alphaTween, this.rotationTween, this.widthTween, this.heightTween,this.colorTween); |
| 48 | this.colorTrans = this.transform.colorTransform; | 48 | this.colorTrans = this.transform.colorTransform; |
| 49 | this._orbitDistance = this.width*2; | 49 | this._orbitDistance = this.width*2; |
| 50 | this.colorTween.addEventListener(TweenEvent.MOTION_CHANGE, updateColor); | 50 | this.colorTween.addEventListener(TweenEvent.MOTION_CHANGE, updateColor); |
| 51 | 51 | ||
| 52 | } | 52 | } |
| 53 | public function resetPosition():void { | 53 | public function resetPosition():void { |
| 54 | this.x = this.originalX; | 54 | this.x = this.originalX; |
| 55 | this.y = this.originalY; | 55 | this.y = this.originalY; |
| 56 | } | 56 | } |
| 57 | public function moveToPoint(x:Number, y:Number,time:Number = 0, loop:Boolean = false):void { | 57 | public function moveToPoint(x:Number, y:Number,time:Number = 0, loop:Boolean = false):void { |
| 58 | this.xTween.finish = x; | 58 | this.xTween.finish = x; |
| 59 | this.xTween.duration = time; | 59 | this.xTween.duration = time; |
| 60 | this.yTween.finish = y; | 60 | this.yTween.finish = y; |
| 61 | this.yTween.duration = time; | 61 | this.yTween.duration = time; |
| 62 | this.xTween.start(); | 62 | this.xTween.start(); |
| 63 | this.yTween.start(); | 63 | this.yTween.start(); |
| 64 | } | 64 | } |
| 65 | public function fadeTo(val:Number =0, time:Number = 0, loop:Boolean = false):void { | 65 | public function fadeTo(val:Number =0, time:Number = 0, loop:Boolean = false):void { |
| 66 | this.alphaTween.finish = val; | 66 | this.alphaTween.finish = val; |
| 67 | this.alphaTween.duration = time; | 67 | this.alphaTween.duration = time; |
| 68 | this.alphaTween.looping=loop; | 68 | this.alphaTween.looping=loop; |
| 69 | this.alphaTween.start(); | 69 | this.alphaTween.start(); |
| 70 | } | 70 | } |
| 71 | public function sizeTo(w:Number, h:Number, time:Number=0, loop:Boolean = false):void { | 71 | public function sizeTo(w:Number, h:Number, time:Number=0, loop:Boolean = false):void { |
| 72 | this.widthTween.finish=w; | 72 | this.widthTween.finish=w; |
| 73 | this.widthTween.looping = loop; | 73 | this.widthTween.looping = loop; |
| 74 | this.widthTween.duration = time; | 74 | this.widthTween.duration = time; |
| 75 | this.heightTween.finish = h; | 75 | this.heightTween.finish = h; |
| 76 | this.heightTween.looping = loop; | 76 | this.heightTween.looping = loop; |
| 77 | this.heightTween.duration = time; | 77 | this.heightTween.duration = time; |
| 78 | this.widthTween.start(); | 78 | this.widthTween.start(); |
| 79 | this.heightTween.start(); | 79 | this.heightTween.start(); |
| 80 | 80 | ||
| 81 | } | 81 | } |
| 82 | public function spinTo(rot:Number= 0, time:Number = 0, loop:Boolean = false):void { | 82 | public function spinTo(rot:Number= 0, time:Number = 0, loop:Boolean = false):void { |
| 83 | this.rotationTween.finish=rot; | 83 | this.rotationTween.finish=rot; |
| 84 | this.rotationTween.duration = time; | 84 | this.rotationTween.duration = time; |
| 85 | this.rotationTween.looping = loop; | 85 | this.rotationTween.looping = loop; |
| 86 | this.rotationTween.start(); | 86 | this.rotationTween.start(); |
| 87 | } | 87 | } |
| 88 | public function xTo(x:Number, time:Number = 0, loop:Boolean = false):void { | 88 | public function xTo(x:Number, time:Number = 0, loop:Boolean = false):void { |
| 89 | this.xTween.finish = x; | 89 | this.xTween.finish = x; |
| 90 | this.xTween.duration = time; | 90 | this.xTween.duration = time; |
| 91 | this.xTween.looping = loop; | 91 | this.xTween.looping = loop; |
| 92 | this.xTween.start(); | 92 | this.xTween.start(); |
| 93 | } | 93 | } |
| 94 | public function yTo(y:Number, time:Number = 0, loop:Boolean = false):void { | 94 | public function yTo(y:Number, time:Number = 0, loop:Boolean = false):void { |
| 95 | this.yTween.finish = y; | 95 | this.yTween.finish = y; |
| 96 | this.yTween.duration = time; | 96 | this.yTween.duration = time; |
| 97 | this.yTween.looping = loop; | 97 | this.yTween.looping = loop; |
| 98 | this.yTween.start(); | 98 | this.yTween.start(); |
| 99 | } | 99 | } |
| 100 | public function set easing(ease:Function):void { | 100 | public function set easing(ease:Function):void { |
| 101 | for (var tw in this.tweenArray) { | 101 | for (var tw in this.tweenArray) { |
| 102 | tweenArray[tw].func = ease; | 102 | tweenArray[tw].func = ease; |
| 103 | } | 103 | } |
| 104 | } | 104 | } |
| 105 | public function colorTo(toColor:Number, time:Number):void { | 105 | public function colorTo(toColor:Number, time:Number):void { |
| 106 | this.lastColor = this.transform.colorTransform.color; | 106 | this.lastColor = this.transform.colorTransform.color; |
| 107 | this.tweenToColor = toColor; | 107 | this.tweenToColor = toColor; |
| 108 | this.colorTween.duration = time; | 108 | this.colorTween.duration = time; |
| 109 | this.colorTween.start(); | 109 | this.colorTween.start(); |
| 110 | } | 110 | } |
| 111 | private function updateColor(event:TweenEvent):void { | 111 | private function updateColor(event:TweenEvent):void { |
| 112 | this.colorTrans.color = Color.interpolateColor(this.lastColor, this.tweenToColor,event.position); | 112 | this.colorTrans.color = Color.interpolateColor(this.lastColor, this.tweenToColor,event.position); |
| 113 | updateColorTransform(); | 113 | updateColorTransform(); |
| 114 | } | 114 | } |
| 115 | public function set currentColor(clr:Number) { | 115 | public function set currentColor(clr:Number) { |
| 116 | this.lastColor = clr; | 116 | this.lastColor = clr; |
| 117 | this.colorTrans.color= clr; | 117 | this.colorTrans.color= clr; |
| 118 | updateColorTransform(); | 118 | updateColorTransform(); |
| 119 | 119 | ||
| 120 | } | 120 | } |
| 121 | public function startOrbit():void { | 121 | public function startOrbit():void { |
| 122 | this.addEventListener(Event.ENTER_FRAME,orbit); | 122 | this.addEventListener(Event.ENTER_FRAME,orbit); |
| 123 | } | 123 | } |
| 124 | public function stopOrbit():void { | 124 | public function stopOrbit():void { |
| 125 | this.removeEventListener(Event.ENTER_FRAME,orbit); | 125 | this.removeEventListener(Event.ENTER_FRAME,orbit); |
| 126 | this.orbitAngle = 0; | 126 | this.orbitAngle = 0; |
| 127 | } | 127 | } |
| 128 | private function orbit(event:Event) { | 128 | private function orbit(event:Event) { |
| 129 | this.y = this._orbitY + Math.sin(this.orbitAngle)*this._orbitDistance; | 129 | this.y = this._orbitY + Math.sin(this.orbitAngle)*this._orbitDistance; |
| 130 | this.x = this._orbitX + Math.cos(this.orbitAngle)*this._orbitDistance; | 130 | this.x = this._orbitX + Math.cos(this.orbitAngle)*this._orbitDistance; |
| 131 | this.orbitAngle += this._orbitVelocity; | 131 | this.orbitAngle += this._orbitVelocity; |
| 132 | } | 132 | } |
| 133 | public function set yEasing(ease:Function) { | 133 | public function set yEasing(ease:Function) { |
| 134 | this.yTween.func = ease; | 134 | this.yTween.func = ease; |
| 135 | } | 135 | } |
| 136 | public function set xEasing(ease:Function) { | 136 | public function set xEasing(ease:Function) { |
| 137 | this.xTween.func = ease; | 137 | this.xTween.func = ease; |
| 138 | } | 138 | } |
| 139 | public function set heightEasing(ease:Function) { | 139 | public function set heightEasing(ease:Function) { |
| 140 | this.heightTween.func = ease; | 140 | this.heightTween.func = ease; |
| 141 | } | 141 | } |
| 142 | public function set widthEasing(ease:Function) { | 142 | public function set widthEasing(ease:Function) { |
| 143 | this.widthTween.func = ease; | 143 | this.widthTween.func = ease; |
| 144 | } | 144 | } |
| 145 | public function set alphaEasing(ease:Function) { | 145 | public function set alphaEasing(ease:Function) { |
| 146 | this.alphaTween.func = ease; | 146 | this.alphaTween.func = ease; |
| 147 | } | 147 | } |
| 148 | public function set rotationEasing(ease:Function) { | 148 | public function set rotationEasing(ease:Function) { |
| 149 | this.rotationTween.func = ease; | 149 | this.rotationTween.func = ease; |
| 150 | } | 150 | } |
| 151 | public function set orbitVelocity(velocity:Number) { | 151 | public function set orbitVelocity(velocity:Number) { |
| 152 | this._orbitVelocity = velocity; | 152 | this._orbitVelocity = velocity; |
| 153 | } | 153 | } |
| 154 | public function set orbitY(y:Number) { | 154 | public function set orbitY(y:Number) { |
| 155 | this._orbitY = y; | 155 | this._orbitY = y; |
| 156 | } | 156 | } |
| 157 | public function set orbitX(x:Number) { | 157 | public function set orbitX(x:Number) { |
| 158 | this._orbitX = x; | 158 | this._orbitX = x; |
| 159 | } | 159 | } |
| 160 | public function set orbitDistance(distance:Number) { | 160 | public function set orbitDistance(distance:Number) { |
| 161 | this._orbitDistance = distance; | 161 | this._orbitDistance = distance; |
| 162 | } | 162 | } |
| 163 | public function get currentColor():Number { | 163 | public function get currentColor():Number { |
| 164 | return this.lastColor; | 164 | return this.lastColor; |
| 165 | } | 165 | } |
| 166 | private function updateColorTransform() { | 166 | private function updateColorTransform() { |
| 167 | this.transform.colorTransform = this.colorTrans; | 167 | this.transform.colorTransform = this.colorTrans; |
| 168 | } | 168 | } |
| 169 | } | 169 | } |
| 170 | } | 170 | } |