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

GTween ©2008 Grant Skinner, gskinner.com. Visit www.gskinner.com/libraries/gtween/ 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 global 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 end 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 end 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 end 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. See the documentation for the "proxy" property for more information.

For a light-weight engine (<4.5kb), GTween boasts a number of advanced features: Also see GTweenFilter and GTweenTimeline for more advanced functionality, or GTweensy for a more lightweight engine.
Beta 5 updates (Feb 1, 2009):
Beta 5 constitutes a major rewrite of GTween. Much of the API has been changed, and it is recommended that you read the API documents to familiarize yourself with the new capabilities. The following list might not be exhaustive.



Public Properties
 PropertyDefined by
  activeTicker : ITicker
[static][read-only] The currently active Ticker object.
GTween
  autoPlay : Boolean = true
Indicates whether the tween should automatically play when an end value is changed.
GTween
  autoRotation : Boolean = false
When true, the tween will always rotate in the shortest direction to reach an end rotation value.
GTween
  autoVisible : Boolean = true
Indicates whether the target's visible property should automatically be set to false when its alpha value is tweened to 0 or less.
GTween
  data : *
Allows you to associate arbitrary data with your tween.
GTween
  defaultEase : Function
[static] Specifies the default easing function to use with new tweens.
GTween
  delay : Number
The length of the delay in frames or seconds (depending on the timingMode).
GTween
  duration : Number = 1
The length of the tween in frames or seconds (depending on the timingMode).
GTween
  ease : Function
The easing function to use for calculating the tween.
GTween
  lockStartProperties : Boolean
If set to true, this prevents the tween from reinitializing its start properties automatically (ex.
GTween
  nextTween : GTween
Specifies another GTween instance that will have paused=false called on it when this tween completes.
GTween
  pauseAll : Boolean = false
[static] Setting this to true pauses all tween instances.
GTween
  paused : Boolean
Indicates whether the tween is currently paused.
GTween
  position : Number
Gets and sets the position in the tween in frames or seconds (depending on the timingMode).
GTween
  propertyTarget : Object
[read-only] Returns the object that will have its property tweened.
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
  reflect : Boolean = false
Indicates whether the tween should use the reflect mode when repeating.
GTween
  repeat : int = 0
The number of times this tween will repeat.
GTween
  reversed : Boolean
Indicates whether a tween should run in reverse.
GTween
  rotationProperties : Object
[static] A hash table specifying properties that should be affected by autoRotation.
GTween
  snapping : Boolean = false
If set to true, tweened values specified by snappingProperties will be rounded (snapped) before they are assigned to the target.
GTween
  snappingProperties : Object
[static] A hash table specifying properties that should have their value rounded (snapped) before being applied.
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
  tweenPosition : Number
[read-only] Returns the calculated absolute position in the tween.
GTween
Public Methods
 MethodDefined by
  
GTween(target:Object = null, duration:Number = 10, properties:Object = null, tweenProperties:Object = null)
Constructs a new GTween instance.
GTween
  
beginning():void
Jumps the tween to its beginning.
GTween
  
deleteProperty(name:String):Boolean
Removes a end value from the tween.
GTween
  
end():void
Jumps the tween to its end.
GTween
  
getProperties():Object
Returns the hash table of all end properties and their values.
GTween
  
getProperty(name:String):Number
Returns the end value for the specified property if one exists.
GTween
  
Returns the hash table of all start properties and their values.
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
  
linearEase(t:Number, b:Number, c:Number, d:Number):Number
[static] The default easing function used by GTween.
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
  
reverse(suppressEvents:Boolean = true):void
Toggles the reversed property and inverts the current tween position.
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
  
setPosition(position:Number, suppressEvents:Boolean = true):void
Sets the position of the tween.
GTween
  
setProperties(properties:Object):void
Shorthand method for making multiple setProperty calls quickly.
GTween
  
setProperty(name:String, value:Number):void
Sets the numeric end value for a property on the target object that you would like to tween.
GTween
  
setStartProperties(properties:Object):void
Allows you to manually assign the start property values for a tween.
GTween
  
setTweenProperties(properties:Object):void
Shortcut method for setting multiple properties on the tween instance quickly.
GTween
Events
 EventSummaryDefined by
   This event is dispatched each time the tween updates properties on its target.GTween
   Dispatched when a tween ends (its position equals its duration).GTween
   Dispatched when a tween copies its initial properties and starts tweening.GTween
Public Constants
 ConstantDefined by
  DELAY : String = "delay"
[static] Constant for the DELAY 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
  START : String = "start"
[static] Constant for the START state.
GTween
  TIME : String = "time"
[static] Constant for the TIME timingMode.
GTween
  TWEEN : String = "tween"
[static] Constant for the TWEEN state.
GTween
Property detail
activeTickerproperty
activeTicker:ITicker  [read-only]

The currently active Ticker object.

Implementation
    public static function get activeTicker():ITicker
autoPlayproperty 
public var autoPlay:Boolean = true

Indicates whether the tween should automatically play when an end value is changed.

autoRotationproperty 
public var autoRotation:Boolean = false

When true, the tween will always rotate in the shortest direction to reach an end rotation value. For example, rotating from 355 degress to 5 degrees will rotate 10 degrees clockwise with .autoRotation set to true. It would rotate 350 degrees counter-clockwise with .autoRotation set to false. This affects all properties specified in the static .rotationProperties hash table.

autoVisibleproperty 
public var autoVisible:Boolean = true

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.

dataproperty 
public var data:*

Allows you to associate arbitrary data with your tween. For example, you might use this to reference specific data when handling events from tweens.

defaultEaseproperty 
public static var defaultEase:Function

Specifies the default easing function to use with new tweens. If null, GTween.linearEase will be used.

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 
public var duration:Number = 1

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.

easeproperty 
public var ease:Function

The easing function to use for calculating the tween. This can be any standard tween function, such as the tween functions in fl.motion.easing.that come with Flash CS3. New tweens will have this set to the defaultTween. Setting this to null will cause GTween to throw null reference errors.

lockStartPropertiesproperty 
lockStartProperties:Boolean  [read-write]

If set to true, this prevents the tween from reinitializing its start properties automatically (ex. when end properties change). If start properties have not already been initialized, this will also cause the tween to immediate initialize them. Note that this will prevent new start property values from being initialized when invalidating, so it could cause unexpected behaviour if you modify the tween while it is playing.

Implementation
    public function get lockStartProperties():Boolean
    public function set lockStartProperties(value:Boolean):void
nextTweenproperty 
public var nextTween:GTween

Specifies another GTween instance that will have paused=false called on it when this tween completes.

pauseAllproperty 
public static var pauseAll:Boolean = false

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

pausedproperty 
paused:Boolean  [read-write]

Indicates whether the tween is currently paused. See play() and pause() for more information.

Implementation
    public 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). This value can be any number, and will be resolved to a tweenPosition value prior to being applied to the tweened values. See tweenPosition for more information.

