Packagecom.gskinner.motion
Classpublic class GTween
InheritanceGTween Inheritance flash.events.EventDispatcher

GTween ©2008 Grant Skinner, gskinner.com. Visit www.gskinner.com/blog for documentation, updates and more free code. Licensed under the MIT license. See the source file header for more information.
GTween is a light-weight instance oriented tween engine. This means that you instantiate tweens for specific purposes, and then reuse, update or discard them. This is different than centralized tween engines where you "register" tweens with a single object. This provides a more familiar and useful interface for object oriented programmers.

In addition to a more traditional setProperty/setProperties tweening interface, GTween also provides a unique proxy interface to tween and access properties of target objects in a more dynamic fashion. This allows you to work with properties directly, and mostly ignore the details of the tween. The proxy "stands in" for your object when working with tweened properties. For example, you can modify destination values (the value you are tweening to), in the middle of a tween. You can also access them dynamically, like so:

mySpriteTween.proxy.rotation += 50;

Assuming no destination value has been set for rotation previously, the above example will get the current rotation from the target, add 50 to it, set it as the destination value for rotation, and start the tween. If the tween has already started, it will adjust for the new values. This is a hugely powerful feature that requires a bit of exploring to completely understand the potential. See the documentation for the "proxy" property for more information.

For a light weight engine (<5kb), GTween boasts a number of advanced features:



Public Properties
 PropertyDefined by
  assignmentProperty : String
Allows you to reassign the .target object back to a property specified by assignmentProperty of an object specified by assignmentTarget.
GTween
  assignmentTarget : Object
Allows you to reassign the .target object back to a property specified by assignmentProperty of an object specified by assignmentTarget.
GTween
  autoHide : Boolean
Indicates whether the target's visible property should automatically be set to false when its alpha value is tweened to 0 or less.
GTween
  autoPlay : Boolean = true
Indicates whether the tween should automatically play when a destination value is changed.
GTween
  autoReverse : Boolean = false
Specifies whether the tween should automatically reverse its initial and destination property values when it reaches its end.
GTween
  delay : Number
The length of the delay in frames or seconds (depending on the timingMode).
GTween
  duration : Number
The length of the tween in frames or seconds (depending on the timingMode).
GTween
  lastProgressPoint : *
[read-only] Returns the name of the last progress point that was passed, or null if none.
GTween
  nextTween : GTween
Specifies another GTween instance that will have .play() called on it when this tween completes.
GTween
  paused : Boolean = false
[static] Setting this to true pauses all tween instances.
GTween
  position : Number
Gets and sets the position in the tween in frames or seconds (depending on the timingMode).
GTween
  proxy : Object
[read-only] The proxy object allows you to work with the properties and methods of the target object directly through GTween.
GTween
  state : String
[read-only] Returns the current positional state of the tween.
GTween
  target : Object
The target object to tween.
GTween
  timeInterval : uint
[static] Sets the time in milliseconds between updates when timingMode is set to GTween.TIME ("time").
GTween
  timingMode : String
[static] Indicates how GTween should deal with timing.
GTween
  transitionFunction : Function
The transition function to use for calculating the tween.
GTween
  useSmartRotation : Boolean = true
When true, the tween will always rotate in the shortest direction to reach the destination rotation.
GTween
Public Methods
 MethodDefined by
  
GTween(target:Object = null, duration:Number = 10, properties:Object = null, tweenProperties:Object = null)
Constructs a new GTween instance.
GTween
  
addChild(child:GTween, synchDuration:Boolean = true):void
Adds a child tween.
GTween
  
addProgressPoint(position:Number, data:*):void
Adds a tween position at which to generate a progress event.
GTween
  
beginning():void
Jumps the tween to its beginning and pauses it.
GTween
  
deleteProperty(propertyName:String):Boolean
Removes a destination value from the tween.
GTween
  
end(endNextTween:Boolean = true):void
Jumps the tween to its end.
GTween
  
getProperty(propertyName:String):Number
Returns the destination value for the specified property if one exists.
GTween
  
invalidate():void
Invalidate forces the tween to repopulate all of the initial properties from the target object, and start playing if autoplay is set to true.
GTween
  
