File: src/link/MovieClipManager.js
/**
* @author Vsevolod Strukchinsky @floatdrop
*/
/**
A class MovieClipManager contains and manages MovieClips.
@example
var mcn = new PIXI.MovieClipManager();
mcn.add("walk", new PIXI.MovieClip([texture1, texture2, ...], 0.23));
mcn.set("walk").animationSpeed(0.22).loop(false).play();
@class MovieClipManager
@extends DisplayObjectContainer
@constructor
**/
LINK.MovieClipManager = function () {
PIXI.DisplayObjectContainer.call(this);
/**
* [read-only] The map of name to MovieClip objects.
* @property _animations {Object}
*/
this._animations = {};
/**
* [read-only] The current displaying animation.
* @property children {MovieClip}
*/
this._current = undefined;
for (var argumentIndex in arguments) {
var arg = arguments[argumentIndex];
this.add(arg.name, arg.clip);
}
};
LINK.MovieClipManager.constructor = LINK.MovieClipManager;
LINK.MovieClipManager.prototype = Object.create(PIXI.DisplayObjectContainer.prototype);
/**
* Gets movieClip with `name`
* @method get
* @param name {String}
* @return MovieClip
*/
LINK.MovieClipManager.prototype.get = function (name) {
return this._animations[name];
};
/**
* Changes movieClip to another
* @method set
* @param name {String}
* @return DisplayObject
*/
LINK.MovieClipManager.prototype.set = function (name) {
if (this._current) {
this._current.stop();
this._current.visible = false;
}
this._current = this._animations[name];
if (this._current) {
this._current.visible = true;
}
return this;
};
/**
* Plays movieClip
* @method play
* @param name {String} (set as undefined to play current MovieClip)
* @return DisplayObject
*/
LINK.MovieClipManager.prototype.play = function (name) {
if (name) {
this.set(name);
}
if (this._current) {
this._current.play();
}
return this;
};
/**
* Stops the current MovieClip and goes to a specific frame
* @method gotoAndStop
* @param frameNumber {Number} frame index to stop at
*/
LINK.MovieClipManager.prototype.gotoAndStop = function (frameNumber) {
this._current.gotoAndStop(frameNumber);
return this;
};
/**
* Goes to a specific frame and begins playing the current MovieClip
* @method gotoAndPlay
* @param frameNumber {Number} frame index to start at
*/
LINK.MovieClipManager.prototype.gotoAndPlay = function(frameNumber)
{
this._current.gotoAndPlay(frameNumber);
return this;
};
/**
* Sets animationSpeed property on current MovieClip
* @method animationSpeed
* @param speed {Number}
*/
LINK.MovieClipManager.prototype.animationSpeed = function(speed)
{
this._current.animationSpeed = speed;
return this;
};
/**
* Sets loop property on current MovieClip
* @method loop
* @param isLooped {Boolean}
*/
LINK.MovieClipManager.prototype.loop = function(isLooped)
{
this._current.loop = isLooped;
return this;
};
/**
* Sets onComplete callback on current MovieClip
* @method onComplete
* @param frameNumber {Number} frame index to start at
*/
LINK.MovieClipManager.prototype.onComplete = function(callback)
{
this._current.onComplete = callback;
return this;
};
/**
* Stops current MovieClip
* @method stop
*/
LINK.MovieClipManager.prototype.stop = function () {
if (this._current) {
this._current.stop();
}
return this;
};
/**
* Creates new animation with name `name`.
* @method add
* @param name {String}
* @param movieClip {MovieClip}
* @return DisplayObject
*/
LINK.MovieClipManager.prototype.add = function (name, movieClip, speed) {
this._animations[name] = movieClip;
movieClip.animationSpeed = speed || movieClip.animationSpeed;
this.addChild(movieClip);
movieClip.visible = false;
return this;
};
/**
* Removes a animation from the container.
* @method remove
* @param name {String}
*/
LINK.MovieClipManager.prototype.remove = function (name) {
if (this._animations[name] === this._current) {
this._current.visible = false;
this._current = undefined;
}
delete this._animations[name];
};