phaser/v3/src/components/Animation.js

86 lines
1.7 KiB
JavaScript
Raw Normal View History

2017-04-04 22:59:16 +00:00
/**
* The Animation Component.
* Should be as small as possible, really just playhead info.
*
* @class
*/
var Animation = function (parent)
{
// Sprite / Game Object
this.parent = parent;
this.animationManager = parent.state.sys.anims;
this.isPlaying = false;
2017-04-05 00:15:53 +00:00
// Reference to the Phaser.Animation object
2017-04-04 22:59:16 +00:00
this.currentAnim = null;
2017-04-05 00:15:53 +00:00
// Reference to the Phaser.AnimationFrame object
2017-04-04 22:59:16 +00:00
this.currentFrame = null;
2017-04-05 00:15:53 +00:00
// Timing playhead values
this.accumulator = 0;
2017-04-04 22:59:16 +00:00
this.nextTick = 0;
};
Animation.prototype.constructor = Animation;
Animation.prototype = {
load: function (key, startFrame)
{
if (startFrame === undefined) { startFrame = 0; }
2017-04-05 00:15:53 +00:00
if (this.isPlaying)
{
this.stop();
}
2017-04-04 22:59:16 +00:00
// Load the new animation in
2017-04-05 00:15:53 +00:00
this.animationManager.load(this, key, startFrame);
2017-04-04 22:59:16 +00:00
2017-04-05 00:15:53 +00:00
this.updateFrame();
2017-04-04 22:59:16 +00:00
},
play: function (key, startFrame)
{
if (startFrame === undefined) { startFrame = 0; }
this.load(key, startFrame);
2017-04-05 00:15:53 +00:00
this.accumulator = 0;
this.currentAnim.getNextTick(this);
2017-04-04 22:59:16 +00:00
2017-04-05 00:15:53 +00:00
this.isPlaying = true;
2017-04-04 22:59:16 +00:00
},
stop: function ()
{
this.isPlaying = false;
},
2017-04-05 00:15:53 +00:00
updateFrame: function ()
{
this.parent.texture = this.currentFrame.frame.texture;
this.parent.frame = this.currentFrame.frame;
},
2017-04-04 22:59:16 +00:00
update: function (timestamp, frameDelta)
{
2017-04-05 00:15:53 +00:00
if (this.isPlaying)
2017-04-04 22:59:16 +00:00
{
2017-04-05 00:15:53 +00:00
this.accumulator += frameDelta;
if (this.accumulator >= this.nextTick)
{
this.currentAnim.setFrame(this);
}
2017-04-04 22:59:16 +00:00
}
}
};
module.exports = Animation;