pause():void
Pauses the tween by stopping tick from being automatically called.
GTween
  
play():void
Plays a tween by incrementing the position property each frame.
GTween
  
removeChild(child:GTween):void
Removes a child tween.
GTween
  
removeProgressPoint(data:*):void
Removes the progress point with the specified name.
GTween
  
reset():void
Resets the tween, pausing it, deleting all destination values, resetting the tween position to the beginning, and nulling the lastProgressPoint.
GTween
  
reverse(reverseChildren:Boolean = true):void
Reverses the tween by swapping its initial and destination properties.
GTween
  
setAssignment(assignmentTarget:Object = null, assignmentProperty:String = null):void
Allows you to tween objects that require re-assignment whenever they are modified by reassigning the target object to a specified property of another object.
GTween
  
setProperties(properties:Object):void
Shorthand method for making multiple setProperty calls quickly.
GTween
  
setProperty(propertyName:String, value:Number):void
Sets the numeric destination value for a property on the target object that you would like to tween.
GTween
  
setTweenProperties(tweenProperties:Object):void
Shortcut method for setting multiple properties on the tween object quickly.
GTween
Public Constants
 ConstantDefined by
  BEGINNING : String = "beginning"
[static] Constant for the BEGINNING state.
GTween
  DELAY_PHASE : String = "delayPhase"
[static] Constant for the DELAY_PHASE state.
GTween
  END : String = "end"
[static] Constant for the END state.
GTween
  FRAME : String = "frame"
[static] Constant for the FRAME timingMode.
GTween
  HYBRID : String = "hybrid"
[static] Constant for the HYBRID timingMode.
GTween
  PROGRESS : String = "progress"
[static] Constant for the PROGRESS event type.
GTween
  TIME : String = "time"
[static] Dispatched when a tween activates and first starts running.
GTween
  TWEEN_PHASE : String = "tweenPhase"
[static] Constant for the TWEEN_PHASE state.
GTween
Property detail
assignmentPropertyproperty
public var assignmentProperty:String

Allows you to reassign the .target object back to a property specified by assignmentProperty of an object specified by assignmentTarget. Useful for tweening elements like colorTransform that need to be reassigned to have an effect.

See setAssignment() for more information.

See also

assignmentTargetproperty 
public var assignmentTarget:Object

Allows you to reassign the .target object back to a property specified by assignmentProperty of an object specified by assignmentTarget. Useful for tweening elements like colorTransform that need to be reassigned to have an effect.

See setAssignment() for more information.

See also

autoHideproperty 
autoHide:Boolean  [read-write]

Indicates whether the target's visible property should automatically be set to false when its alpha value is tweened to 0 or less. Only affects objects with a visible property.

Implementation
    public function get autoHide():Boolean
    public function set autoHide(value:Boolean):void
autoPlayproperty 
public var autoPlay:Boolean = true

Indicates whether the tween should automatically play when a destination value is changed.

autoReverseproperty 
public var autoReverse:Boolean = false

Specifies whether the tween should automatically reverse its initial and destination property values when it reaches its end. Example: this could be used to create a tween that automatically plays forward and in reverse continually with:
myTween.autoReverse = true;
myTween.nextTween = myTween; // automatically play this tween again when it ends

delayproperty 
delay:Number  [read-write]

The length of the delay in frames or seconds (depending on the timingMode). The delay occurs before a tween reads initial values or starts playing.

Implementation
    public function get delay():Number
    public function set delay(value:Number):void
durationproperty 
duration:Number  [read-write]

The length of the tween in frames or seconds (depending on the timingMode). Setting this will also update any child transitions that have synchDuration set to true.

Implementation
    public function get duration():Number
    public function set duration(value:Number):void
lastProgressPointproperty 
lastProgressPoint:*  [read-only]

Returns the name of the last progress point that was passed, or null if none.

Implementation
    public function get lastProgressPoint():*
nextTweenproperty 
public var nextTween:GTween

Specifies another GTween instance that will have .play() called on it when this tween completes.

pausedproperty 
paused:Boolean = false  [read-write]

Setting this to true pauses all tween instances. This does not affect individual tweens' .paused property.

Implementation
    public static function get paused():Boolean
    public function set paused(value:Boolean):void
