phaser/src/gameobjects/sprite/Sprite.js
2018-02-09 03:44:23 +00:00

142 lines
4.2 KiB
JavaScript

var Class = require('../../utils/Class');
var Components = require('../components');
var GameObject = require('../GameObject');
var SpriteRender = require('./SpriteRender');
/**
* @classdesc
* A Sprite Game Object.
*
* A Sprite Game Object is used for the display of both static and animated images in your game.
* Sprites can have input events and physics bodies. They can also be tweened, tinted, scrolled
* 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
* @memberOf Phaser.GameObjects
* @constructor
* @since 3.0.0
*
* @extends Phaser.GameObjects.Components.Alpha
* @extends Phaser.GameObjects.Components.Animation
* @extends Phaser.GameObjects.Components.BlendMode
* @extends Phaser.GameObjects.Components.Depth
* @extends Phaser.GameObjects.Components.Flip
* @extends Phaser.GameObjects.Components.GetBounds
* @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
* @extends Phaser.GameObjects.Components.Texture
* @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.
* @param {string|integer} [frame] - An optional frame from the Texture this Game Object is rendering with.
*/
var Sprite = new Class({
Extends: GameObject,
Mixins: [
Components.Alpha,
Components.BlendMode,
Components.Depth,
Components.Flip,
Components.GetBounds,
Components.Origin,
Components.Pipeline,
Components.ScaleMode,
Components.ScrollFactor,
Components.Size,
Components.Texture,
Components.Tint,
Components.Transform,
Components.Visible,
SpriteRender
],
initialize:
function Sprite (scene, x, y, texture, frame)
{
GameObject.call(this, scene, 'Sprite');
/**
* [description]
*
* @name Phaser.GameObjects.Sprite#anims
* @type {[type]}
* @since 3.0.0
*/
this.anims = new Components.Animation(this);
this.setTexture(texture, frame);
this.setPosition(x, y);
this.setSizeToFrame();
this.setOrigin();
this.initPipeline('TextureTintPipeline');
},
/**
* [description]
*
* @method Phaser.GameObjects.Sprite#preUpdate
* @since 3.0.0
*
* @param {number} time - [description]
* @param {number} delta - [description]
*/
preUpdate: function (time, delta)
{
this.anims.update(time, delta);
},
/**
* [description]
*
* @method Phaser.GameObjects.Sprite#play
* @since 3.0.0
*
* @param {string} key - [description]
* @param {boolean} ignoreIfPlaying - [description]
* @param {integer|string} startFrame - [description]
*
* @return {[type]} [description]
*/
play: function (key, ignoreIfPlaying, startFrame)
{
this.anims.play(key, ignoreIfPlaying, startFrame);
return this;
},
/**
* [description]
*
* @method Phaser.GameObjects.Sprite#toJSON
* @since 3.0.0
*
* @return {object} [description]
*/
toJSON: function ()
{
var data = Components.ToJSON(this);
// Extra Sprite data is added here
return data;
}
});
module.exports = Sprite;