O'Reilly logo

Introducing Starling by Thibault Imbert

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required