positionproperty 
position:Number  [read-write]

Gets and sets the position in the tween in frames or seconds (depending on the timingMode). The value will be between -delay and duration (beginning and end of tween respectively).

Note: Normally this value would be set and modified automatically by the tween, but is provided for advanced users.

Implementation
    public function get position():Number
    public function set position(value:Number):void
proxyproperty 
proxy:Object  [read-only]

The proxy object allows you to work with the properties and methods of the target object directly through GTween. Numeric property assignments will be used by GTween as destination values. The proxy will return GTween destination values when they are set, or the target's property values if they are not. Delete operations on properties will result in a deleteProperty call. All other property access and method execution through proxy will be passed directly to the target object.

Example 1: Equivalent to calling myGTween.setProperty("scaleY",2.5):
myGTween.proxy.scaleY = 2.5;

Example 2: Gets the current rotation value from the target object (because it hasn't been set yet on the GTween), adds 100 to it, and then calls setProperty on the GTween instance with the appropriate value:
myGTween.proxy.rotation += 100;

Example 3: Sets a destination value (through setProperty) for scaleX, then retrieves it from GTween (because it will always return destination values when available):
trace(myGTween.proxy.scaleX); // 1 (value from target, because no destination value is set)
myGTween.proxy.scaleX = 2; // set a destination value
trace(myGTween.proxy.scaleX); // 2 (destination value from GTween)
trace(myGTween.target.scaleX); // 1 (current value from target)


Example 4: Property deletions only affect destination properties on GTween, not the target object:
myGTween.proxy.rotation = 50; // set a destination value
trace(myGTween.proxy.rotation); // 50 (destination value from GTween)
delete(myGTween.proxy.rotation); // delete the destination value
trace(myGTween.proxy.rotation); // 0 (current value from target)


Example 5: Non-numeric property access is passed through to the target:
myGTween.proxy.blendMode = "multiply"; // passes value assignment through to the target
trace(myGTween.target.blendMode); // "multiply" (value from target)
trace(myGTween.proxy.blendMode); // "multiply" (value passed through from target)


Example 6: Method calls are passed through to target:
myGTween.proxy.gotoAndStop(30); // gotoAndStop(30) will be called on the target

Implementation
    public function get proxy():Object
stateproperty 
state:String  [read-only]

Returns the current positional state of the tween. This does not indicate if the tween is paused - use the .paused property for this. Possible values are: GTween.BEGINNING, GTween.DELAY_PHASE, GTween.TWEEN_PHASE, GTween.END
The beginning state indicates the tween either has not been played. The tween's position will equal -delay.
The delayPhase state indicates that the tween is active (running), but is currently delaying prior to initing. The tween's position is less than 0. Note that it may be paused.
The tweenPhase state indicates that the tween has inited, and is tweening the property values. Note that it may be paused.
The end state indicates that the tween has completed playing. Setting any new properties on the tween will reset it, and set its state to beginning.

New tweens with autoplay set to false start with a state of BEGINNING. When first played, a tween will have a state of either DELAY_PHASE (if delay > 0) or TWEEN_PHASE (if delay == 0). When the delay ends, and tweening begins, the state will change to TWEEN_PHASE. When the tween reaches its end (position == duration), the state will be set to END. If you change any destination properties on an ended tween, its state will be set back to BEGINNING.

Implementation
    public function get state():String
targetproperty 
target:Object  [read-write]

The target object to tween. This can be any kind of object.

Implementation
    public function get target():Object
    public function set target(value:Object):void
timeIntervalproperty 
timeInterval:uint  [read-write]

Sets the time in milliseconds between updates when timingMode is set to GTween.TIME ("time"). Setting this to a lower number will generally result in smoother animations but higher CPU usage. Defaults to 40ms (~25 updates per second).

Implementation
    public static function get timeInterval():uint
    public function set timeInterval(value:uint):void
timingModeproperty 
timingMode:String  [read-write]

Indicates how GTween should deal with timing. This can be set to either GTween.TIME, GTween.FRAME, or GTween.HYBRID.

In frame mode, GTween will update once every frame, and all positional values are specified in frames (duration, position, delay, and progress point positions).

In time mode, updates will occur at an interval specified by the timeInterval property, independent of the frame rate, and all positional values are specified in seconds.

In hybrid mode, all updates occur on a frame, but all positional values are specified in seconds. Each frame the tween will calculate it's position based on the elapsed time. This offers lower CPU usage, and a more familiar time based interface, but can result in choppy animations in high CPU situations.

The time mode will generally run more smoothly, as it is less dependent on the running frame rate of the movie. The frame mode will generally use less CPU, and can be synched with timeline animations. You can change modes at any time, but existing tweens will continue to use the mode that was active when they were created.

Implementation
    public static function get timingMode():String
    public function set timingMode(value:String):void
transitionFunctionproperty 
public var transitionFunction:Function

The transition function to use for calculating the tween.

useSmartRotationproperty 
public var useSmartRotation:Boolean = true

When true, the tween will always rotate in the shortest direction to reach the destination rotation. For example, rotating from 355 degress to 5 degrees will rotate 10 degrees clockwise with useSmartRotation set to true. It would rotate 350 degrees counter-clockwise with useSmartRotation set to false.

Constructor detail
GTween()constructor
public function GTween(target:Object = null, duration:Number = 10, properties:Object = null, tweenProperties:Object = null)

Constructs a new GTween instance.

Parameters
target:Object (default = null) — The object whose properties will be tweened. Defaults to null.
 
duration:Number (default = 10) — The length of the tween in frames or seconds depending on the timingMode. Defaults to 10.
 
properties:Object (default = null) — An object containing destination property values. For example, to tween to x=100, y=100, you could pass {x:100, y:100} as the props object.
 
tweenProperties:Object (default = null) — Indicates whether the tween should automatically play when a destination value is changed. Defaults to true.
Method detail
addChild()method
public function addChild(child:GTween, synchDuration:Boolean = true):void

Adds a child tween. Child tweens are played in synch with their parent (ie. their position properties are synchronized). By specifying true for synchDuration, you can also ensure that a child will keep its duration synchronized with its parent.

Parameters
child:GTween — A GTween instance to add as a child tween.
 
synchDuration:Boolean (default = true) — indicates whether the child tween's duration property should be kept in synch with the parent. Defaults to true. You may choose to set this to false if you have a shorter tween as a child, and want it to end sooner than its parent.
addProgressPoint()method 
public function addProgressPoint(position:Number, data:*):void

Adds a tween position at which to generate a progress event. You can use this to trigger other functionality at specific points in the tween. The name is the string value that lastProgressPoint will be set to when the event is dispatched.

Example: This will generate a progress event and set the lastProgressPoint to "middle" half way through a 5 second tween: myGTween.addProgressPoint(2.5, "middle");

Parameters
position:Number — The position at which to dispatch a progress event.
 
data:* — The name that will be set to lastProgressPoint when this progress point is reached.
beginning()method 
public function beginning():void

Jumps the tween to its beginning and pauses it. This sets all target properties to their init values, and sets the state to BEGINNING.

deleteProperty()method 
public function deleteProperty(propertyName:String):Boolean

Removes a destination value from the tween. This prevents the GTween instance from tweening the property.

Parameters
propertyName:String — The name of the destination property to delete.

Returns
Boolean
end()method 
public function end(endNextTween:Boolean = true):void

Jumps the tween to its end. This is the same as setting position=duration, except it also provides you with the option to also end the nextTween (recursively). This sets all target properties to their destination values, sets the state to END, and fires the COMPLETE event.

Parameters
endNextTween:Boolean (default = true)
getProperty()method 
public function getProperty(propertyName:String):Number

Returns the destination value for the specified property if one exists.

Parameters
propertyName:String — The name of the property to return a destination value for.

Returns
Number
invalidate()method 
public function invalidate():void

Invalidate forces the tween to repopulate all of the initial properties from the target object, and start playing if autoplay is set to true. If the tween is currently playing, then it will also set the position to 0. For example, if you changed the x and y position of an object while it was playing, you could call invalidate on it to force it to resume the tween with the new property values.

pause()method 
public function pause():void

Pauses the tween by stopping tick from being automatically called. This also releases the tween for garbage collection if it is not referenced externally.

play()method 
public function play():void

Plays a tween by incrementing the position property each frame. This also prevents the tween from being garbage collected while it is active. This is achieved by way of two methods: 1. If the target object is an IEventDispatcher, then the tween will subscribe to a dummy event using a hard reference. This allows the tween to be garbage collected if its target is also collected, and there are no other external references to it. 2. If the target object is not an IEventDispatcher, then the tween is placed in the activeTweens list, to prevent collection until it is paused or reaches the end of the transition). Note that pausing all tweens via the GTween.paused static property will not free the tweens for collection.

