#

API Docs for: 1.6.0
Show: Inherited Protected Private Deprecated

# File: src/pixi/display/MovieClip.js

    /**
     * @author Mat Groves http://matgroves.com/ @Doormat23
     */

    /**
     * A MovieClip is a simple way to display an animation depicted by a list of textures.
     *
     * @class MovieClip
     * @extends Sprite
     * @constructor
     * @param textures {Array<Texture>} an array of {Texture} objects that make up the animation
     */
    PIXI.MovieClip = function(textures)
    {
        PIXI.Sprite.call(this, textures[0]);

        /**
         * The array of textures that make up the animation
         *
         * @property textures
         * @type Array
         */
        this.textures = textures;

        /**
         * The speed that the MovieClip will play at. Higher is faster, lower is slower
         *
         * @property animationSpeed
         * @type Number
         * @default 1
         */
        this.animationSpeed = 1;

        /**
         * Whether or not the movie clip repeats after playing.
         *
         * @property loop
         * @type Boolean
         * @default true
         */
        this.loop = true;

        /**
         * Function to call when a MovieClip finishes playing
         *
         * @property onComplete
         * @type Function
         */
        this.onComplete = null;

        /**
         * [read-only] The MovieClips current frame index (this may not have to be a whole number)
         *
         * @property currentFrame
         * @type Number
         * @default 0
         * @readOnly
         */
        this.currentFrame = 0;

        /**
         * [read-only] Indicates if the MovieClip is currently playing
         *
         * @property playing
         * @type Boolean
         * @readOnly
         */
        this.playing = false;
    };

    // constructor
    PIXI.MovieClip.prototype = Object.create( PIXI.Sprite.prototype );
    PIXI.MovieClip.prototype.constructor = PIXI.MovieClip;

    /**
    * [read-only] totalFrames is the total number of frames in the MovieClip. This is the same as number of textures
    * assigned to the MovieClip.
    *
    * @property totalFrames
    * @type Number
    * @default 0
    * @readOnly
    */
    Object.defineProperty( PIXI.MovieClip.prototype, 'totalFrames', {
    	get: function() {

    		return this.textures.length;
    	}
    });

    /**
     * Stops the MovieClip
     *
     * @method stop
     */
    PIXI.MovieClip.prototype.stop = function()
    {
        this.playing = false;
    };

    /**
     * Plays the MovieClip
     *
     * @method play
     */
    PIXI.MovieClip.prototype.play = function()
    {
        this.playing = true;
    };

    /**
     * Stops the MovieClip and goes to a specific frame
     *
     * @method gotoAndStop
     * @param frameNumber {Number} frame index to stop at
     */
    PIXI.MovieClip.prototype.gotoAndStop = function(frameNumber)
    {
        this.playing = false;
        this.currentFrame = frameNumber;
        var round = (this.currentFrame + 0.5) | 0;
        this.setTexture(this.textures[round % this.textures.length]);
    };

    /**
     * Goes to a specific frame and begins playing the MovieClip
     *
     * @method gotoAndPlay
     * @param frameNumber {Number} frame index to start at
     */
    PIXI.MovieClip.prototype.gotoAndPlay = function(frameNumber)
    {
        this.currentFrame = frameNumber;
        this.playing = true;
    };

    /*
     * Updates the object transform for rendering
     *
     * @method updateTransform
     * @private
     */
    PIXI.MovieClip.prototype.updateTransform = function()
    {
        PIXI.Sprite.prototype.updateTransform.call(this);

        if(!this.playing)return;

        this.currentFrame += this.animationSpeed;

        var round = (this.currentFrame + 0.5) | 0;

        this.currentFrame = this.currentFrame % this.textures.length;

        if(this.loop || round < this.textures.length)
        {
            this.setTexture(this.textures[round % this.textures.length]);
        }
        else if(round >= this.textures.length)
        {
            this.gotoAndStop(this.textures.length - 1);
            if(this.onComplete)
            {
                this.onComplete();
            }
        }
    };

    /**
     * A short hand way of creating a movieclip from an array of frame ids
     *
     * @static
     * @method fromFrames
     * @param frames {Array} the array of frames ids the movieclip will use as its texture frames
     */
    PIXI.MovieClip.fromFrames = function(frames)
    {
        var textures = [];

        for (var i = 0; i < frames.length; i++)
        {
            textures.push(new PIXI.Texture.fromFrame(frames[i]));
        }

        return new PIXI.MovieClip(textures);
    };

    /**
     * A short hand way of creating a movieclip from an array of image ids
     *
     * @static
     * @method fromFrames
     * @param frames {Array} the array of image ids the movieclip will use as its texture frames
     */
    PIXI.MovieClip.fromImages = function(images)
    {
        var textures = [];

        for (var i = 0; i < images.length; i++)
        {
            textures.push(new PIXI.Texture.fromImage(images[i]));
        }

        return new PIXI.MovieClip(textures);
    };