Working towards some easy-to-use score animation type things.

Actionscript's lack of method overloading sucks. I can't have more
than one constructor, and factory methods will have to have different names.


git-svn-id: svn+ssh://src.earth.threerings.net/nenya/trunk@205 ed5b42cb-e716-0410-a449-f6a68f950b19
This commit is contained in:
Ray Greenwell
2007-04-24 21:22:54 +00:00
parent b77c72220e
commit 97ff4caa41
2 changed files with 97 additions and 0 deletions
@@ -0,0 +1,21 @@
package com.threerings.flash {
public class AnimationAdapter extends Animation
{
/**
* @param host the display object we'll be animating.
* @param enterFrame called on every frame: function (elapsed :Number) :void
*/
public function AnimationAdapter (enterFrame :Function)
{
_enterFrame = enterFrame;
}
override protected function enterFrame () :void
{
_enterFrame(_now - _start);
}
protected var _enterFrame :Function;
}
}
@@ -0,0 +1,76 @@
package com.threerings.flash {
import flash.events.Event;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
public class FloatingTextAnimation extends TextField
{
public static function create (
text :String, duration :Number = 1000, dy :int = -10,
format :TextFormat = null) :FloatingTextAnimation
{
var fta :FloatingTextAnimation = new FloatingTextAnimation();
fta.defaultTextFormat = format;
fta.autoSize = TextFieldAutoSize.CENTER;
fta.text = text;
fta.width = fta.textWidth + 5;
fta.height = fta.textHeight + 4;
fta.duration = duration;
fta.dy = dy;
return fta;
}
public var duration :Number;
public var dy :Number;
public function FloatingTextAnimation ()
{
addEventListener(Event.ADDED_TO_STAGE, handleAdded);
addEventListener(Event.REMOVED_FROM_STAGE, handleRemoved);
_anim = new AnimationAdapter(enterFrame);
}
override public function set x (val :Number) :void
{
super.x = (val - width/2);
}
override public function set y (val :Number) :void
{
_startY = val;
super.y = (val - height/2);
}
protected function enterFrame (elapsed :Number) :void
{
var perc :Number = elapsed / duration;
if (perc >= 1) {
parent.removeChild(this);
return;
}
alpha = 1 - perc;
this.y = _startY + (dy * perc);
}
protected function handleAdded (... ignored) :void
{
_anim.start();
}
protected function handleRemoved (... ignored) :void
{
_anim.stop();
}
protected var _startY :Number;
protected var _anim :AnimationAdapter;
}
}