removeChild()method 
public function removeChild(child:GTween):void

Removes a child tween.

Parameters
child:GTween — The child GTween instance to remove.
removeProgressPoint()method 
public function removeProgressPoint(data:*):void

Removes the progress point with the specified name.

Parameters
data:* — The data associated with the progress point to remove.
reset()method 
public function reset():void

Resets the tween, pausing it, deleting all destination values, resetting the tween position to the beginning, and nulling the lastProgressPoint. This will not change the target's current properties.

reverse()method 
public function reverse(reverseChildren:Boolean = true):void

Reverses the tween by swapping its initial and destination properties. If the tween is playing (state = TWEEN_PHASE), it will also reverse the tween position.

Note: calling reverse on a playing tween may result in unusual results with nonlinear tween functions.

Parameters
reverseChildren:Boolean (default = true) — Indicates whether to call reverse on all child tweens.
setAssignment()method 
public function setAssignment(assignmentTarget:Object = null, assignmentProperty:String = null):void

Allows you to tween objects that require re-assignment whenever they are modified by reassigning the target object to a specified property of another object. For example, in order for changes to a colorTransform object to be visible, it must be assigned back to the .transform.colorTransform property of a display object. To make this work, you would call myTween.setAssignment(myDisplayObject.transform,"colorTransform"); This will also cause GTween to retrieve the target each time it copies its initial values.

