phaser/wip/archived/gameobjects/sprite/Sprite.js

109 lines
3.1 KiB
JavaScript
Raw Normal View History

2013-10-01 12:54:29 +00:00
/**
* @author Richard Davey <rich@photonstorm.com>
2016-04-04 21:15:01 +00:00
* @copyright 2016 Photon Storm Ltd.
2013-10-01 12:54:29 +00:00
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* Sprites are the lifeblood of your game, used for nearly everything visual.
2013-11-28 15:57:09 +00:00
*
2013-10-25 14:02:21 +00:00
* At its most basic a Sprite consists of a set of coordinates and a texture that is rendered to the canvas.
* They also contain additional properties allowing for physics motion (via Sprite.body), input handling (via Sprite.input),
* events (via Sprite.events), animation (via Sprite.animations), camera culling and more. Please see the Examples for use cases.
*
* @class Phaser.GameObject.Sprite
2013-10-01 12:54:29 +00:00
* @constructor
* @extends Phaser.GameObject
2013-10-25 14:02:21 +00:00
* @param {Phaser.Game} game - A reference to the currently running game.
* @param {number} x - The x coordinate (in world space) to position the Sprite at.
* @param {number} y - The y coordinate (in world space) to position the Sprite at.
2013-11-13 20:57:09 +00:00
* @param {string|Phaser.RenderTexture|Phaser.BitmapData|PIXI.Texture} key - This is the image or texture used by the Sprite during rendering. It can be a string which is a reference to the Cache entry, or an instance of a RenderTexture or PIXI.Texture.
2013-10-25 14:02:21 +00:00
* @param {string|number} frame - If this Sprite is using part of a sprite sheet or texture atlas you can specify the exact frame to use by giving a string or numeric index.
2013-10-01 12:54:29 +00:00
*/
2016-10-12 14:19:04 +00:00
Phaser.GameObject.Sprite = function (game, x, y, key, frame)
{
2016-10-14 03:09:22 +00:00
this.game = game;
var _texture = game.textures.get(key);
var _frame = _texture.get(frame);
Phaser.GameObject.call(this, game, x, y, _texture, _frame);
2014-03-23 07:59:28 +00:00
2013-10-01 12:54:29 +00:00
/**
* @property {number} type - The const type of this object.
* @readonly
*/
this.type = Phaser.SPRITE;
2015-03-23 15:04:27 +00:00
/**
* @property {number} physicsType - The const physics body type of this object.
* @readonly
*/
this.physicsType = Phaser.SPRITE;
2016-10-14 03:09:22 +00:00
this.children = new Phaser.Component.Children(this);
};
Phaser.GameObject.Sprite.prototype = Object.create(Phaser.GameObject.prototype);
Phaser.GameObject.Sprite.prototype.constructor = Phaser.GameObject.Sprite;
/**
* Automatically called by World.preUpdate.
2013-10-25 14:02:21 +00:00
*
2016-10-14 03:09:22 +00:00
* @method Phaser.Sprite#preUpdate
* @memberof Phaser.Sprite
2013-10-01 12:54:29 +00:00
*/
2016-10-14 03:09:22 +00:00
Phaser.GameObject.Sprite.prototype.preUpdate = function ()
{
if (this.parent)
{
this.color.worldAlpha = this.parent.color.worldAlpha;
}
2016-10-14 05:31:01 +00:00
this.children.preUpdate();
2016-10-14 03:09:22 +00:00
};
// Phaser.GameObject.Sprite.prototype.update = function ()
// {
// };
// Phaser.GameObject.Sprite.prototype.postUpdate = function ()
// {
// };
2016-10-14 03:09:22 +00:00
Object.defineProperties(Phaser.GameObject.Sprite.prototype, {
width: {
enumerable: true,
get: function ()
{
return this.transform._scaleX * this.frame.realWidth;
},
set: function (value)
{
this.scaleX = value / this.frame.realWidth;
}
},
height: {
enumerable: true,
get: function ()
{
return this.transform._scaleY * this.frame.realHeight;
},
set: function (value)
{
this.scaleY = value / this.frame.realHeight;
}
}
});