2018-02-12 16:01:20 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
|
|
|
* @copyright 2018 Photon Storm Ltd.
|
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
2017-02-23 03:54:54 +00:00
|
|
|
var Class = require('../../utils/Class');
|
2017-07-04 00:59:31 +00:00
|
|
|
var Components = require('../components');
|
2017-12-15 04:07:45 +00:00
|
|
|
var GameObject = require('../GameObject');
|
2017-02-23 03:54:54 +00:00
|
|
|
var SpriteRender = require('./SpriteRender');
|
|
|
|
|
2018-02-06 14:13:30 +00:00
|
|
|
/**
|
2018-02-07 15:27:21 +00:00
|
|
|
* @classdesc
|
2018-02-06 14:13:30 +00:00
|
|
|
* A Sprite Game Object.
|
|
|
|
*
|
|
|
|
* A Sprite Game Object is used for the display of both static and animated images in your game.
|
2018-02-09 03:44:23 +00:00
|
|
|
* Sprites can have input events and physics bodies. They can also be tweened, tinted, scrolled
|
2018-02-06 14:13:30 +00:00
|
|
|
* and animated.
|
|
|
|
*
|
|
|
|
* The main difference between a Sprite and an Image Game Object is that you cannot animate Images.
|
|
|
|
* As such, Sprites take a fraction longer to process and have a larger API footprint due to the Animation
|
|
|
|
* Component. If you do not require animation then you can safely use Images to replace Sprites in all cases.
|
|
|
|
*
|
|
|
|
* @class Sprite
|
|
|
|
* @extends Phaser.GameObjects.GameObject
|
2018-10-10 09:49:13 +00:00
|
|
|
* @memberof Phaser.GameObjects
|
2018-02-06 14:13:30 +00:00
|
|
|
* @constructor
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @extends Phaser.GameObjects.Components.Alpha
|
|
|
|
* @extends Phaser.GameObjects.Components.BlendMode
|
|
|
|
* @extends Phaser.GameObjects.Components.Depth
|
|
|
|
* @extends Phaser.GameObjects.Components.Flip
|
|
|
|
* @extends Phaser.GameObjects.Components.GetBounds
|
2018-04-20 17:57:49 +00:00
|
|
|
* @extends Phaser.GameObjects.Components.Mask
|
2018-02-06 14:13:30 +00:00
|
|
|
* @extends Phaser.GameObjects.Components.Origin
|
|
|
|
* @extends Phaser.GameObjects.Components.Pipeline
|
|
|
|
* @extends Phaser.GameObjects.Components.ScaleMode
|
|
|
|
* @extends Phaser.GameObjects.Components.ScrollFactor
|
|
|
|
* @extends Phaser.GameObjects.Components.Size
|
2018-07-05 12:06:28 +00:00
|
|
|
* @extends Phaser.GameObjects.Components.TextureCrop
|
2018-02-06 14:13:30 +00:00
|
|
|
* @extends Phaser.GameObjects.Components.Tint
|
|
|
|
* @extends Phaser.GameObjects.Components.Transform
|
|
|
|
* @extends Phaser.GameObjects.Components.Visible
|
|
|
|
*
|
|
|
|
* @param {Phaser.Scene} scene - The Scene to which this Game Object belongs. A Game Object can only belong to one Scene at a time.
|
|
|
|
* @param {number} x - The horizontal position of this Game Object in the world.
|
|
|
|
* @param {number} y - The vertical position of this Game Object in the world.
|
|
|
|
* @param {string} texture - The key of the Texture this Game Object will use to render with, as stored in the Texture Manager.
|
2018-03-20 14:56:31 +00:00
|
|
|
* @param {(string|integer)} [frame] - An optional frame from the Texture this Game Object is rendering with.
|
2018-02-06 14:13:30 +00:00
|
|
|
*/
|
2017-02-23 03:54:54 +00:00
|
|
|
var Sprite = new Class({
|
|
|
|
|
2017-04-05 01:10:48 +00:00
|
|
|
Extends: GameObject,
|
|
|
|
|
2017-02-23 03:54:54 +00:00
|
|
|
Mixins: [
|
|
|
|
Components.Alpha,
|
|
|
|
Components.BlendMode,
|
2018-02-01 00:50:15 +00:00
|
|
|
Components.Depth,
|
2017-04-04 22:59:16 +00:00
|
|
|
Components.Flip,
|
2017-03-02 02:06:13 +00:00
|
|
|
Components.GetBounds,
|
2018-04-20 17:57:49 +00:00
|
|
|
Components.Mask,
|
2017-03-02 02:06:13 +00:00
|
|
|
Components.Origin,
|
2018-01-29 21:46:48 +00:00
|
|
|
Components.Pipeline,
|
2017-02-23 03:54:54 +00:00
|
|
|
Components.ScaleMode,
|
2017-07-04 11:01:27 +00:00
|
|
|
Components.ScrollFactor,
|
2017-03-02 02:06:13 +00:00
|
|
|
Components.Size,
|
2018-07-05 12:06:28 +00:00
|
|
|
Components.TextureCrop,
|
2017-07-04 11:01:27 +00:00
|
|
|
Components.Tint,
|
2017-03-02 02:06:13 +00:00
|
|
|
Components.Transform,
|
2017-02-23 03:54:54 +00:00
|
|
|
Components.Visible,
|
|
|
|
SpriteRender
|
|
|
|
],
|
|
|
|
|
|
|
|
initialize:
|
|
|
|
|
2017-07-14 13:30:20 +00:00
|
|
|
function Sprite (scene, x, y, texture, frame)
|
2017-02-23 03:54:54 +00:00
|
|
|
{
|
2017-07-14 13:30:20 +00:00
|
|
|
GameObject.call(this, scene, 'Sprite');
|
2017-02-23 03:54:54 +00:00
|
|
|
|
2018-07-18 23:18:09 +00:00
|
|
|
/**
|
|
|
|
* The internal crop data object, as used by `setCrop` and passed to the `Frame.setCropUVs` method.
|
|
|
|
*
|
|
|
|
* @name Phaser.GameObjects.Sprite#_crop
|
|
|
|
* @type {object}
|
|
|
|
* @private
|
|
|
|
* @since 3.11.0
|
|
|
|
*/
|
2018-08-03 17:50:36 +00:00
|
|
|
this._crop = this.resetCropObject();
|
2018-07-18 23:18:09 +00:00
|
|
|
|
2018-02-06 14:13:30 +00:00
|
|
|
/**
|
2018-06-08 14:50:44 +00:00
|
|
|
* The Animation Controller of this Sprite.
|
2018-02-06 14:13:30 +00:00
|
|
|
*
|
|
|
|
* @name Phaser.GameObjects.Sprite#anims
|
2018-03-19 11:54:31 +00:00
|
|
|
* @type {Phaser.GameObjects.Components.Animation}
|
2018-02-06 14:13:30 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-04-04 22:59:16 +00:00
|
|
|
this.anims = new Components.Animation(this);
|
|
|
|
|
2017-02-23 03:54:54 +00:00
|
|
|
this.setTexture(texture, frame);
|
2017-03-02 02:06:13 +00:00
|
|
|
this.setPosition(x, y);
|
2017-03-02 04:00:39 +00:00
|
|
|
this.setSizeToFrame();
|
2018-02-09 15:23:26 +00:00
|
|
|
this.setOriginFromFrame();
|
2018-09-05 10:19:02 +00:00
|
|
|
this.initPipeline();
|
2017-04-04 22:59:16 +00:00
|
|
|
},
|
|
|
|
|
2018-02-06 14:13:30 +00:00
|
|
|
/**
|
2018-06-08 14:50:44 +00:00
|
|
|
* Update this Sprite's animations.
|
2018-02-06 14:13:30 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.GameObjects.Sprite#preUpdate
|
2018-04-18 12:29:22 +00:00
|
|
|
* @protected
|
2018-02-06 14:13:30 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-06-08 14:50:44 +00:00
|
|
|
* @param {number} time - The current timestamp.
|
|
|
|
* @param {number} delta - The delta time, in ms, elapsed since the last frame.
|
2018-02-06 14:13:30 +00:00
|
|
|
*/
|
2017-07-04 22:43:13 +00:00
|
|
|
preUpdate: function (time, delta)
|
2017-04-04 22:59:16 +00:00
|
|
|
{
|
2017-07-04 22:43:13 +00:00
|
|
|
this.anims.update(time, delta);
|
2017-04-05 03:41:53 +00:00
|
|
|
},
|
|
|
|
|
2018-02-06 14:13:30 +00:00
|
|
|
/**
|
2018-06-08 14:50:44 +00:00
|
|
|
* Start playing the given animation.
|
2018-02-06 14:13:30 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.GameObjects.Sprite#play
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-06-08 14:50:44 +00:00
|
|
|
* @param {string} key - The string-based key of the animation to play.
|
|
|
|
* @param {boolean} [ignoreIfPlaying=false] - If an animation is already playing then ignore this call.
|
|
|
|
* @param {integer} [startFrame=0] - Optionally start the animation playing from this frame index.
|
2018-02-06 14:13:30 +00:00
|
|
|
*
|
2018-03-05 02:24:47 +00:00
|
|
|
* @return {Phaser.GameObjects.Sprite} This Game Object.
|
2018-02-06 14:13:30 +00:00
|
|
|
*/
|
2017-09-26 08:45:04 +00:00
|
|
|
play: function (key, ignoreIfPlaying, startFrame)
|
2017-04-05 03:41:53 +00:00
|
|
|
{
|
2017-09-26 08:45:04 +00:00
|
|
|
this.anims.play(key, ignoreIfPlaying, startFrame);
|
2017-08-10 04:17:26 +00:00
|
|
|
|
|
|
|
return this;
|
2017-04-12 23:35:27 +00:00
|
|
|
},
|
|
|
|
|
2018-02-06 14:13:30 +00:00
|
|
|
/**
|
2018-06-08 14:50:44 +00:00
|
|
|
* Build a JSON representation of this Sprite.
|
2018-02-06 14:13:30 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.GameObjects.Sprite#toJSON
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-03-29 14:12:32 +00:00
|
|
|
* @return {JSONGameObject} A JSON representation of the Game Object.
|
2018-02-06 14:13:30 +00:00
|
|
|
*/
|
2017-04-12 23:35:27 +00:00
|
|
|
toJSON: function ()
|
|
|
|
{
|
|
|
|
var data = Components.ToJSON(this);
|
|
|
|
|
|
|
|
// Extra Sprite data is added here
|
|
|
|
|
|
|
|
return data;
|
2018-09-25 14:11:15 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles the pre-destroy step for the Sprite, which removes the Animation component.
|
|
|
|
*
|
|
|
|
* @method Phaser.GameObjects.Sprite#preDestroy
|
|
|
|
* @private
|
|
|
|
* @since 3.14.0
|
|
|
|
*/
|
|
|
|
preDestroy: function ()
|
|
|
|
{
|
|
|
|
this.anims.destroy();
|
|
|
|
|
|
|
|
this.anims = undefined;
|
2017-01-24 18:08:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = Sprite;
|