Note: this does not work with filters yet, as they must be assigned to an array first, and then to the filters property. Use GTweenFilter instead.

Parameters
assignmentTarget:Object (default = null) — The object to reassign the property on.
 
assignmentProperty:String (default = null) — The name of the property to reassign the target to.
setProperties()method 
public function setProperties(properties:Object):void

Shorthand method for making multiple setProperty calls quickly.

Example: set x and y destination values:
myGTween.setProperties({x:200, y:400});

Parameters
properties:Object — An object containing destination property values.
setProperty()method 
public function setProperty(propertyName:String, value:Number):void

Sets the numeric destination value for a property on the target object that you would like to tween. For example, if you wanted to tween to a new x position, you could use: myGTween.setProperty("x",400). Nonnumeric values are ignored.

Parameters
propertyName:String — The name of the property to tween.
 
value:Number — The numeric destination value (the value to tween to).
setTweenProperties()method 
public function setTweenProperties(tweenProperties:Object):void

Shortcut method for setting multiple properties on the tween object quickly. This does not set destination values (ie. the value to tween to).

Example: This will set the duration, autoReverse, and nextTween properties of a tween:
myTween.setTweenProperties({duration:4, autoReverse:true, nextTween:anotherTween});

Parameters
tweenProperties:Object
Constant detail
BEGINNINGconstant
public static const BEGINNING:String = "beginning"

Constant for the BEGINNING state.

DELAY_PHASEconstant 
public static const DELAY_PHASE:String = "delayPhase"

Constant for the DELAY_PHASE state.

ENDconstant 
public static const END:String = "end"

Constant for the END state.

FRAMEconstant 
public static const FRAME:String = "frame"

Constant for the FRAME timingMode.

HYBRIDconstant 
public static const HYBRID:String = "hybrid"

Constant for the HYBRID timingMode.

PROGRESSconstant 
public static const PROGRESS:String = "progress"

Constant for the PROGRESS event type.

TIMEconstant 
public static const TIME:String = "time"

Dispatched when a tween activates and first starts running. That is, when the tween exits the beginning or end state, and enters either the delayPhase or tweenPhase state.

TWEEN_PHASEconstant 
public static const TWEEN_PHASE:String = "tweenPhase"

Constant for the TWEEN_PHASE state.