Skip to content
This repository has been archived by the owner on Sep 18, 2018. It is now read-only.

Commit

Permalink
changed PrimitiveTween render/update system 2x speed boost
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewfitz committed Jul 21, 2009
1 parent 87fc309 commit d9e35e7
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 28 deletions.
6 changes: 5 additions & 1 deletion changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ Desuade Library Changelog
Beta 3:

Motion:

-200% speed boost! Renders 2x as many tweens without performance hit
-removed individual PrimitiveTween update listeners - now uses a BasicTween loop update
-added render() methods for PrimitiveTweens
-PrimitiveTweens now do not run an update event automatically - you can use your own loop/timer/event to run render()

Partigen:
-Fix: addColorTween always using 'color'

Expand Down
39 changes: 38 additions & 1 deletion com/desuade/motion/tweens/BasicTween.as
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ THE SOFTWARE.

package com.desuade.motion.tweens {

//Easing functions can be included with import fl.motion.easing.*
import flash.display.*;
import com.desuade.debugging.*
import com.desuade.motion.events.*
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.utils.getTimer;

/**
* A very basic tween that allows you to tween a given value on any object to a new value.
Expand All @@ -41,11 +42,21 @@ package com.desuade.motion.tweens {
*/
public class BasicTween extends EventDispatcher {

/**
* @private
*/
internal static var inited:Boolean = false;

/**
* @private
*/
protected static var _tweenholder:Object = {};

/**
* @private
*/
internal static var _sprite:Sprite = new Sprite();

/**
* @private
*/
Expand All @@ -61,6 +72,24 @@ package com.desuade.motion.tweens {
*/
protected var _active:Boolean = false;

/**
* @private
*/
protected static function init():void {
_sprite.addEventListener(Event.ENTER_FRAME, update, false, 0, true);
inited = true;
}

/**
* @private
*/
protected static function update($u:Object):void {
var times:int = getTimer();
for each (var tween:PrimitiveTween in _tweenholder) {
tween.render(times);
}
}

/**
* <p>The constructor accepts an object that has all the paramaters needed to create a new tween.</p>
* <p>Paramaters for the tween object:</p>
Expand All @@ -83,6 +112,7 @@ package com.desuade.motion.tweens {
*/
public function BasicTween($tweenObject:Object) {
super();
if(!inited) init();
_tweenconfig = $tweenObject;
Debug.output('motion', 40001);
}
Expand Down Expand Up @@ -129,6 +159,13 @@ package com.desuade.motion.tweens {
return _tweenconfig;
}

/**
* Gets the primitivetween id
*/
public function get ptid():int{
return _tweenID;
}

/**
* This creates a new tween that is a duplicate clone of this.
*
Expand Down
13 changes: 7 additions & 6 deletions com/desuade/motion/tweens/PrimitiveBezierTween.as
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,18 @@ package com.desuade.motion.tweens {
}

/**
* @private
* This renders the tween. It calculates and sets the new value, and checks to see if the tween is finished.
*
* @param time The current getTimer() time.
*/
protected override function update(u:Object):void {
var tmr:int = getTimer() - starttime;
if(tmr >= duration){
public override function render($time:int):void {
$time -= starttime;
if($time >= duration){
target[property] = value;
end();
} else {
var nres:Number;
var res:Number = ease(tmr, startvalue, difvalue, duration);
var res:Number = ease($time, startvalue, difvalue, duration);
var easeposition:Number = (res-startvalue)/(value-startvalue);
if(bezier.length == 1) {
nres = startvalue + (easeposition*(2*(1-easeposition)*(bezier[0]-startvalue)+(easeposition*difvalue)));
Expand All @@ -106,7 +108,6 @@ package com.desuade.motion.tweens {
target[property] = nres;
dispatchEvent(new TweenEvent(TweenEvent.UPDATED, {primitiveTween:this}));
}

}

}
Expand Down
12 changes: 7 additions & 5 deletions com/desuade/motion/tweens/PrimitiveMultiTween.as
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,20 @@ package com.desuade.motion.tweens {
}

/**
* @private
* This renders the tween. It calculates and sets the new value, and checks to see if the tween is finished.
*
* @param time The current getTimer() time.
*/
protected override function update($u:Object):void {
var tmr:int = getTimer() - starttime;
if(tmr >= duration){
public override function render($time:int):void {
$time -= starttime;
if($time >= duration){
for (var i:int = 0; i < arrayObject.props.length; i++) {
target[arrayObject.props[i]] = arrayObject.values[i];
}
end();
} else {
for (var k:int = 0; k < arrayObject.props.length; k++) {
target[arrayObject.props[k]] = ease(tmr, arrayObject.startvalues[k], arrayObject.difvalues[k], duration);
target[arrayObject.props[k]] = ease($time, arrayObject.startvalues[k], arrayObject.difvalues[k], duration);
}
dispatchEvent(new TweenEvent(TweenEvent.UPDATED, {primitiveTween:this}));
}
Expand Down
22 changes: 7 additions & 15 deletions com/desuade/motion/tweens/PrimitiveTween.as
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,8 @@ THE SOFTWARE.

package com.desuade.motion.tweens {

import flash.display.*;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.utils.getTimer;

import com.desuade.motion.eases.Linear;
Expand All @@ -51,11 +48,6 @@ package com.desuade.motion.tweens {
*/
public static var _count:int = 1;

/**
* @private
*/
internal static var _sprite:Sprite = new Sprite();

/**
* This is the unique internal id of the tween.
*/
Expand Down Expand Up @@ -125,7 +117,6 @@ package com.desuade.motion.tweens {
difvalue = (startvalue > value) ? (value-startvalue) : -(startvalue-value);
}
dispatchEvent(new TweenEvent(TweenEvent.STARTED, {primitiveTween:this}));
_sprite.addEventListener(Event.ENTER_FRAME, update, false, 0, true);
Debug.output('motion', 50001, [id]);
}

Expand All @@ -136,21 +127,22 @@ package com.desuade.motion.tweens {
*/
public function end($broadcast:Boolean = true):void {
Debug.output('motion', 50002, [id]);
_sprite.removeEventListener(Event.ENTER_FRAME, update);
if($broadcast) dispatchEvent(new TweenEvent(TweenEvent.ENDED, {primitiveTween:this}));
delete this;
}

/**
* @private
* This renders the tween. It calculates and sets the new value, and checks to see if the tween is finished.
*
* @param time The current getTimer() time.
*/
protected function update($u:Object):void {
var tmr:int = getTimer() - starttime;
if(tmr >= duration){
public function render($time:int):void {
$time -= starttime;
if($time >= duration){
target[property] = value;
end();
} else {
target[property] = ease(tmr, startvalue, difvalue, duration);
target[property] = ease($time, startvalue, difvalue, duration);
dispatchEvent(new TweenEvent(TweenEvent.UPDATED, {primitiveTween:this}));
}
}
Expand Down

0 comments on commit d9e35e7

Please sign in to comment.