mirror of
https://github.com/photonstorm/phaser
synced 2024-12-25 04:23:30 +00:00
140 lines
5.2 KiB
JavaScript
140 lines
5.2 KiB
JavaScript
var Class = require('../../utils/Class');
|
|
var GetValue = require('../../utils/object/GetValue');
|
|
var GetFrames = require('./inc/GetFrames');
|
|
|
|
// A Frame based Animation
|
|
// This consists of a key, some default values (like the frame rate) and a bunch of Frame objects.
|
|
// The Animation Manager creates these
|
|
// Game Objects don't own an instance of these directly
|
|
// Game Objects have the Animation Component, which are like playheads to global Animations (these objects)
|
|
// So multiple Game Objects can have playheads all pointing to this one Animation instance
|
|
|
|
// Phaser.Animations.Animation
|
|
|
|
var Animation = new Class({
|
|
|
|
initialize:
|
|
|
|
function Animation (manager, key, config)
|
|
{
|
|
this.manager = manager;
|
|
|
|
this.key = key;
|
|
|
|
// A frame based animation (as opposed to a bone based animation)
|
|
this.type = 'frame';
|
|
|
|
// Extract all the frame data into the frames array
|
|
this.frames = GetFrames(manager.textureManager, GetValue(config, 'frames', []), GetValue(config, 'defaultTextureKey', null));
|
|
|
|
// The frame rate of playback in frames per second (default 24 if duration is null)
|
|
this.frameRate = GetValue(config, 'frameRate', null);
|
|
|
|
// How long the animation should play for. If frameRate is set it overrides this value
|
|
// otherwise frameRate is derived from duration
|
|
this.duration = GetValue(config, 'duration', null);
|
|
|
|
if (this.duration === null && this.frameRate === null)
|
|
{
|
|
// No duration or frameRate given, use default frameRate of 24fps
|
|
this.frameRate = 24;
|
|
this.duration = this.frameRate / this.frames.length;
|
|
}
|
|
else if (this.duration && this.frameRate === null)
|
|
{
|
|
// Duration given but no frameRate, so set the frameRate based on duration
|
|
// I.e. 12 frames in the animation, duration = 4 (4000 ms)
|
|
// So frameRate is 12 / 4 = 3 fps
|
|
this.frameRate = this.frames.length / this.duration;
|
|
}
|
|
else
|
|
{
|
|
// frameRate given, derive duration from it (even if duration also specified)
|
|
// I.e. 15 frames in the animation, frameRate = 30 fps
|
|
// So duration is 15 / 30 = 0.5 (half a second)
|
|
this.duration = this.frames.length / this.frameRate;
|
|
}
|
|
|
|
// ms per frame (without including frame specific modifiers)
|
|
this.msPerFrame = 1000 / this.frameRate;
|
|
|
|
// Skip frames if the time lags, or always advanced anyway?
|
|
this.skipMissedFrames = GetValue(config, 'skipMissedFrames', true);
|
|
|
|
// Delay before starting playback (in seconds)
|
|
this.delay = GetValue(config, 'delay', 0);
|
|
|
|
// Number of times to repeat the animation (-1 for infinity)
|
|
this.repeat = GetValue(config, 'repeat', 0);
|
|
|
|
// Delay before the repeat starts (in seconds)
|
|
this.repeatDelay = GetValue(config, 'repeatDelay', 0);
|
|
|
|
// Should the animation yoyo? (reverse back down to the start) before repeating?
|
|
this.yoyo = GetValue(config, 'yoyo', false);
|
|
|
|
// Should sprite.visible = true when the animation starts to play?
|
|
this.showOnStart = GetValue(config, 'showOnStart', false);
|
|
|
|
// Should sprite.visible = false when the animation finishes?
|
|
this.hideOnComplete = GetValue(config, 'hideOnComplete', false);
|
|
|
|
// Callbacks
|
|
this.callbackScope = GetValue(config, 'callbackScope', this);
|
|
|
|
this.onStart = GetValue(config, 'onStart', false);
|
|
this.onStartParams = GetValue(config, 'onStartParams', []);
|
|
|
|
this.onRepeat = GetValue(config, 'onRepeat', false);
|
|
this.onRepeatParams = GetValue(config, 'onRepeatParams', []);
|
|
|
|
// Called for EVERY frame of the animation.
|
|
// See AnimationFrame.onUpdate for a frame specific callback.
|
|
this.onUpdate = GetValue(config, 'onUpdate', false);
|
|
this.onUpdateParams = GetValue(config, 'onUpdateParams', []);
|
|
|
|
this.onComplete = GetValue(config, 'onComplete', false);
|
|
this.onCompleteParams = GetValue(config, 'onCompleteParams', []);
|
|
|
|
// Global pause, effects all Game Objects using this Animation instance
|
|
this.paused = false;
|
|
|
|
this.manager.on('pauseall', this.pause.bind(this));
|
|
this.manager.on('resumeall', this.resume.bind(this));
|
|
},
|
|
|
|
addFrame: require('./inc/AddFrame'),
|
|
addFrameAt: require('./inc/AddFrameAt'),
|
|
checkFrame: require('./inc/CheckFrame'),
|
|
completeAnimation: require('./inc/CompleteAnimation'),
|
|
getFirstTick: require('./inc/GetFirstTick'),
|
|
getFrameAt: require('./inc/GetFrameAt'),
|
|
getNextTick: require('./inc/GetNextTick'),
|
|
load: require('./inc/Load'),
|
|
nextFrame: require('./inc/NextFrame'),
|
|
previousFrame: require('./inc/PreviousFrame'),
|
|
removeFrame: require('./inc/RemoveFrame'),
|
|
removeFrameAt: require('./inc/RemoveFrameAt'),
|
|
repeatAnimation: require('./inc/RepeatAnimation'),
|
|
setFrame: require('./inc/SetFrame'),
|
|
toJSON: require('./inc/ToJSON'),
|
|
updateFrameSequence: require('./inc/UpdateFrameSequence'),
|
|
|
|
pause: function ()
|
|
{
|
|
this.paused = true;
|
|
},
|
|
|
|
resume: function ()
|
|
{
|
|
this.paused = false;
|
|
},
|
|
|
|
destroy: function ()
|
|
{
|
|
|
|
}
|
|
|
|
});
|
|
|
|
module.exports = Animation;
|