Negative values
Values below 0 will always resolve to a tweenPosition of 0. Negative values can be used to set up a delay on the tween, as the tween will have to count up to 0 before initing.

Positive values
Positive values are resolved based on the duration, repeat, reflect, and reversed properties.

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

Returns the object that will have its property tweened. In a standard GTween, this will usually be the same as target, except if an assignmentTarget was set. This also makes it easy for subclasses like GTweenFilter can divert the property target.

Implementation
    public function get propertyTarget():Object
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 end values. The proxy will return GTween end 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 an end property value (through setProperty) for scaleX, then retrieves it from GTween (because it will always return end values when available):
trace(myGTween.proxy.scaleX); // 1 (value from target, because no end value is set)
myGTween.proxy.scaleX = 2; // set a end value
trace(myGTween.proxy.scaleX); // 2 (end value from GTween)
trace(myGTween.target.scaleX); // 1 (current value from target)


Example 4: Property deletions only affect end properties on GTween, not the target object:
myGTween.proxy.rotation = 50; // set a end value
trace(myGTween.proxy.rotation); // 50 (end value from GTween)
delete(myGTween.proxy.rotation); // delete the end 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
reflectproperty 
public var reflect:Boolean = false

Indicates whether the tween should use the reflect mode when repeating. If reflect is set to true, then the tween will play backwards on every other repeat. This has similar effects to reversed, but the properties are exclusive of one another. For instance, with reversed set to false, reflected set to true, and a repeat of 1, the tween will play from start to end, then repeat and reflect to play from end to start. If in the previous example reversed was instead set to true, the tween would play from end to start, then repeat and reflect to play from start to end. Finally, with reversed set to false, reflected set to false, and a repeat of 1, the tween will play from start to end, then repeat from start to end

repeatproperty 
public var repeat:int = 0

The number of times this tween will repeat. If 0, the tween will only run once. If 1 or more, the tween will repeat that many times. If -1, the tween will repeat forever.

reversedproperty 
reversed:Boolean  [read-write]

Indicates whether a tween should run in reverse. In the simplest examples this means that the tween will play from its end values to its start values. See "reflect" for more information on how these two related properties interact. Also see reverse().

Implementation
    public function get reversed():Boolean
    public function set reversed(value:Boolean):void
rotationPropertiesproperty 
public static var rotationProperties:Object

A hash table specifying properties that should be affected by autoRotation.

snappingproperty 
public var snapping:Boolean = false

If set to true, tweened values specified by snappingProperties will be rounded (snapped) before they are assigned to the target.

snappingPropertiesproperty 
public static var snappingProperties:Object

A hash table specifying properties that should have their value rounded (snapped) before being applied. This can be toggled for each tween instance basis with the .snapping property.

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.START, GTween.DELAY, GTween.TWEEN, 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 START. When first played, a tween will have a state of either DELAY (if delay > 0) or TWEEN (if delay == 0). When the delay ends, and tweening begins, the state will change to TWEEN. When the tween reaches its end position == durationrepeat+1), the state will be set to END. If you change any end properties on an ended tween, its state will be set back to START.

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 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, etc).

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 hybrid mode generally provides the smoothest results. The frame mode makes it easy to synch tweens 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
tweenPositionproperty 
tweenPosition:Number  [read-only]

