org.as3s.ProxyTween
Proxy-Based Tween Library | ProxyTween Ver.0.1
Download: http://as3s.org/uploads/proxytween/proxytween.zip
Minimum Usage
mct = new ProxyTween(mc);
mct.x = 200;
Just set properties of the proxy instead of the target.
Sample Code 1 (Click)
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import org.as3s.ProxyTween;
[SWF(width='700',height='300',backgroundColor='0xeeeeee',frameRate='60')]
public class ProxyTweenSample1 extends Sprite
{
private var sp:Sprite;
private var spt:ProxyTween;
public function ProxyTweenSample1()
{
//Target Object
sp = new Sprite();
sp.graphics.beginFill(0x333333);
sp.graphics.drawRect(-50,-50,100,100);
sp.graphics.endFill();
sp.x = 350;
sp.y = 150;
addChild(sp);
spt = new ProxyTween(sp, ProxyTween.ELASTIC);
//spt = new ProxyTween(sp, ProxyTween.EASE_IN_OUT_QUAD, 60);
spt.addEventListener(ProxyTween.COMPLETE, onTweenComplete);
stage.addEventListener(MouseEvent.CLICK, onClick);
}
private function onClick(event:MouseEvent):void {
spt.x = event.stageX;
spt.y = event.stageY;
}
private function onTweenComplete(event:Event):void {
trace("Complete!");
}
}
}
Sample Code 2 (RollOver/RollOut)
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import org.as3s.ProxyTween;
[SWF(width='700',height='300',backgroundColor='0xeeeeee',frameRate='60')]
public class ProxyTweenSample2 extends Sprite
{
private var sp:Sprite;
private var spt:ProxyTween;
private var elastic:ProxyTween;
public function ProxyTweenSample2()
{
//Target Object
sp = new Sprite();
sp.graphics.beginFill(0x333333);
sp.graphics.drawRect(-50,-50,100,100);
sp.graphics.endFill();
sp.x = 350;
sp.y = 150;
sp.alpha = 0.2;
addChild(sp);
spt = new ProxyTween(sp);
sp.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
sp.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
}
private function onMouseOver(event:MouseEvent):void {
spt.alpha = 1;
}
private function onMouseOut(event:MouseEvent):void {
spt.alpha = 0.2;
}
}
}
org.as3s.ProxyTween
* org.as3s.Tween
*
* ProxyTween Class for ActionScript3.0
*
* Download: http://as3s.org/uploads/proxytween/proxytween.zip
*
* Copyright (c) 2009 Hisato Ogata
* Licensed under the MIT License
* http://www.opensource.org/licenses/mit-license.php
*
* @langversion ActionScript 3.0
* @playerversion Flash 9
*
* @version 0.1
* @author Hisato Ogata
* @see http://as3s.org/proxytween/
*
* @example usage:
* <listing version="3.0">
*
* // Standard Tweening
* var mct:ProxyTween = new ProxyTween(mc, ProxyTween.EASE_IN_OUT_QUAD, 24);
* mct.addEventListener(Tween.COMPLETE, onTweenComplete);
* mct.x = 300;
* mct.y = 200;
*
* // Time-Based Tweening
* var mct:ProxyTween = new ProxyTween(mc, ProxyTween.EASE_IN_OUT_QUAD, 1, null, true);
*
* // Proportional Easing
* var tween:ProxyTween = new ProxyTween(mc, {x:100}, ProxyTween.EASING);
*
* // Proportional Easing with custom arguments
* var tween:ProxyTween = new ProxyTween(mc, {x:100}, ProxyTween.EASING, 0, [0.2]);
*
* </listing>
*/
package org.as3s
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.IEventDispatcher;
import flash.utils.Proxy;
import flash.utils.flash_proxy;
import flash.utils.getTimer;
public dynamic class ProxyTween extends Proxy implements IEventDispatcher
{
//Event
public static const COMPLETE:String = "complete";
//Basic Easing
public static const EASING:String = "easing";
public static const UNIFORM:String = "uniform";
public static const ACCELERATE:String = "accelerate";
public static const BOUNCE:String = "bounce";
public static const ELASTIC:String = "elastic";
//Robert Penner's Easing Equations
public static const LINEAR:String = "easeNone";
public static const EASE_NONE:String = "easeNone";
public static const EASE_IN_QUAD:String = "easeInQuad";
public static const EASE_OUT_QUAD:String = "easeOutQuad";
public static const EASE_IN_OUT_QUAD:String = "easeInOutQuad";
public static const EASE_OUT_IN_QUAD:String = "easeOutInQuad";
public static const EASE_IN_CUBIC:String = "easeInCubic";
public static const EASE_OUT_CUBIC:String = "easeOutCubic";
public static const EASE_IN_OUT_CUBIC:String = "easeInOutCubic";
public static const EASE_OUT_IN_CUBIC:String = "easeOutInCubic";
public static const EASE_IN_QUART:String = "easeInQuart";
public static const EASE_OUT_QUART:String = "easeOutQuart";
public static const EASE_IN_OUT_QUART:String = "easeInOutQuart";
public static const EASE_OUT_IN_QUART:String = "easeOutInQuart";
public static const EASE_IN_QUINT:String = "easeInQuint";
public static const EASE_OUT_QUINT:String = "easeOutQuint";
public static const EASE_IN_OUT_QUINT:String = "easeInOutQuint";
public static const EASE_OUT_IN_QUINT:String = "easeOutInQuint";
public static const EASE_IN_SINE:String = "easeInSine";
public static const EASE_OUT_SINE:String = "easeOutSine";
public static const EASE_IN_OUT_SINE:String = "easeInOutSine";
public static const EASE_OUT_IN_SINE:String = "easeOutInSine";
public static const EASE_IN_CIRC:String = "easeInCirc";
public static const EASE_OUT_CIRC:String = "easeOutCirc";
public static const EASE_IN_OUT_CIRC:String = "easeInOutCirc";
public static const EASE_OUT_IN_CIRC:String = "easeOutInCirc";
public static const EASE_IN_EXPO:String = "easeInExpo";
public static const EASE_OUT_EXPO:String = "easeOutExpo";
public static const EASE_IN_OUT_EXPO:String = "easeInOutExpo";
public static const EASE_OUT_IN_EXPO:String = "easeOutInExpo";
public static const EASE_IN_ELASTIC:String = "easeInElastic";
public static const EASE_OUT_ELASTIC:String = "easeOutElastic";
public static const EASE_IN_OUT_ELASTIC:String = "easeInOutElastic";
public static const EASE_OUT_IN_ELASTIC:String = "easeOutInElastic";
public static const EASE_IN_BACK:String = "easeInBack";
public static const EASE_OUT_BACK:String = "easeOutBack";
public static const EASE_IN_OUT_BACK:String = "easeInOutBack";
public static const EASE_OUT_IN_BACK:String = "easeOutInBack";
public static const EASE_IN_BOUNCE:String = "easeInBounce";
public static const EASE_OUT_BOUNCE:String = "easeOutBounce";
public static const EASE_IN_OUT_BOUNCE:String = "easeInOutBounce";
public static const EASE_OUT_IN_BOUNCE:String = "easeOutInBounce";
private static var frame:Sprite = new Sprite();
private var _dispatcher:EventDispatcher;
public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void {
_dispatcher.addEventListener(type, listener, useCapture, priority);
}
public function dispatchEvent(evt:Event):Boolean {
return _dispatcher.dispatchEvent(evt);
}
public function hasEventListener(type:String):Boolean {
return _dispatcher.hasEventListener(type);
}
public function removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void {
_dispatcher.removeEventListener(type, listener, useCapture);
}
public function willTrigger(type:String):Boolean {
return _dispatcher.willTrigger(type);
}
private var _target:Object;
private var _func:Function;
private var _equation:Function;
private var _duration:Number = Number.POSITIVE_INFINITY;
private var _args:Array;
private var _useSeconds:Boolean = false;
private var _params:Object;
private var _frames:int = 0;
private function get time():int {
return _useSeconds ? getTimer() : _frames;
}
public function ProxyTween(target:Object, func:* = null, duration:Number = 0, args:Array = null, useSeconds:Boolean = false):void
{
_dispatcher = new EventDispatcher(this);
_target = target;
init(func, duration, args, useSeconds);
}
public function init(func:* = null, duration:Number = 0, args:Array = null, useSeconds:Boolean = false):void
{
if (ProxyTween[func] is Function) { //Basic Easing
_func = ease;
_equation = ProxyTween[func];
}
else if (ProxyTweenEquations[func] is Function) { //Robert Penner's Tween
_func = tween;
_equation = ProxyTweenEquations[func];
}
else if (func is Function) { //Custom Easing
_func = ease;
_equation = func;
}
else { //Default
_func = ease;
_equation = easing;
}
_duration = useSeconds ? duration*1000 : duration;
_args = args != null ? args.concat() : [];
_useSeconds = useSeconds;
_params = {};
}
public function reset():void
{
_params = {};
frame.removeEventListener(Event.ENTER_FRAME, update);
}
override flash_proxy function getProperty(name:*):*
{
return _target[name];
}
override flash_proxy function setProperty(name:*, value:*):void
{
if (_params[name]==null) {
_params[name] = new Object();
}
var p:Object = _params[name];
p.value = p.begin = _target[name];
p.end = value;
p.change = p.end - p.begin;
p.time = time;
frame.addEventListener(Event.ENTER_FRAME, update);
}
private function update(event:Event):void {
if (_func.apply(this, _args)) {
frame.removeEventListener(Event.ENTER_FRAME, update);
dispatchEvent(new Event(COMPLETE));
} else {
_frames++;
}
}
/**
* Main Function for Robert Penner's Tween
*/
private function tween():Boolean {
var complete:Boolean = true;
for (var name:String in _params) {
var p:Object = _params[name];
var t:int = time - p.time;
p.value = _equation(t, p.begin, p.change, _duration);
if (t>=_duration) {
_target[name] = p.end;
delete _params[name];
}
else {
_target[name] = p.value;
complete = false;
}
}
return complete;
}
/**
* Main Function for Basic Easing
*/
private function ease():Boolean {
var complete:Boolean = true;
for (var name:String in _params) {
var p:Object = _params[name];
_target[name] = p.value;
if (_equation.apply(p, _args)) {
_target[name] = p.end;
delete _params[name];
}
else {
complete = false;
}
}
return complete;
}
/**
* Basic Fractional Easing Function
*
* @param fraction Specifies the value of the fraction.
* @param threshold Specifies the value of the threshold of the convergence.
*
*/
public static var easing:Function = function(fraction:Number = 0.2, threshold:Number = 0.05):Boolean {
var distance:Number = this.end - this.value;
var v:Number = distance*fraction;
if ((v<0 ? -v : v) <threshold) { // (v<0 ? -v : v) is an optimization of Math.abs(v)
return true;
} else {
this.value += v;
return false;
}
};
/**
* Non-Accelerated Motion Function
*
* @param v Specifies the velocity of the motion.
*
*/
public static var uniform:Function = function(v:Number = 1):Boolean {
if (v*this.change<0) v = -v;
var distance:Number = this.end - this.value;
if (v>0 && v>distance || v<0 && v<distance) {
return true;
} else {
this.value += v;
return false;
}
};
/**
* Accelerated Motion Function
*
* @param a Specifies the acceleration of the motion.
* @param iv Specifies the initial velocity of the motion.
*
*/
public static var accelerate:Function = function(a:Number = 1, iv:Number = 0):Boolean {
if (a*this.change<0) a = -a;
this.v = this.v!=null ? this.v + a : iv + a;
var distance:Number = this.end - this.value;
if (this.v>0 && this.v>distance || this.v<0 && this.v<distance) {
return true;
} else {
this.value += this.v;
return false;
}
};
/**
* Bounce Motion Function
*
* @param a Specifies the acceleration of the motion.
* @param iv Specifies the initial velocity of the motion.
* @param reflect Specifies the reflectance of the boundary.
*
*/
public static var bounce:Function = function(a:Number = 1, iv:Number = 0, reflect:Number = 1):Boolean {
if (a*this.change<0) a = -a;
if (this.v==null) this.v = iv;
var distance:Number = this.end - this.value;
if (a>0 && this.v>0 && this.v>distance || a<0 && this.v<0 && this.v<distance) {
if ((this.v<0 ? -this.v : this.v) <= (a<0 ? -a : a)) {
return true;
} else {
this.v *= -reflect;
this.v += a;
return false;
}
} else {
this.v += a;
this.value += this.v;
return false;
}
};
/**
* Elastic Motion Function
*
* @param k Specifies the constant of spring.
* @param damper Specifies the damper property of the motion.
* @param iv Specifies the initial velocity of the motion.
* @param threshold Specifies the value of the threshold of the convergence.
*
*/
public static var elastic:Function = function(k:Number = 0.2, damper:Number = 0.2, iv:Number = 0, threshold:Number = 0.05):Boolean {
if (this.v==null) this.v = iv;
var distance:Number = this.end - this.value;
var a:Number = k*distance - damper*this.v;
this.v += a;
if ((this.v<0 ? -this.v : this.v) <threshold && (distance<0 ? -distance : distance) <threshold) {
return true;
} else {
this.value += this.v;
return false;
}
};
}
}
[...] 対象オブジェクトのプロパティを直接変更する感覚でトゥイーンが実現できる Proxy型Tweenライブラリ ProxyTween ver.0.1を公開しました。 [...]
Proxy型Tweenライブラリ ProxyTween at AS3S.ORG
31 Jul 09 at 6:11 pm