Juggler

The Juggler API allows you to animate any objects implementing the IAnimatable interface. MovieClip objects implement the latter, but you can also define you own type of animated object for Starling, all you need to do is implement the IAnimatable interface and override the advanceTime method. This is how the particles extension works, we will come back to this at the end of this tutorial.

Below we can see how animation is done on a MovieClip, the main logic is located here. On each frame, the texture is swapped. With the native APIs, a similar mechanism would be changing the bitmapData used by a Bitmap object on each frame:

// IAnimatable
public function advanceTime(passedTime:Number):void
{
    var finalFrame:int;
    var previousFrame:int = mCurrentFrame;
    
    if (mLoop && mCurrentTime == mTotalTime) { mCurrentTime = 0.0; 
                                              mCurrentFrame = 0; }
    if (!mPlaying || passedTime == 0.0 || mCurrentTime == mTotalTime) return;
    
    mCurrentTime += passedTime;
    finalFrame = mTextures.length - 1;
    
    while (mCurrentTime >= mStartTimes[mCurrentFrame] + mDurations[mCurrentFrame])
    {
        if (mCurrentFrame == finalFrame)
        {
            if (hasEventListener(Event.COMPLETE))
            {
                var restTime:Number = mCurrentTime - mTotalTime;
                mCurrentTime = mTotalTime;
                dispatchEventWith(Event.COMPLETE);
                
                // user might have changed movie clip settings, so we restart the
                                                                           method
                advanceTime(restTime);
                return;
            }
            
            if (mLoop)
            {
                mCurrentTime -= mTotalTime;
                mCurrentFrame = 0;
            }
            else
            {
                mCurrentTime = mTotalTime;
                break;
            }
        }
        else
        {
            mCurrentFrame++;
            
            var sound

Get Introducing Starling now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.