| /trunk/actionscript/as3/com/vfd/animation/AnimatedClip.as r11 | /trunk/actionscript/as3/com/vfd/animation/AnimatedClip.as r12 | ||
| 1 | package com.vfd.animation{ | 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,duration: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 = duration; |
| 60 | this.yTween.finish = y; | 60 | this.xTween.looping = loop; |
| 61 | this.yTween.duration = time; | 61 | this.yTween.finish = y; |
| 62 | this.xTween.start(); | 62 | this.yTween.duration = duration; |
| 63 | this.yTween.start(); | 63 | this.yTween.looping = loop; |
| 64 | } | 64 | this.xTween.start(); |
| 65 | public function fadeTo(val:Number =0, time:Number = 0, loop:Boolean = false):void { | 65 | this.yTween.start(); |
| 66 | } | ||
| 67 | public function fadeTo(val:Number =0, duration:Number = 0, loop:Boolean = false):void { | ||
| 66 | this.alphaTween.finish = val; | 68 | this.alphaTween.finish = val; |
| 67 | this.alphaTween.duration = time; | 69 | this.alphaTween.duration = duration; |
| 68 | this.alphaTween.looping=loop; | 70 | this.alphaTween.looping=loop; |
| 69 | this.alphaTween.start(); | 71 | this.alphaTween.start(); |
| 70 | } | 72 | } |
| 71 | public function sizeTo(w:Number, h:Number, time:Number=0, loop:Boolean = false):void { | 73 | public function sizeTo(w:Number, h:Number, duration:Number=0, loop:Boolean = false):void { |
| 72 | this.widthTween.finish=w; | 74 | this.widthTween.finish=w; |
| 73 | this.widthTween.looping = loop; | 75 | this.widthTween.looping = loop; |
| 74 | this.widthTween.duration = time; | 76 | this.widthTween.duration = duration; |
| 75 | this.heightTween.finish = h; | 77 | this.heightTween.finish = h; |
| 76 | this.heightTween.looping = loop; | 78 | this.heightTween.looping = loop; |
| 77 | this.heightTween.duration = time; | 79 | this.heightTween.duration = duration; |
| 78 | this.widthTween.start(); | 80 | this.widthTween.start(); |
| 79 | this.heightTween.start(); | 81 | this.heightTween.start(); |
| 80 | 82 | ||
| 81 | } | 83 | } |
| 82 | public function spinTo(rot:Number= 0, time:Number = 0, loop:Boolean = false):void { | 84 | public function widthTo(width:Number, duration:Number, loop:Boolean = false):void { |
| 85 | this.widthTween.finish =width; | ||
| 86 | this.widthTween.looping = loop; | ||
| 87 | this.widthTween.duration = duration; | ||
| 88 | this.widthTween.start(); | ||
| 89 | } | ||
| 90 | public function heightTo(height:Number, duration:Number, loop:Boolean = false):void { | ||
| 91 | this.heightTween.finish =height; | ||
| 92 | this.heightTween.looping = loop; | ||
| 93 | this.heightTween.duration = duration; | ||
| 94 | this.heightTween.start(); | ||
| 95 | } | ||
| 96 | public function spinTo(rot:Number= 0, duration:Number = 0, loop:Boolean = false):void { | ||
| 83 | this.rotationTween.finish=rot; | 97 | this.rotationTween.finish=rot; |
| 84 | this.rotationTween.duration = time; | 98 | this.rotationTween.duration = duration; |
| 85 | this.rotationTween.looping = loop; | 99 | this.rotationTween.looping = loop; |
| 86 | this.rotationTween.start(); | 100 | this.rotationTween.start(); |
| 87 | } | 101 | } |
| 88 | public function xTo(x:Number, time:Number = 0, loop:Boolean = false):void { | 102 | public function xTo(x:Number, duration:Number = 0, loop:Boolean = false):void { |
| 89 | this.xTween.finish = x; | 103 | this.xTween.finish = x; |
| 90 | this.xTween.duration = time; | 104 | this.xTween.duration = duration; |
| 91 | this.xTween.looping = loop; | 105 | this.xTween.looping = loop; |
| 92 | this.xTween.start(); | 106 | this.xTween.start(); |
| 93 | } | 107 | } |
| 94 | public function yTo(y:Number, time:Number = 0, loop:Boolean = false):void { | 108 | public function yTo(y:Number, duration:Number = 0, loop:Boolean = false):void { |
| 95 | this.yTween.finish = y; | 109 | this.yTween.finish = y; |
| 96 | this.yTween.duration = time; | 110 | this.yTween.duration = duration; |
| 97 | this.yTween.looping = loop; | 111 | this.yTween.looping = loop; |
| 98 | this.yTween.start(); | 112 | this.yTween.start(); |
| 99 | } | 113 | } |
| 100 | public function set easing(ease:Function):void { | 114 | public function set easing(ease:Function):void { |
| 101 | for (var tw in this.tweenArray) { | 115 | for (var tw in this.tweenArray) { |
| 102 | tweenArray[tw].func = ease; | 116 | tweenArray[tw].func = ease; |
| 103 | } | 117 | } |
| 104 | } | 118 | } |
| 105 | public function colorTo(toColor:Number, time:Number):void { | 119 | public function colorTo(toColor:Number, duration:Number):void { |
| 106 | this.lastColor = this.transform.colorTransform.color; | 120 | this.lastColor = this.transform.colorTransform.color; |
| 107 | this.tweenToColor = toColor; | 121 | this.tweenToColor = toColor; |
| 108 | this.colorTween.duration = time; | 122 | this.colorTween.duration = duration; |
| 109 | this.colorTween.start(); | 123 | this.colorTween.start(); |
| 110 | } | 124 | } |
| 111 | private function updateColor(event:TweenEvent):void { | 125 | private function updateColor(event:TweenEvent):void { |
| 112 | this.colorTrans.color = Color.interpolateColor(this.lastColor, this.tweenToColor,event.position); | 126 | this.colorTrans.color = Color.interpolateColor(this.lastColor, this.tweenToColor,event.position); |
| 113 | updateColorTransform(); | 127 | updateColorTransform(); |
| 114 | } | 128 | } |
| 115 | public function set currentColor(clr:Number) { | 129 | public function set currentColor(clr:Number) { |
| 116 | this.lastColor = clr; | 130 | this.lastColor = clr; |
| 117 | this.colorTrans.color= clr; | 131 | this.colorTrans.color= clr; |
| 118 | updateColorTransform(); | 132 | updateColorTransform(); |
| 119 | 133 | ||
| 120 | } | 134 | } |
| 121 | public function startOrbit():void { | 135 | public function startOrbit():void { |
| 122 | this.addEventListener(Event.ENTER_FRAME,orbit); | 136 | this.addEventListener(Event.ENTER_FRAME,orbit); |
| 123 | } | 137 | } |
| 124 | public function stopOrbit():void { | 138 | public function stopOrbit():void { |
| 125 | this.removeEventListener(Event.ENTER_FRAME,orbit); | 139 | this.removeEventListener(Event.ENTER_FRAME,orbit); |
| 126 | this.orbitAngle = 0; | 140 | this.orbitAngle = 0; |
| 127 | } | 141 | } |
| 128 | private function orbit(event:Event) { | 142 | private function orbit(event:Event) { |
| 129 | this.y = this._orbitY + Math.sin(this.orbitAngle)*this._orbitDistance; | 143 | this.y = this._orbitY + Math.sin(this.orbitAngle)*this._orbitDistance; |
| 130 | this.x = this._orbitX + Math.cos(this.orbitAngle)*this._orbitDistance; | 144 | this.x = this._orbitX + Math.cos(this.orbitAngle)*this._orbitDistance; |
| 131 | this.orbitAngle += this._orbitVelocity; | 145 | this.orbitAngle += this._orbitVelocity; |
| 132 | } | 146 | } |
| 133 | public function set yEasing(ease:Function) { | 147 | public function set yEasing(ease:Function) { |
| 134 | this.yTween.func = ease; | 148 | this.yTween.func = ease; |
| 135 | } | 149 | } |
| 136 | public function set xEasing(ease:Function) { | 150 | public function set xEasing(ease:Function) { |
| 137 | this.xTween.func = ease; | 151 | this.xTween.func = ease; |
| 138 | } | 152 | } |
| 139 | public function set heightEasing(ease:Function) { | 153 | public function set heightEasing(ease:Function) { |
| 140 | this.heightTween.func = ease; | 154 | this.heightTween.func = ease; |
| 141 | } | 155 | } |
| 142 | public function set widthEasing(ease:Function) { | 156 | public function set widthEasing(ease:Function) { |
| 143 | this.widthTween.func = ease; | 157 | this.widthTween.func = ease; |
| 144 | } | 158 | } |
| 145 | public function set alphaEasing(ease:Function) { | 159 | public function set alphaEasing(ease:Function) { |
| 146 | this.alphaTween.func = ease; | 160 | this.alphaTween.func = ease; |
| 147 | } | 161 | } |
| 148 | public function set rotationEasing(ease:Function) { | 162 | public function set rotationEasing(ease:Function) { |
| 149 | this.rotationTween.func = ease; | 163 | this.rotationTween.func = ease; |
| 150 | } | 164 | } |
| 151 | public function set orbitVelocity(velocity:Number) { | 165 | public function set orbitVelocity(velocity:Number) { |
| 152 | this._orbitVelocity = velocity; | 166 | this._orbitVelocity = velocity; |
| 153 | } | 167 | } |
| 154 | public function set orbitY(y:Number) { | 168 | public function set orbitY(y:Number) { |
| 155 | this._orbitY = y; | 169 | this._orbitY = y; |
| 156 | } | 170 | } |
| 157 | public function set orbitX(x:Number) { | 171 | public function set orbitX(x:Number) { |
| 158 | this._orbitX = x; | 172 | this._orbitX = x; |
| 159 | } | 173 | } |
| 160 | public function set orbitDistance(distance:Number) { | 174 | public function set orbitDistance(distance:Number) { |
| 161 | this._orbitDistance = distance; | 175 | this._orbitDistance = distance; |
| 162 | } | 176 | } |
| 163 | public function get currentColor():Number { | 177 | public function get currentColor():Number { |
| 164 | return this.lastColor; | 178 | return this.lastColor; |
| 165 | } | 179 | } |
| 166 | private function updateColorTransform() { | 180 | private function updateColorTransform() { |
| 167 | this.transform.colorTransform = this.colorTrans; | 181 | this.transform.colorTransform = this.colorTrans; |
| 168 | } | 182 | } |
| 169 | } | 183 | } |
| 170 | } | 184 | } |