Returns the calculated absolute position in the tween. This is a deterministic value between 0 and duration calculated from the current position based on the duration, repeat, reflect, and reversed properties.

For example, a tween with a position of 5 on a tween with a duration of 3, and repeat set to true would have a tweenPosition of 2 (2 seconds into the first repeat). The same tween with reflect set to true would have a tweenPosition of 1 (because it would be 2 seconds into the first repeat which is playing backwards). With reflect and reversed set to true it would have a tweenPosition of 2.

Tweens with a position less than 0 will have a tweenPosition of 0. Tweens with a position greater than durationrepeat+1) (the total length of the tween) will have a tweenPosition equal to duration.

Implementation
    public function get tweenPosition():Number
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 end 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) — An object containing properties to set on this tween. For example, you could pass {ease:myEase} to set the ease property of the new instance. This also provides a shortcut for setting up event listeners. See .setTweenProperties() for more information.
Method detail
beginning()method
public function beginning():void

Jumps the tween to its beginning. This is the same as setting position=-delay.

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

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

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

Returns
Boolean
end()method 
public function end():void

Jumps the tween to its end. This is the same as setting position=(repeat+1)uration.

getProperties()method 
public function getProperties():Object

Returns the hash table of all end properties and their values. This is a copy of the internal hash of values, so modifying the returned object will not affect the tween.

Returns
Object
getProperty()method 
public function getProperty(name:String):Number

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

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

Returns
Number
getStartProperties()method 
public function getStartProperties():Object

Returns the hash table of all start properties and their values. This is a copy of the internal hash of values, so modifying the returned object will not affect the tween.

Returns
Object
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 a the target object while the tween was playing, you could call invalidate on it to force it to resume the tween with the new property values.

linearEase()method 
public static function linearEase(t:Number, b:Number, c:Number, d:Number):Number

The default easing function used by GTween.

Parameters
t:Number
 
b:Number
 
c:Number
 
d:Number

Returns
Number
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.pauseAll static property will not free the tweens for collection.

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

Toggles the reversed property and inverts the current tween position. This will cause a tween to reverse playing visually. There is currently an issue with this functionality for tweens with a repeat of -1

Parameters
suppressEvents:Boolean (default = true) — Indicates whether to suppress any events or callbacks that are generated as a result of the position change.
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, 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.
setPosition()method 
public function setPosition(position:Number, suppressEvents:Boolean = true):void

Sets the position of the tween. Using the position property will always suppress events and callbacks, whereas the setPosition method allows you to manually set the position and specify whether to suppress events or not.

Parameters
position:Number — The position to jump to in seconds or frames (depending on the timingMode).
 
suppressEvents:Boolean (default = true) — Indicates whether to suppress events and callbacks generated from the change in position.
setProperties()method 
public function setProperties(properties:Object):void

Shorthand method for making multiple setProperty calls quickly. This removes any existing target property values on the tween.

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

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

Sets the numeric end 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). Non-numeric values are ignored.

Parameters
name:String — The name of the property to tween.
 
value:Number — The numeric end value (the value to tween to).
setStartProperties()method 
public function setStartProperties(properties:Object):void

Allows you to manually assign the start property values for a tween. These are the properties that will be applied when the tween is at tween position 0. Normally these are automatically copied from the target object on initialization, or whenever a end value changes. You can also use the lockStartProperties property to ensure your start properties are not reinitialized after you set them.

Parameters
properties:Object — An object containing start property values.
setTweenProperties()method 
public function setTweenProperties(properties:Object):void

Shortcut method for setting multiple properties on the tween instance quickly. This does not set end values (ie. the value to tween to). This method also provides you with a quick method for adding listeners to specific events, using the special properties: initListener, completeListener, changeListener.

Example: This will set the duration, reflect, and nextTween properties of a tween, and add a listener for the complete event:
myTween.setTweenProperties({duration:4, reflect:true, nextTween:anotherTween, completeListener:completeHandlerFunction});

Parameters
properties:Object
Event detail
changeevent 
Event object type: flash.events.Event

This event is dispatched each time the tween updates properties on its target. It will be dispatched each "tick" during the TWEEN.

completeevent  
Event object type: flash.events.Event

Dispatched when a tween ends (its position equals its duration).

initevent  
Event object type: flash.events.Event

Dispatched when a tween copies its initial properties and starts tweening. In tweens with a delay of 0, this event will fire immediately when it starts playing. In tweens with a delay set, this will fire when the delay state is ended, and the tween state is entered.

Constant detail
DELAYconstant
public static const DELAY:String = "delay"

Constant for the DELAY 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.

STARTconstant 
public static const START:String = "start"

Constant for the START state.

TIMEconstant 
public static const TIME:String = "time"

Constant for the TIME timingMode.

TWEENconstant 
public static const TWEEN:String = "tween"

Constant for the TWEEN state.