phaser/src/gameobjects/BuildGameObjectAnimation.js

92 lines
2.6 KiB
JavaScript
Raw Normal View History

2018-02-12 16:01:20 +00:00
/**
* @author Richard Davey <rich@photonstorm.com>
2023-01-02 17:36:27 +00:00
* @copyright 2013-2023 Photon Storm Ltd.
2019-05-10 15:15:04 +00:00
* @license {@link https://opensource.org/licenses/MIT|MIT License}
2018-02-12 16:01:20 +00:00
*/
2017-04-11 02:13:30 +00:00
var GetAdvancedValue = require('../utils/object/GetAdvancedValue');
2018-02-12 17:03:53 +00:00
/**
* Adds an Animation component to a Sprite and populates it based on the given config.
*
2018-03-16 00:46:39 +00:00
* @function Phaser.GameObjects.BuildGameObjectAnimation
2018-02-12 17:03:53 +00:00
* @since 3.0.0
*
* @param {Phaser.GameObjects.Sprite} sprite - The sprite to add an Animation component to.
* @param {object} config - The animation config.
2018-02-12 17:03:53 +00:00
*
* @return {Phaser.GameObjects.Sprite} The updated Sprite.
*/
2017-04-11 02:13:30 +00:00
var BuildGameObjectAnimation = function (sprite, config)
{
var animConfig = GetAdvancedValue(config, 'anims', null);
if (animConfig === null)
{
return sprite;
}
if (typeof animConfig === 'string')
{
// { anims: 'key' }
sprite.anims.play(animConfig);
}
else if (typeof animConfig === 'object')
{
// { anims: {
// key: string
2020-11-23 10:32:00 +00:00
// startFrame: [string|number]
2017-04-11 02:13:30 +00:00
// delay: [float]
// repeat: [integer]
// repeatDelay: [float]
// yoyo: [boolean]
// play: [boolean]
// delayedPlay: [boolean]
// }
// }
var anims = sprite.anims;
2017-04-11 02:13:30 +00:00
var key = GetAdvancedValue(animConfig, 'key', undefined);
if (key)
{
var startFrame = GetAdvancedValue(animConfig, 'startFrame', undefined);
var delay = GetAdvancedValue(animConfig, 'delay', 0);
var repeat = GetAdvancedValue(animConfig, 'repeat', 0);
var repeatDelay = GetAdvancedValue(animConfig, 'repeatDelay', 0);
var yoyo = GetAdvancedValue(animConfig, 'yoyo', false);
2017-04-11 02:13:30 +00:00
var play = GetAdvancedValue(animConfig, 'play', false);
var delayedPlay = GetAdvancedValue(animConfig, 'delayedPlay', 0);
2017-04-11 02:13:30 +00:00
var playConfig = {
key: key,
delay: delay,
repeat: repeat,
repeatDelay: repeatDelay,
yoyo: yoyo,
startFrame: startFrame
};
if (play)
{
anims.play(playConfig);
}
else if (delayedPlay > 0)
{
2020-09-04 16:16:51 +00:00
anims.playAfterDelay(playConfig, delayedPlay);
}
else
{
anims.load(playConfig);
}
2017-04-11 02:13:30 +00:00
}
}
return sprite;
};
module.exports = BuildGameObjectAnimation;