Timeline Class
The Timeline class synchronizes multiple tweens and allows them to be controlled as a group. Please note that if a timeline is looping, the tweens on it may appear to loop even if the "loop" property of the tween is false.
Constructor
Timeline
-
tweens
-
labels
-
props
Parameters:
-
tweens
ArrayAn array of Tweens to add to this timeline. See addTween for more info.
-
labels
ObjectAn object defining labels for using gotoAndPlay/gotoAndStop. See setLabels for details.
-
props
ObjectThe configuration properties to apply to this tween instance (ex.
{loop:true}
). All properties default to false. Supported props are:- loop: sets the loop property on this tween.
- useTicks: uses ticks for all durations instead of milliseconds.
- ignoreGlobalPause: sets the ignoreGlobalPause property on this tween.
- paused: indicates whether to start the tween paused.
- position: indicates the initial position for this timeline.
- onChange: specifies a listener to add for the change event.
Item Index
Methods
- _dispatchEvent
- _goto
- addEventListener
- addLabel
- addTween
- clone
- dispatchEvent
- getCurrentLabel
- getLabels
- gotoAndPlay
- gotoAndStop
- hasEventListener
- initialize deprecated
- off
- on
- removeAllEventListeners
- removeEventListener
- removeTween
- resolve
- setLabels
- setPaused
- setPosition
- tick
- toString
- updateDuration
- willTrigger
Properties
Events
Methods
_dispatchEvent
-
eventObj
-
eventPhase
_goto
()
protected
addEventListener
-
type
-
listener
-
[useCapture]
Adds the specified event listener. Note that adding multiple listeners to the same function will result in multiple callbacks getting fired.
Example
displayObject.addEventListener("click", handleClick);
function handleClick(event) {
// Click happened.
}
Parameters:
-
type
StringThe string type of the event.
-
listener
Function | ObjectAn object with a handleEvent method, or a function that will be called when the event is dispatched.
-
[useCapture]
Boolean optionalFor events that bubble, indicates whether to listen for the event in the capture or bubbling/target phase.
addTween
-
tween
Adds one or more tweens (or timelines) to this timeline. The tweens will be paused (to remove them from the normal ticking system) and managed by this timeline. Adding a tween to multiple timelines will result in unexpected behaviour.
Parameters:
-
tween
ObjectThe tween(s) to add. Accepts multiple arguments.
Returns:
Tween The first tween that was passed in.
clone
()
protected
dispatchEvent
-
eventObj
Dispatches the specified event to all listeners.
Example
// Use a string event
this.dispatchEvent("complete");
// Use an Event instance
var event = new createjs.Event("progress");
this.dispatchEvent(event);
Parameters:
Returns:
Returns the value of eventObj.defaultPrevented.
getCurrentLabel
()
String
Returns the name of the label on or immediately before the current position. For example, given a timeline with two labels, "first" on frame index 4, and "second" on frame 8, getCurrentLabel would return:
- null if the current position is 2.
- "first" if the current position is 4.
- "first" if the current position is 7.
- "second" if the current position is 15.
Returns:
The name of the current label or null if there is no label
getLabels
()
ArrayObject
Returns a sorted list of the labels defined on this timeline.
Returns:
A sorted array of objects with label and position properties.
gotoAndPlay
-
positionOrLabel
Unpauses this timeline and jumps to the specified position or label.
gotoAndStop
-
positionOrLabel
Pauses this timeline and jumps to the specified position or label.
hasEventListener
-
type
Indicates whether there is at least one listener for the specified event type.
Parameters:
-
type
StringThe string type of the event.
Returns:
Returns true if there is at least one listener for the specified event.
initialize
()
deprecated
protected
off
-
type
-
listener
-
[useCapture]
A shortcut to the removeEventListener method, with the same parameters and return value. This is a companion to the .on method.
on
-
type
-
listener
-
[scope]
-
[once=false]
-
[data]
-
[useCapture=false]
A shortcut method for using addEventListener that makes it easier to specify an execution scope, have a listener only run once, associate arbitrary data with the listener, and remove the listener.
This method works by creating an anonymous wrapper function and subscribing it with addEventListener. The created anonymous function is returned for use with .removeEventListener (or .off).
Example
var listener = myBtn.on("click", handleClick, null, false, {count:3});
function handleClick(evt, data) {
data.count -= 1;
console.log(this == myBtn); // true - scope defaults to the dispatcher
if (data.count == 0) {
alert("clicked 3 times!");
myBtn.off("click", listener);
// alternately: evt.remove();
}
}
Parameters:
-
type
StringThe string type of the event.
-
listener
Function | ObjectAn object with a handleEvent method, or a function that will be called when the event is dispatched.
-
[scope]
Object optionalThe scope to execute the listener in. Defaults to the dispatcher/currentTarget for function listeners, and to the listener itself for object listeners (ie. using handleEvent).
-
[once=false]
Boolean optionalIf true, the listener will remove itself after the first time it is triggered.
-
[data]
optionalArbitrary data that will be included as the second parameter when the listener is called.
-
[useCapture=false]
Boolean optionalFor events that bubble, indicates whether to listen for the event in the capture or bubbling/target phase.
Returns:
Returns the anonymous function that was created and assigned as the listener. This is needed to remove the listener later using .removeEventListener.
removeAllEventListeners
-
[type]
Removes all listeners for the specified type, or all listeners of all types.
Example
// Remove all listeners
displayObject.removeAllEventListeners();
// Remove all click listeners
displayObject.removeAllEventListeners("click");
Parameters:
-
[type]
String optionalThe string type of the event. If omitted, all listeners for all types will be removed.
removeEventListener
-
type
-
listener
-
[useCapture]
Removes the specified event listener.
Important Note: that you must pass the exact function reference used when the event was added. If a proxy function, or function closure is used as the callback, the proxy/closure reference must be used - a new proxy or closure will not work.
Example
displayObject.removeEventListener("click", handleClick);
removeTween
-
tween
Removes one or more tweens from this timeline.
Parameters:
-
tween
ObjectThe tween(s) to remove. Accepts multiple arguments.
Returns:
Boolean Returns true if all of the tweens were successfully removed.
resolve
-
positionOrLabel
If a numeric position is passed, it is returned unchanged. If a string is passed, the position of the corresponding frame label will be returned, or null if a matching label is not defined.
setLabels
-
o
Defines labels for use with gotoAndPlay/Stop. Overwrites any previously set labels.
Parameters:
-
o
ObjectAn object defining labels for using gotoAndPlay/Stop in the form
{labelName:time}
where time is in milliseconds (or ticks ifuseTicks
is true).
setPaused
-
value
Pauses or plays this timeline.
Parameters:
-
value
BooleanIndicates whether the tween should be paused (true) or played (false).
setPosition
-
value
-
[actionsMode]
Advances the timeline to the specified position.
Parameters:
-
value
NumberThe position to seek to in milliseconds (or ticks if
useTicks
is true). -
[actionsMode]
Number optionalparameter specifying how actions are handled. See the Tween setPosition method for more details.
Returns:
Returns true if the timeline is complete (ie. the full timeline has run & loop is false).
tick
-
delta
Advances this timeline by the specified amount of time in milliseconds (or ticks if useTicks is true). This is normally called automatically by the Tween engine (via Tween.tick), but is exposed for advanced uses.
Parameters:
-
delta
NumberThe time to advance in milliseconds (or ticks if useTicks is true).
toString
()
String
Returns a string representation of this object.
Returns:
a string representation of the instance.
updateDuration
()
Recalculates the duration of the timeline. The duration is automatically updated when tweens are added or removed, but this method is useful if you modify a tween after it was added to the timeline.
willTrigger
-
type
Indicates whether there is at least one listener for the specified event type on this object or any of its ancestors (parent, parent's parent, etc). A return value of true indicates that if a bubbling event of the specified type is dispatched from this object, it will trigger at least one listener.
This is similar to hasEventListener, but it searches the entire event flow for a listener, not just this object.
Parameters:
-
type
StringThe string type of the event.
Returns:
Returns true
if there is at least one listener for the specified event.
Properties
_labelList
ArrayObject
protected
_tweens
ArrayTween
protected
duration
Number
Read-only property specifying the total duration of this timeline in milliseconds (or ticks if useTicks is true). This value is usually automatically updated as you modify the timeline. See updateDuration for more information.
loop
Boolean
If true, the timeline will loop when it reaches the end. Can be set via the props param.
position
Object
Read-only. The current normalized position of the timeline. This will always be a value between 0 and duration. Changing this property directly will have no effect.
Events
change
Called whenever the timeline's position changes.