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}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2015-06-17 00:25:56 +00:00
|
|
|
* A TileSprite is a Sprite that has a repeating texture. The texture can be scrolled and scaled independently of the TileSprite itself.
|
|
|
|
* Textures will automatically wrap and are designed so that you can create game backdrops using seamless textures as a source.
|
|
|
|
*
|
|
|
|
* TileSprites have no input handler or physics bodies by default, both need enabling in the same way as for normal Sprites.
|
|
|
|
*
|
2015-07-21 12:24:12 +00:00
|
|
|
* You shouldn't ever create a TileSprite any larger than your actual screen size. If you want to create a large repeating background
|
|
|
|
* that scrolls across the whole map of your game, then you create a TileSprite that fits the screen size and then use the `tilePosition`
|
|
|
|
* property to scroll the texture as the player moves. If you create a TileSprite that is thousands of pixels in size then it will
|
|
|
|
* consume huge amounts of memory and cause performance issues. Remember: use `tilePosition` to scroll your texture and `tileScale` to
|
|
|
|
* adjust the scale of the texture - don't resize the sprite itself or make it larger than it needs.
|
|
|
|
*
|
2015-06-17 00:25:56 +00:00
|
|
|
* An important note about texture dimensions:
|
|
|
|
*
|
2015-07-21 12:24:12 +00:00
|
|
|
* When running under Canvas a TileSprite can use any texture size without issue. When running under WebGL the texture should ideally be
|
2015-06-17 00:25:56 +00:00
|
|
|
* a power of two in size (i.e. 4, 8, 16, 32, 64, 128, 256, 512, etch pixels width by height). If the texture isn't a power of two
|
|
|
|
* it will be rendered to a blank canvas that is the correct size, which means you may have 'blank' areas appearing to the right and
|
|
|
|
* bottom of your frame. To avoid this ensure your textures are perfect powers of two.
|
|
|
|
*
|
|
|
|
* TileSprites support animations in the same way that Sprites do. You add and play animations using the AnimationManager. However
|
|
|
|
* if your game is running under WebGL please note that each frame of the animation must be a power of two in size, or it will receive
|
|
|
|
* additional padding to enforce it to be so.
|
2014-02-13 15:03:46 +00:00
|
|
|
*
|
|
|
|
* @class Phaser.TileSprite
|
2013-10-01 12:54:29 +00:00
|
|
|
* @constructor
|
2014-10-19 23:54:56 +00:00
|
|
|
* @extends PIXI.TilingSprite
|
2015-03-01 07:00:17 +00:00
|
|
|
* @extends Phaser.Component.Core
|
|
|
|
* @extends Phaser.Component.Angle
|
|
|
|
* @extends Phaser.Component.Animation
|
|
|
|
* @extends Phaser.Component.AutoCull
|
|
|
|
* @extends Phaser.Component.Bounds
|
2015-03-30 12:51:47 +00:00
|
|
|
* @extends Phaser.Component.BringToTop
|
2015-03-01 07:00:17 +00:00
|
|
|
* @extends Phaser.Component.Destroy
|
|
|
|
* @extends Phaser.Component.FixedToCamera
|
2015-03-30 12:51:47 +00:00
|
|
|
* @extends Phaser.Component.Health
|
|
|
|
* @extends Phaser.Component.InCamera
|
2015-03-01 07:00:17 +00:00
|
|
|
* @extends Phaser.Component.InputEnabled
|
|
|
|
* @extends Phaser.Component.InWorld
|
2015-03-30 12:51:47 +00:00
|
|
|
* @extends Phaser.Component.LifeSpan
|
2015-03-01 07:00:17 +00:00
|
|
|
* @extends Phaser.Component.LoadTexture
|
|
|
|
* @extends Phaser.Component.Overlap
|
|
|
|
* @extends Phaser.Component.PhysicsBody
|
|
|
|
* @extends Phaser.Component.Reset
|
|
|
|
* @extends Phaser.Component.Smoothed
|
2014-02-13 23:13:10 +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 TileSprite at.
|
|
|
|
* @param {number} y - The y coordinate (in world space) to position the TileSprite at.
|
|
|
|
* @param {number} width - The width of the TileSprite.
|
|
|
|
* @param {number} height - The height of the TileSprite.
|
2016-04-06 22:58:34 +00:00
|
|
|
* @param {string|Phaser.BitmapData|PIXI.Texture} key - This is the image or texture used by the TileSprite during rendering. It can be a string which is a reference to the Phaser Image Cache entry, or an instance of a RenderTexture, PIXI.Texture or BitmapData.
|
2014-02-13 23:13:10 +00:00
|
|
|
* @param {string|number} frame - If this TileSprite 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
|
|
|
*/
|
2014-02-13 23:13:10 +00:00
|
|
|
Phaser.TileSprite = function (game, x, y, width, height, key, frame) {
|
2013-09-03 02:19:42 +00:00
|
|
|
|
|
|
|
x = x || 0;
|
|
|
|
y = y || 0;
|
|
|
|
width = width || 256;
|
|
|
|
height = height || 256;
|
|
|
|
key = key || null;
|
2014-02-13 23:13:10 +00:00
|
|
|
frame = frame || null;
|
2013-09-03 02:19:42 +00:00
|
|
|
|
2013-11-25 03:13:04 +00:00
|
|
|
/**
|
|
|
|
* @property {number} type - The const type of this object.
|
|
|
|
* @readonly
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2013-11-25 03:13:04 +00:00
|
|
|
this.type = Phaser.TILESPRITE;
|
2013-09-12 20:54:41 +00:00
|
|
|
|
2015-03-28 00:56:02 +00:00
|
|
|
/**
|
|
|
|
* @property {number} physicsType - The const physics body type of this object.
|
|
|
|
* @readonly
|
|
|
|
*/
|
|
|
|
this.physicsType = Phaser.SPRITE;
|
|
|
|
|
2014-02-13 23:13:10 +00:00
|
|
|
/**
|
|
|
|
* @property {Phaser.Point} _scroll - Internal cache var.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._scroll = new Phaser.Point();
|
|
|
|
|
2015-07-21 12:24:12 +00:00
|
|
|
var def = game.cache.getImage('__default', true);
|
|
|
|
|
|
|
|
PIXI.TilingSprite.call(this, new PIXI.Texture(def.base), width, height);
|
2014-02-13 23:13:10 +00:00
|
|
|
|
2015-02-17 06:00:41 +00:00
|
|
|
Phaser.Component.Core.init.call(this, game, x, y, key, frame);
|
2014-06-11 13:38:14 +00:00
|
|
|
|
2013-09-03 02:19:42 +00:00
|
|
|
};
|
|
|
|
|
2014-02-13 15:03:46 +00:00
|
|
|
Phaser.TileSprite.prototype = Object.create(PIXI.TilingSprite.prototype);
|
2013-09-03 02:19:42 +00:00
|
|
|
Phaser.TileSprite.prototype.constructor = Phaser.TileSprite;
|
2014-02-13 23:13:10 +00:00
|
|
|
|
2015-03-23 19:19:07 +00:00
|
|
|
Phaser.Component.Core.install.call(Phaser.TileSprite.prototype, [
|
2015-02-23 04:44:11 +00:00
|
|
|
'Angle',
|
|
|
|
'Animation',
|
|
|
|
'AutoCull',
|
|
|
|
'Bounds',
|
2015-03-30 12:51:47 +00:00
|
|
|
'BringToTop',
|
2015-02-23 04:44:11 +00:00
|
|
|
'Destroy',
|
|
|
|
'FixedToCamera',
|
2015-03-30 12:51:47 +00:00
|
|
|
'Health',
|
|
|
|
'InCamera',
|
2015-02-23 04:44:11 +00:00
|
|
|
'InputEnabled',
|
|
|
|
'InWorld',
|
2015-03-30 12:51:47 +00:00
|
|
|
'LifeSpan',
|
2015-02-23 04:44:11 +00:00
|
|
|
'LoadTexture',
|
|
|
|
'Overlap',
|
|
|
|
'PhysicsBody',
|
|
|
|
'Reset',
|
|
|
|
'Smoothed'
|
2015-03-23 19:19:07 +00:00
|
|
|
]);
|
2015-02-23 04:44:11 +00:00
|
|
|
|
2015-02-25 02:49:50 +00:00
|
|
|
Phaser.TileSprite.prototype.preUpdatePhysics = Phaser.Component.PhysicsBody.preUpdate;
|
|
|
|
Phaser.TileSprite.prototype.preUpdateLifeSpan = Phaser.Component.LifeSpan.preUpdate;
|
|
|
|
Phaser.TileSprite.prototype.preUpdateInWorld = Phaser.Component.InWorld.preUpdate;
|
|
|
|
Phaser.TileSprite.prototype.preUpdateCore = Phaser.Component.Core.preUpdate;
|
|
|
|
|
2014-02-13 23:13:10 +00:00
|
|
|
/**
|
|
|
|
* Automatically called by World.preUpdate.
|
|
|
|
*
|
|
|
|
* @method Phaser.TileSprite#preUpdate
|
|
|
|
* @memberof Phaser.TileSprite
|
|
|
|
*/
|
|
|
|
Phaser.TileSprite.prototype.preUpdate = function() {
|
|
|
|
|
2014-03-21 18:04:24 +00:00
|
|
|
if (this._scroll.x !== 0)
|
|
|
|
{
|
|
|
|
this.tilePosition.x += this._scroll.x * this.game.time.physicsElapsed;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._scroll.y !== 0)
|
|
|
|
{
|
|
|
|
this.tilePosition.y += this._scroll.y * this.game.time.physicsElapsed;
|
|
|
|
}
|
|
|
|
|
2015-02-25 02:49:50 +00:00
|
|
|
if (!this.preUpdatePhysics() || !this.preUpdateLifeSpan() || !this.preUpdateInWorld())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2014-02-28 19:45:15 +00:00
|
|
|
|
2015-02-25 02:49:50 +00:00
|
|
|
return this.preUpdateCore();
|
2014-02-13 23:13:10 +00:00
|
|
|
|
2014-03-23 08:40:24 +00:00
|
|
|
};
|
2014-02-13 23:13:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets this TileSprite to automatically scroll in the given direction until stopped via TileSprite.stopScroll().
|
|
|
|
* The scroll speed is specified in pixels per second.
|
|
|
|
* A negative x value will scroll to the left. A positive x value will scroll to the right.
|
|
|
|
* A negative y value will scroll up. A positive y value will scroll down.
|
|
|
|
*
|
|
|
|
* @method Phaser.TileSprite#autoScroll
|
|
|
|
* @memberof Phaser.TileSprite
|
2014-08-07 12:47:54 +00:00
|
|
|
* @param {number} x - Horizontal scroll speed in pixels per second.
|
|
|
|
* @param {number} y - Vertical scroll speed in pixels per second.
|
2014-02-13 23:13:10 +00:00
|
|
|
*/
|
|
|
|
Phaser.TileSprite.prototype.autoScroll = function(x, y) {
|
|
|
|
|
|
|
|
this._scroll.set(x, y);
|
|
|
|
|
2014-03-23 08:40:24 +00:00
|
|
|
};
|
2014-02-13 23:13:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Stops an automatically scrolling TileSprite.
|
|
|
|
*
|
|
|
|
* @method Phaser.TileSprite#stopScroll
|
|
|
|
* @memberof Phaser.TileSprite
|
|
|
|
*/
|
|
|
|
Phaser.TileSprite.prototype.stopScroll = function() {
|
|
|
|
|
|
|
|
this._scroll.set(0, 0);
|
|
|
|
|
2014-03-23 08:40:24 +00:00
|
|
|
};
|
2014-02-13 23:13:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Destroys the TileSprite. This removes it from its parent group, destroys the event and animation handlers if present
|
|
|
|
* and nulls its reference to game, freeing it up for garbage collection.
|
2014-03-23 07:59:28 +00:00
|
|
|
*
|
2014-02-13 23:13:10 +00:00
|
|
|
* @method Phaser.TileSprite#destroy
|
|
|
|
* @memberof Phaser.TileSprite
|
2014-02-27 20:05:16 +00:00
|
|
|
* @param {boolean} [destroyChildren=true] - Should every child of this object have its destroy method called?
|
2014-02-13 23:13:10 +00:00
|
|
|
*/
|
2014-02-27 20:05:16 +00:00
|
|
|
Phaser.TileSprite.prototype.destroy = function(destroyChildren) {
|
|
|
|
|
2015-02-17 06:00:41 +00:00
|
|
|
Phaser.Component.Destroy.prototype.destroy.call(this, destroyChildren);
|
2014-04-22 00:43:22 +00:00
|
|
|
|
2015-02-03 21:32:25 +00:00
|
|
|
PIXI.TilingSprite.prototype.destroy.call(this);
|
|
|
|
|
2014-03-23 08:40:24 +00:00
|
|
|
};
|
2014-02-13 23:13:10 +00:00
|
|
|
|
2014-03-20 03:48:54 +00:00
|
|
|
/**
|
|
|
|
* Resets the TileSprite. This places the TileSprite at the given x/y world coordinates, resets the tilePosition and then
|
|
|
|
* sets alive, exists, visible and renderable all to true. Also resets the outOfBounds state.
|
|
|
|
* If the TileSprite has a physics body that too is reset.
|
2014-06-30 10:49:53 +00:00
|
|
|
*
|
2014-03-20 03:48:54 +00:00
|
|
|
* @method Phaser.TileSprite#reset
|
|
|
|
* @memberof Phaser.TileSprite
|
|
|
|
* @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.
|
2015-10-27 08:10:14 +00:00
|
|
|
* @return {Phaser.TileSprite} This instance.
|
2014-03-20 03:48:54 +00:00
|
|
|
*/
|
|
|
|
Phaser.TileSprite.prototype.reset = function(x, y) {
|
|
|
|
|
2015-02-17 06:00:41 +00:00
|
|
|
Phaser.Component.Reset.prototype.reset.call(this, x, y);
|
2014-03-20 03:48:54 +00:00
|
|
|
|
|
|
|
this.tilePosition.x = 0;
|
|
|
|
this.tilePosition.y = 0;
|
|
|
|
|
|
|
|
return this;
|
2014-06-30 10:49:53 +00:00
|
|
|
|
2014-03-20 03:48:54 +00:00
|
|
|
};
|