| 1 | /** |
|---|
| 2 | * A couple of commonly used animation functions. |
|---|
| 3 | * |
|---|
| 4 | * @author Jeroen Wijering |
|---|
| 5 | * @version 1.12 |
|---|
| 6 | **/ |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | class com.jeroenwijering.utils.Animations { |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | /** |
|---|
| 13 | * Fadein function for MovieClip. |
|---|
| 14 | * |
|---|
| 15 | * @param tgt The Movieclip to fade in. |
|---|
| 16 | * @param end The final alpha value. |
|---|
| 17 | * @param spd The amount of alpha change per frame. |
|---|
| 18 | **/ |
|---|
| 19 | public static function fadeIn(tgt:MovieClip,end:Number,spd:Number) { |
|---|
| 20 | arguments.length < 3 ? spd = 20: null; |
|---|
| 21 | arguments.length < 2 ? end = 100: null; |
|---|
| 22 | tgt._visible = true; |
|---|
| 23 | tgt.onEnterFrame = function() { |
|---|
| 24 | if(this._alpha > end-spd) { |
|---|
| 25 | delete this.onEnterFrame; |
|---|
| 26 | this._alpha = end; |
|---|
| 27 | } else { |
|---|
| 28 | this._alpha += spd; |
|---|
| 29 | } |
|---|
| 30 | }; |
|---|
| 31 | }; |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | /** |
|---|
| 35 | * Fadeout function for MovieClip. |
|---|
| 36 | * |
|---|
| 37 | * @param tgt The Movieclip to fade out. |
|---|
| 38 | * @param end The final alpha value. |
|---|
| 39 | * @param spd The amount of alpha change per frame. |
|---|
| 40 | * @param rmv Removing the clip off stage switch. |
|---|
| 41 | **/ |
|---|
| 42 | public static function fadeOut(tgt:MovieClip,end:Number, |
|---|
| 43 | spd:Number,rmv:Boolean) { |
|---|
| 44 | arguments.length < 4 ? rmv = false: null; |
|---|
| 45 | arguments.length < 3 ? spd = 20: null; |
|---|
| 46 | arguments.length < 2 ? end = 0: null; |
|---|
| 47 | tgt.onEnterFrame = function() { |
|---|
| 48 | if(this._alpha < end+spd) { |
|---|
| 49 | delete this.onEnterFrame; |
|---|
| 50 | this._alpha = end; |
|---|
| 51 | end == 0 ? this._visible = false: null; |
|---|
| 52 | rmv == true ? this.removeMovieClip(): null; |
|---|
| 53 | } else { |
|---|
| 54 | this._alpha -= spd; |
|---|
| 55 | } |
|---|
| 56 | }; |
|---|
| 57 | }; |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | /** |
|---|
| 61 | * Crossfade a given MovieClip through 0. |
|---|
| 62 | * |
|---|
| 63 | * @param tgt The Movieclip to crossfade. |
|---|
| 64 | * @param alp The final alpha value. |
|---|
| 65 | **/ |
|---|
| 66 | public static function crossfade(tgt:MovieClip, alp:Number) { |
|---|
| 67 | var phs = "out"; |
|---|
| 68 | var pct = alp/5; |
|---|
| 69 | tgt.onEnterFrame = function() { |
|---|
| 70 | if(phs == "out") { |
|---|
| 71 | this._alpha -= pct; |
|---|
| 72 | if (this._alpha < 1) { phs = "in"; } |
|---|
| 73 | } else { |
|---|
| 74 | this._alpha += pct; |
|---|
| 75 | this._alpha >= alp ? delete this.onEnterFrame : null; |
|---|
| 76 | } |
|---|
| 77 | }; |
|---|
| 78 | }; |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | /** |
|---|
| 82 | * Smoothly move a Movielip to a certain position. |
|---|
| 83 | * |
|---|
| 84 | * @param tgt The Movielip to move. |
|---|
| 85 | * @param xps The x destination. |
|---|
| 86 | * @param yps The y destination. |
|---|
| 87 | * @param spd The movement speed (1 - 2). |
|---|
| 88 | **/ |
|---|
| 89 | public static function easeTo(tgt:MovieClip,xps:Number,yps:Number, |
|---|
| 90 | spd:Number) { |
|---|
| 91 | arguments.length < 4 ? spd = 2: null; |
|---|
| 92 | tgt.onEnterFrame = function() { |
|---|
| 93 | this._x = xps-(xps-this._x)/(1+1/spd); |
|---|
| 94 | this._y = yps-(yps-this._y)/(1+1/spd); |
|---|
| 95 | if (this._x>xps-1 && this._x<xps+1 && |
|---|
| 96 | this._y>yps-1 && this._y<yps+1) { |
|---|
| 97 | this._x = Math.round(xps); |
|---|
| 98 | this._y = Math.round(yps); |
|---|
| 99 | delete this.onEnterFrame; |
|---|
| 100 | } |
|---|
| 101 | }; |
|---|
| 102 | }; |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | /** |
|---|
| 106 | * Typewrite text into a textfield. |
|---|
| 107 | * |
|---|
| 108 | * @param tgt Movieclip that has a 'tf' TextField. |
|---|
| 109 | * @param txt The textstring to write; uses current content if omitted. |
|---|
| 110 | * @param spd The speed of typing (1 - 2). |
|---|
| 111 | **/ |
|---|
| 112 | public static function easeText(tgt:MovieClip,txt:String,spd:Number) { |
|---|
| 113 | if (arguments.length < 2) { |
|---|
| 114 | tgt.str = tgt.tf.text; |
|---|
| 115 | tgt.hstr = tgt.tf.htmlText; |
|---|
| 116 | } else { |
|---|
| 117 | tgt.str = tgt.hstr = txt; |
|---|
| 118 | } |
|---|
| 119 | if (arguments.length < 3) { spd = 1.5; } |
|---|
| 120 | tgt.tf.text = ""; |
|---|
| 121 | tgt.i = 0; |
|---|
| 122 | tgt.onEnterFrame = function() { |
|---|
| 123 | this.tf.text = this.str.substr(0, this.str.length - |
|---|
| 124 | Math.floor((this.str.length - this.tf.text.length)/spd)); |
|---|
| 125 | if(this.tf.text == this.str) { |
|---|
| 126 | this.tf.htmlText = this.hstr; |
|---|
| 127 | delete this.onEnterFrame; |
|---|
| 128 | } |
|---|
| 129 | this.i++; |
|---|
| 130 | }; |
|---|
| 131 | }; |
|---|
| 132 | |
|---|
| 133 | |
|---|
| 134 | } |
|---|