phaser/docs/Tilemap.js.html

2110 lines
65 KiB
HTML
Raw Normal View History

2013-11-28 15:57:09 +00:00
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Phaser Source: tilemap/Tilemap.js</title>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/sunlight.default.css">
<link type="text/css" rel="stylesheet" href="styles/site.cerulean.css">
</head>
<body>
<div class="container-fluid">
<div class="navbar navbar-fixed-top navbar-inverse">
<div class="navbar-inner">
<a class="brand" href="index.html">Phaser</a>
<ul class="nav">
<li class="dropdown">
<a href="namespaces.list.html" class="dropdown-toggle" data-toggle="dropdown">Namespaces<b
class="caret"></b></a>
<ul class="dropdown-menu ">
<li>
<a href="Phaser.html">Phaser</a>
</li>
</ul>
</li>
<li class="dropdown">
<a href="classes.list.html" class="dropdown-toggle" data-toggle="dropdown">Classes<b
class="caret"></b></a>
<ul class="dropdown-menu ">
<li>
<a href="Phaser.Animation.html">Animation</a>
</li>
<li>
<a href="Phaser.AnimationManager.html">AnimationManager</a>
</li>
<li>
<a href="Phaser.AnimationParser.html">AnimationParser</a>
</li>
<li>
<a href="Phaser.BitmapData.html">BitmapData</a>
</li>
<li>
<a href="Phaser.BitmapText.html">BitmapText</a>
</li>
<li>
<a href="Phaser.Button.html">Button</a>
</li>
<li>
<a href="Phaser.Cache.html">Cache</a>
</li>
<li>
<a href="Phaser.Camera.html">Camera</a>
</li>
<li>
<a href="Phaser.Canvas.html">Canvas</a>
</li>
<li>
<a href="Phaser.Circle.html">Circle</a>
</li>
<li>
<a href="Phaser.Color.html">Color</a>
</li>
<li>
<a href="Phaser.Device.html">Device</a>
</li>
<li>
<a href="Phaser.Easing.html">Easing</a>
</li>
<li>
<a href="Phaser.Easing.Back.html">Back</a>
</li>
<li>
<a href="Phaser.Easing.Bounce.html">Bounce</a>
</li>
<li>
<a href="Phaser.Easing.Circular.html">Circular</a>
</li>
<li>
<a href="Phaser.Easing.Cubic.html">Cubic</a>
</li>
<li>
<a href="Phaser.Easing.Elastic.html">Elastic</a>
</li>
<li>
<a href="Phaser.Easing.Exponential.html">Exponential</a>
</li>
<li>
<a href="Phaser.Easing.Linear.html">Linear</a>
</li>
<li>
<a href="Phaser.Easing.Quadratic.html">Quadratic</a>
</li>
<li>
<a href="Phaser.Easing.Quartic.html">Quartic</a>
</li>
<li>
<a href="Phaser.Easing.Quintic.html">Quintic</a>
</li>
<li>
<a href="Phaser.Easing.Sinusoidal.html">Sinusoidal</a>
</li>
<li>
<a href="Phaser.Ellipse.html">Ellipse</a>
</li>
2013-11-28 15:57:09 +00:00
<li>
<a href="Phaser.Events.html">Events</a>
</li>
<li>
<a href="Phaser.Filter.html">Filter</a>
</li>
<li>
<a href="Phaser.Frame.html">Frame</a>
</li>
<li>
<a href="Phaser.FrameData.html">FrameData</a>
</li>
<li>
<a href="Phaser.Game.html">Game</a>
</li>
<li>
<a href="Phaser.GameObjectCreator.html">GameObjectCreator</a>
</li>
2013-11-28 15:57:09 +00:00
<li>
<a href="Phaser.GameObjectFactory.html">GameObjectFactory</a>
</li>
2014-02-05 06:29:17 +00:00
<li>
<a href="Phaser.Gamepad.html">Gamepad</a>
</li>
<li>
<a href="Phaser.GamepadButton.html">GamepadButton</a>
</li>
2013-11-28 15:57:09 +00:00
<li>
<a href="Phaser.Graphics.html">Graphics</a>
</li>
<li>
<a href="Phaser.Group.html">Group</a>
</li>
<li>
<a href="Phaser.Image.html">Image</a>
</li>
2013-11-28 15:57:09 +00:00
<li>
<a href="Phaser.Input.html">Input</a>
</li>
<li>
<a href="Phaser.InputHandler.html">InputHandler</a>
</li>
<li>
<a href="Phaser.Key.html">Key</a>
</li>
<li>
<a href="Phaser.Keyboard.html">Keyboard</a>
</li>
2014-02-05 06:29:17 +00:00
<li>
<a href="Phaser.Line.html">Line</a>
</li>
2013-11-28 15:57:09 +00:00
<li>
<a href="Phaser.LinkedList.html">LinkedList</a>
</li>
<li>
<a href="Phaser.Loader.html">Loader</a>
</li>
<li>
<a href="Phaser.LoaderParser.html">LoaderParser</a>
</li>
<li>
<a href="Phaser.Math.html">Math</a>
</li>
<li>
<a href="Phaser.Mouse.html">Mouse</a>
</li>
<li>
<a href="Phaser.MSPointer.html">MSPointer</a>
</li>
<li>
<a href="Phaser.Net.html">Net</a>
</li>
<li>
<a href="Phaser.Particles.html">Particles</a>
</li>
<li>
<a href="Phaser.Particles.Arcade.Emitter.html">Emitter</a>
</li>
<li>
<a href="Phaser.Physics.html">Physics</a>
</li>
<li>
<a href="Phaser.Physics.Arcade.html">Arcade</a>
</li>
<li>
<a href="Phaser.Physics.Arcade.Body.html">Body</a>
</li>
<li>
<a href="Phaser.Physics.Ninja.html">Ninja</a>
</li>
<li>
<a href="Phaser.Physics.Ninja.AABB.html">AABB</a>
</li>
<li>
<a href="Phaser.Physics.Ninja.Body.html">Body</a>
</li>
<li>
<a href="Phaser.Physics.Ninja.Circle.html">Circle</a>
</li>
<li>
<a href="Phaser.Physics.Ninja.Tile.html">Tile</a>
</li>
<li>
<a href="Phaser.Physics.P2.html">P2</a>
</li>
<li>
<a href="Phaser.Physics.P2.Body.html">Body</a>
</li>
<li>
<a href="Phaser.Physics.P2.BodyDebug.html">BodyDebug</a>
</li>
<li>
<a href="Phaser.Physics.P2.CollisionGroup.html">CollisionGroup</a>
</li>
<li>
<a href="Phaser.Physics.P2.ContactMaterial.html">ContactMaterial</a>
</li>
<li>
<a href="Phaser.Physics.P2.DistanceConstraint.html">DistanceConstraint</a>
</li>
<li>
<a href="Phaser.Physics.P2.GearConstraint.html">GearConstraint</a>
</li>
<li>
<a href="Phaser.Physics.P2.InversePointProxy.html">InversePointProxy</a>
</li>
<li>
<a href="Phaser.Physics.P2.LockConstraint.html">LockConstraint</a>
</li>
<li>
<a href="Phaser.Physics.P2.Material.html">Material</a>
</li>
<li>
<a href="Phaser.Physics.P2.PointProxy.html">PointProxy</a>
</li>
<li>
<a href="Phaser.Physics.P2.PrismaticConstraint.html">PrismaticConstraint</a>
</li>
<li>
<a href="Phaser.Physics.P2.RevoluteConstraint.html">RevoluteConstraint</a>
</li>
<li>
<a href="Phaser.Physics.P2.Spring.html">Spring</a>
</li>
2013-11-28 15:57:09 +00:00
<li>
<a href="Phaser.Plugin.html">Plugin</a>
</li>
<li>
<a href="Phaser.PluginManager.html">PluginManager</a>
</li>
<li>
<a href="Phaser.Point.html">Point</a>
</li>
<li>
<a href="Phaser.Pointer.html">Pointer</a>
</li>
<li>
<a href="Phaser.Polygon.html">Polygon</a>
</li>
<li>
<a href="Phaser.QuadTree.html">QuadTree</a>
</li>
2013-11-28 15:57:09 +00:00
<li>
<a href="Phaser.RandomDataGenerator.html">RandomDataGenerator</a>
</li>
<li>
<a href="Phaser.Rectangle.html">Rectangle</a>
</li>
<li>
<a href="Phaser.RenderTexture.html">RenderTexture</a>
</li>
<li>
<a href="Phaser.RequestAnimationFrame.html">RequestAnimationFrame</a>
</li>
<li>
<a href="Phaser.RetroFont.html">RetroFont</a>
</li>
<li>
<a href="Phaser.ScaleManager.html">ScaleManager</a>
</li>
2013-11-28 15:57:09 +00:00
<li>
<a href="Phaser.Signal.html">Signal</a>
</li>
2014-02-05 06:29:17 +00:00
<li>
<a href="Phaser.SinglePad.html">SinglePad</a>
</li>
2013-11-28 15:57:09 +00:00
<li>
<a href="Phaser.Sound.html">Sound</a>
</li>
<li>
<a href="Phaser.SoundManager.html">SoundManager</a>
</li>
<li>
<a href="Phaser.Sprite.html">Sprite</a>
</li>
<li>
<a href="Phaser.SpriteBatch.html">SpriteBatch</a>
</li>
2013-11-28 15:57:09 +00:00
<li>
<a href="Phaser.Stage.html">Stage</a>
</li>
<li>
<a href="Phaser.State.html">State</a>
</li>
<li>
<a href="Phaser.StateManager.html">StateManager</a>
</li>
<li>
<a href="Phaser.Text.html">Text</a>
</li>
<li>
<a href="Phaser.Tile.html">Tile</a>
</li>
<li>
<a href="Phaser.Tilemap.html">Tilemap</a>
</li>
<li>
<a href="Phaser.TilemapLayer.html">TilemapLayer</a>
</li>
<li>
<a href="Phaser.TilemapParser.html">TilemapParser</a>
</li>
<li>
<a href="Phaser.Tileset.html">Tileset</a>
</li>
<li>
<a href="Phaser.TileSprite.html">TileSprite</a>
</li>
<li>
<a href="Phaser.Time.html">Time</a>
</li>
<li>
<a href="Phaser.Timer.html">Timer</a>
</li>
2014-02-05 06:29:17 +00:00
<li>
<a href="Phaser.TimerEvent.html">TimerEvent</a>
</li>
2013-11-28 15:57:09 +00:00
<li>
<a href="Phaser.Touch.html">Touch</a>
</li>
<li>
<a href="Phaser.Tween.html">Tween</a>
</li>
<li>
<a href="Phaser.TweenManager.html">TweenManager</a>
</li>
<li>
<a href="Phaser.Utils.html">Utils</a>
</li>
<li>
<a href="Phaser.Utils.Debug.html">Debug</a>
</li>
<li>
<a href="Phaser.World.html">World</a>
</li>
<li>
<a href="SignalBinding.html">SignalBinding</a>
</li>
</ul>
</li>
</ul>
</div>
</div>
<div class="row-fluid">
<div class="span12">
<div id="main">
<h1 class="page-title">Source: tilemap/Tilemap.js</h1>
<section>
<article>
<pre class="sunlight-highlight-javascript linenums">/**
* @author Richard Davey &lt;rich@photonstorm.com>
2014-02-05 06:29:17 +00:00
* @copyright 2014 Photon Storm Ltd.
2013-11-28 15:57:09 +00:00
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* Creates a new Phaser.Tilemap object. The map can either be populated with data from a Tiled JSON file or from a CSV file.
* To do this pass the Cache key as the first parameter. When using Tiled data you need only provide the key.
* When using CSV data you must provide the key and the tileWidth and tileHeight parameters.
* If creating a blank tilemap to be populated later, you can either specify no parameters at all and then use `Tilemap.create` or pass the map and tile dimensions here.
* Note that all Tilemaps use a base tile size to calculate dimensions from, but that a TilemapLayer may have its own unique tile size that overrides it.
* A Tile map is rendered to the display using a TilemapLayer. It is not added to the display list directly itself.
2013-11-28 15:57:09 +00:00
* A map may have multiple layers. You can perform operations on the map data such as copying, pasting, filling and shuffling the tiles around.
*
* @class Phaser.Tilemap
* @constructor
* @param {Phaser.Game} game - Game reference to the currently running game.
* @param {string} [key] - The key of the tilemap data as stored in the Cache. If you're creating a blank map either leave this parameter out or pass `null`.
* @param {number} [tileWidth=32] - The pixel width of a single map tile. If using CSV data you must specify this. Not required if using Tiled map data.
* @param {number} [tileHeight=32] - The pixel height of a single map tile. If using CSV data you must specify this. Not required if using Tiled map data.
* @param {number} [width=10] - The width of the map in tiles. If this map is created from Tiled or CSV data you don't need to specify this.
* @param {number} [height=10] - The height of the map in tiles. If this map is created from Tiled or CSV data you don't need to specify this.
2013-11-28 15:57:09 +00:00
*/
Phaser.Tilemap = function (game, key, tileWidth, tileHeight, width, height) {
2013-11-28 15:57:09 +00:00
/**
* @property {Phaser.Game} game - A reference to the currently running Game.
*/
this.game = game;
/**
2014-02-05 06:29:17 +00:00
* @property {string} key - The key of this map data in the Phaser.Cache.
2013-11-28 15:57:09 +00:00
*/
2014-02-05 06:29:17 +00:00
this.key = key;
2013-11-28 15:57:09 +00:00
var data = Phaser.TilemapParser.parse(this.game, key, tileWidth, tileHeight, width, height);
2013-11-28 15:57:09 +00:00
2014-02-05 06:29:17 +00:00
if (data === null)
2013-11-28 15:57:09 +00:00
{
2014-02-05 06:29:17 +00:00
return;
2013-11-28 15:57:09 +00:00
}
/**
* @property {number} width - The width of the map (in tiles).
*/
2014-02-05 06:29:17 +00:00
this.width = data.width;
/**
* @property {number} height - The height of the map (in tiles).
*/
2014-02-05 06:29:17 +00:00
this.height = data.height;
/**
* @property {number} tileWidth - The base width of the tiles in the map (in pixels).
*/
2014-02-05 06:29:17 +00:00
this.tileWidth = data.tileWidth;
/**
* @property {number} tileHeight - The base height of the tiles in the map (in pixels).
*/
2014-02-05 06:29:17 +00:00
this.tileHeight = data.tileHeight;
/**
* @property {string} orientation - The orientation of the map data (as specified in Tiled), usually 'orthogonal'.
*/
2014-02-05 06:29:17 +00:00
this.orientation = data.orientation;
/**
* @property {number} version - The version of the map data (as specified in Tiled, usually 1).
*/
2014-02-05 06:29:17 +00:00
this.version = data.version;
/**
* @property {object} properties - Map specific properties as specified in Tiled.
*/
2014-02-05 06:29:17 +00:00
this.properties = data.properties;
/**
* @property {number} widthInPixels - The width of the map in pixels based on width * tileWidth.
*/
2014-02-05 06:29:17 +00:00
this.widthInPixels = data.widthInPixels;
/**
* @property {number} heightInPixels - The height of the map in pixels based on height * tileHeight.
*/
2014-02-05 06:29:17 +00:00
this.heightInPixels = data.heightInPixels;
2013-11-28 15:57:09 +00:00
/**
2014-02-05 06:29:17 +00:00
* @property {array} layers - An array of Tilemap layer data.
2013-11-28 15:57:09 +00:00
*/
2014-02-05 06:29:17 +00:00
this.layers = data.layers;
2013-11-28 15:57:09 +00:00
/**
2014-02-05 06:29:17 +00:00
* @property {array} tilesets - An array of Tilesets.
2013-11-28 15:57:09 +00:00
*/
2014-02-05 06:29:17 +00:00
this.tilesets = data.tilesets;
2013-11-28 15:57:09 +00:00
2014-02-05 06:29:17 +00:00
/**
* @property {array} tiles - The super array of Tiles.
*/
this.tiles = data.tiles;
2013-11-28 15:57:09 +00:00
/**
2014-02-05 06:29:17 +00:00
* @property {array} objects - An array of Tiled Object Layers.
2013-11-28 15:57:09 +00:00
*/
2014-02-05 06:29:17 +00:00
this.objects = data.objects;
/**
* @property {array} collideIndexes - An array of tile indexes that collide.
*/
this.collideIndexes = [];
/**
* @property {array} collision - An array of collision data (polylines, etc).
*/
this.collision = data.collision;
2014-02-05 06:29:17 +00:00
/**
* @property {array} images - An array of Tiled Image Layers.
*/
this.images = data.images;
/**
* @property {number} currentLayer - The current layer.
*/
this.currentLayer = 0;
/**
* @property {array} debugMap - Map data used for debug values only.
*/
this.debugMap = [];
2013-11-28 15:57:09 +00:00
/**
* @property {array} _results - Internal var.
* @private
*/
this._results = [];
/**
* @property {number} _tempA - Internal var.
* @private
*/
this._tempA = 0;
/**
* @property {number} _tempB - Internal var.
* @private
*/
this._tempB = 0;
};
/**
* @constant
* @type {number}
*/
Phaser.Tilemap.CSV = 0;
/**
* @constant
* @type {number}
*/
Phaser.Tilemap.TILED_JSON = 1;
Phaser.Tilemap.prototype = {
/**
2014-03-24 12:41:45 +00:00
* Creates an empty map of the given dimensions and one blank layer. If layers already exist they are erased.
2013-11-28 15:57:09 +00:00
*
* @method Phaser.Tilemap#create
2014-03-24 12:41:45 +00:00
* @param {string} name - The name of the default layer of the map.
2013-11-28 15:57:09 +00:00
* @param {number} width - The width of the map in tiles.
* @param {number} height - The height of the map in tiles.
* @param {number} tileWidth - The width of the tiles the map uses for calculations.
* @param {number} tileHeight - The height of the tiles the map uses for calculations.
2014-03-24 12:41:45 +00:00
* @param {Phaser.Group} [group] - Optional Group to add the layer to. If not specified it will be added to the World group.
* @return {Phaser.TilemapLayer} The TilemapLayer object. This is an extension of Phaser.Image and can be moved around the display list accordingly.
2013-11-28 15:57:09 +00:00
*/
create: function (name, width, height, tileWidth, tileHeight, group) {
2014-03-24 12:41:45 +00:00
if (typeof group === 'undefined') { group = this.game.world; }
this.width = width;
this.height = height;
2013-11-28 15:57:09 +00:00
this.setTileSize(tileWidth, tileHeight);
2014-03-24 12:41:45 +00:00
this.layers.length = 0;
2013-11-28 15:57:09 +00:00
2014-03-24 12:41:45 +00:00
return this.createBlankLayer(name, width, height, tileWidth, tileHeight, group);
2013-11-28 15:57:09 +00:00
2014-02-05 06:29:17 +00:00
},
/**
* Sets the base tile size for the map.
*
* @method Phaser.Tilemap#setTileSize
* @param {number} tileWidth - The width of the tiles the map uses for calculations.
* @param {number} tileHeight - The height of the tiles the map uses for calculations.
*/
setTileSize: function (tileWidth, tileHeight) {
this.tileWidth = tileWidth;
this.tileHeight = tileHeight;
this.widthInPixels = this.width * tileWidth;
this.heightInPixels = this.height * tileHeight;
},
2014-02-05 06:29:17 +00:00
/**
* Adds an image to the map to be used as a tileset. A single map may use multiple tilesets.
* Note that the tileset name can be found in the JSON file exported from Tiled, or in the Tiled editor.
*
* @method Phaser.Tilemap#addTilesetImage
* @param {string} tileset - The name of the tileset as specified in the map data.
* @param {string} [key] - The key of the Phaser.Cache image used for this tileset. If not specified it will look for an image with a key matching the tileset parameter.
2014-03-24 12:41:45 +00:00
* @param {number} [tileWidth=32] - The width of the tiles in the Tileset Image. If not given it will default to the map.tileWidth value, if that isn't set then 32.
* @param {number} [tileHeight=32] - The height of the tiles in the Tileset Image. If not given it will default to the map.tileHeight value, if that isn't set then 32.
* @param {number} [tileMargin=0] - The width of the tiles in the Tileset Image. If not given it will default to the map.tileWidth value.
* @param {number} [tileSpacing=0] - The height of the tiles in the Tileset Image. If not given it will default to the map.tileHeight value.
* @param {number} [gid=0] - If adding multiple tilesets to a blank/dynamic map, specify the starting GID the set will use here.
2014-03-24 12:41:45 +00:00
* @return {Phaser.Tileset} Returns the Tileset object that was created or updated, or null if it failed.
2014-02-05 06:29:17 +00:00
*/
addTilesetImage: function (tileset, key, tileWidth, tileHeight, tileMargin, tileSpacing, gid) {
if (typeof tileWidth === 'undefined') { tileWidth = this.tileWidth; }
if (typeof tileHeight === 'undefined') { tileHeight = this.tileHeight; }
if (typeof tileMargin === 'undefined') { tileMargin = 0; }
if (typeof tileSpacing === 'undefined') { tileSpacing = 0; }
if (typeof gid === 'undefined') { gid = 0; }
2014-02-05 06:29:17 +00:00
2014-03-24 12:41:45 +00:00
// In-case we're working from a blank map
if (tileWidth === 0)
{
tileWidth = 32;
}
if (tileHeight === 0)
{
tileHeight = 32;
}
2014-02-05 06:29:17 +00:00
if (typeof key === 'undefined')
{
if (typeof tileset === 'string')
{
key = tileset;
}
else
{
2014-03-24 12:41:45 +00:00
return null;
2014-02-05 06:29:17 +00:00
}
}
if (typeof tileset === 'string')
{
tileset = this.getTilesetIndex(tileset);
}
if (this.tilesets[tileset])
{
this.tilesets[tileset].setImage(this.game.cache.getImage(key));
2014-03-24 12:41:45 +00:00
return this.tilesets[tileset];
2014-02-05 06:29:17 +00:00
}
else
{
var newSet = new Phaser.Tileset(key, gid, tileWidth, tileHeight, tileMargin, tileSpacing, {});
2014-02-05 06:29:17 +00:00
newSet.setImage(this.game.cache.getImage(key));
2014-02-05 06:29:17 +00:00
this.tilesets.push(newSet);
2014-02-05 06:29:17 +00:00
var i = this.tilesets.length - 1;
var x = tileMargin;
var y = tileMargin;
2014-02-05 06:29:17 +00:00
var count = 0;
var countX = 0;
var countY = 0;
for (var t = gid; t &lt; gid + newSet.total; t++)
{
this.tiles[t] = [x, y, i];
x += tileWidth + tileSpacing;
count++;
if (count === newSet.total)
{
break;
}
countX++;
if (countX === newSet.columns)
{
x = tileMargin;
y += tileHeight + tileSpacing;
countX = 0;
countY++;
2014-03-24 12:41:45 +00:00
if (countY === newSet.rows)
{
break;
}
}
}
2014-03-24 12:41:45 +00:00
return newSet;
}
2014-03-24 12:41:45 +00:00
return null;
2014-02-05 06:29:17 +00:00
},
/**
* Creates a Sprite for every object matching the given gid in the map data. You can optionally specify the group that the Sprite will be created in. If none is
* given it will be created in the World. All properties from the map data objectgroup are copied across to the Sprite, so you can use this as an easy way to
2014-03-24 12:41:45 +00:00
* configure Sprite properties from within the map editor. For example giving an object a property of alpha: 0.5 in the map editor will duplicate that when the
2014-02-05 06:29:17 +00:00
* Sprite is created. You could also give it a value like: body.velocity.x: 100 to set it moving automatically.
*
* @method Phaser.Tilemap#createFromObjects
2014-02-05 06:29:17 +00:00
* @param {string} name - The name of the Object Group to create Sprites from.
2014-03-24 12:41:45 +00:00
* @param {number} gid - The layer array index value, or if a string is given the layer name within the map data.
2014-02-05 06:29:17 +00:00
* @param {string} key - The Game.cache key of the image that this Sprite will use.
* @param {number|string} [frame] - If the Sprite image contains multiple frames you can specify which one to use here.
* @param {boolean} [exists=true] - The default exists state of the Sprite.
* @param {boolean} [autoCull=false] - The default autoCull state of the Sprite. Sprites that are autoCulled are culled from the camera if out of its range.
* @param {Phaser.Group} [group=Phaser.World] - Group to add the Sprite to. If not specified it will be added to the World group.
* @param {object} [CustomClass=Phaser.Sprite] - If you wish to create your own class, rather than Phaser.Sprite, pass the class here. Your class must extend Phaser.Sprite and have the same constructor parameters.
2014-03-24 12:41:45 +00:00
* @param {boolean} [adjustY=true] - By default the Tiled map editor uses a bottom-left coordinate system. Phaser uses top-left. So most objects will appear too low down. This parameter moves them up by their height.
2014-02-05 06:29:17 +00:00
*/
2014-03-24 12:41:45 +00:00
createFromObjects: function (name, gid, key, frame, exists, autoCull, group, CustomClass, adjustY) {
2014-02-05 06:29:17 +00:00
if (typeof exists === 'undefined') { exists = true; }
if (typeof autoCull === 'undefined') { autoCull = false; }
2014-02-05 06:29:17 +00:00
if (typeof group === 'undefined') { group = this.game.world; }
if (typeof CustomClass === 'undefined') { CustomClass = Phaser.Sprite; }
2014-03-24 12:41:45 +00:00
if (typeof adjustY === 'undefined') { adjustY = true; }
2014-02-05 06:29:17 +00:00
if (!this.objects[name])
{
console.warn('Tilemap.createFromObjects: Invalid objectgroup name given: ' + name);
return;
}
var sprite;
for (var i = 0, len = this.objects[name].length; i &lt; len; i++)
{
if (this.objects[name][i].gid === gid)
{
sprite = new CustomClass(this.game, this.objects[name][i].x, this.objects[name][i].y, key, frame);
2014-02-05 06:29:17 +00:00
sprite.name = this.objects[name][i].name;
sprite.visible = this.objects[name][i].visible;
sprite.autoCull = autoCull;
sprite.exists = exists;
2014-02-05 06:29:17 +00:00
2014-03-24 12:41:45 +00:00
if (adjustY)
{
sprite.y -= sprite.height;
}
group.add(sprite);
for (var property in this.objects[name][i].properties)
{
group.set(sprite, property, this.objects[name][i].properties[property], false, false, 0);
}
}
}
},
2014-02-05 06:29:17 +00:00
/**
* Creates a new TilemapLayer object. By default TilemapLayers are fixed to the camera.
* The `layer` parameter is important. If you've created your map in Tiled then you can get this by looking in Tiled and looking at the Layer name.
* Or you can open the JSON file it exports and look at the layers[].name value. Either way it must match.
2014-03-24 12:41:45 +00:00
* If you wish to create a blank layer to put your own tiles on then see Tilemap.createBlankLayer.
2014-02-05 06:29:17 +00:00
*
* @method Phaser.Tilemap#createLayer
2014-02-05 06:29:17 +00:00
* @param {number|string} layer - The layer array index value, or if a string is given the layer name, within the map data that this TilemapLayer represents.
* @param {number} [width] - The rendered width of the layer, should never be wider than Game.width. If not given it will be set to Game.width.
* @param {number} [height] - The rendered height of the layer, should never be wider than Game.height. If not given it will be set to Game.height.
* @param {Phaser.Group} [group] - Optional Group to add the object to. If not specified it will be added to the World group.
* @return {Phaser.TilemapLayer} The TilemapLayer object. This is an extension of Phaser.Sprite and can be moved around the display list accordingly.
*/
createLayer: function (layer, width, height, group) {
// Add Buffer support for the left of the canvas
if (typeof width === 'undefined') { width = this.game.width; }
if (typeof height === 'undefined') { height = this.game.height; }
if (typeof group === 'undefined') { group = this.game.world; }
var index = layer;
if (typeof layer === 'string')
{
index = this.getLayerIndex(layer);
}
if (index === null || index > this.layers.length)
{
console.warn('Tilemap.createLayer: Invalid layer ID given: ' + index);
return;
}
return group.add(new Phaser.TilemapLayer(this.game, this, index, width, height));
},
2014-03-24 12:41:45 +00:00
/**
* Creates a new and empty layer on this Tilemap. By default TilemapLayers are fixed to the camera.
*
* @method Phaser.Tilemap#createLayer
* @param {string} name - The name of this layer. Must be unique within the map.
* @param {number} width - The width of the layer in tiles.
* @param {number} height - The height of the layer in tiles.
* @param {number} tileWidth - The width of the tiles the layer uses for calculations.
* @param {number} tileHeight - The height of the tiles the layer uses for calculations.
* @param {Phaser.Group} [group] - Optional Group to add the layer to. If not specified it will be added to the World group.
* @return {Phaser.TilemapLayer} The TilemapLayer object. This is an extension of Phaser.Image and can be moved around the display list accordingly.
*/
createBlankLayer: function (name, width, height, tileWidth, tileHeight, group) {
if (typeof group === 'undefined') { group = this.game.world; }
if (this.getLayerIndex(name) !== null)
{
console.warn('Tilemap.createBlankLayer: Layer with matching name already exists');
return;
}
var row;
var output = [];
for (var y = 0; y &lt; height; y++)
{
row = [];
for (var x = 0; x &lt; width; x++)
{
row.push(null);
}
output.push(row);
}
var layer = {
name: name,
x: 0,
y: 0,
width: width,
height: height,
widthInPixels: width * tileWidth,
heightInPixels: height * tileHeight,
alpha: 1,
visible: true,
properties: {},
indexes: [],
callbacks: [],
bodies: [],
data: output
};
this.layers.push(layer);
this.currentLayer = this.layers.length - 1;
var w = layer.widthInPixels;
var h = layer.heightInPixels;
if (w > this.game.width)
{
w = this.game.width;
}
if (h > this.game.height)
{
h = this.game.height;
}
var output = new Phaser.TilemapLayer(this.game, this, this.layers.length - 1, w, h);
output.name = name;
return group.add(output);
},
2014-02-05 06:29:17 +00:00
/**
* Gets the layer index based on the layers name.
*
* @method Phaser.Tilemap#getIndex
2014-02-05 06:29:17 +00:00
* @protected
* @param {array} location - The local array to search.
* @param {string} name - The name of the array element to get.
* @return {number} The index of the element in the array, or null if not found.
*/
getIndex: function (location, name) {
for (var i = 0; i &lt; location.length; i++)
{
if (location[i].name === name)
{
return i;
}
}
return null;
},
/**
* Gets the layer index based on its name.
*
* @method Phaser.Tilemap#getLayerIndex
2014-02-05 06:29:17 +00:00
* @param {string} name - The name of the layer to get.
* @return {number} The index of the layer in this tilemap, or null if not found.
*/
getLayerIndex: function (name) {
return this.getIndex(this.layers, name);
},
/**
* Gets the tileset index based on its name.
*
* @method Phaser.Tilemap#getTilesetIndex
2014-02-05 06:29:17 +00:00
* @param {string} name - The name of the tileset to get.
* @return {number} The index of the tileset in this tilemap, or null if not found.
*/
getTilesetIndex: function (name) {
return this.getIndex(this.tilesets, name);
},
/**
* Gets the image index based on its name.
*
* @method Phaser.Tilemap#getImageIndex
2014-02-05 06:29:17 +00:00
* @param {string} name - The name of the image to get.
* @return {number} The index of the image in this tilemap, or null if not found.
*/
getImageIndex: function (name) {
return this.getIndex(this.images, name);
},
/**
* Gets the object index based on its name.
*
* @method Phaser.Tilemap#getObjectIndex
2014-02-05 06:29:17 +00:00
* @param {string} name - The name of the object to get.
* @return {number} The index of the object in this tilemap, or null if not found.
*/
getObjectIndex: function (name) {
return this.getIndex(this.objects, name);
},
/**
* Sets a global collision callback for the given tile index within the layer. This will affect all tiles on this layer that have the same index.
* If a callback is already set for the tile index it will be replaced. Set the callback to null to remove it.
* If you want to set a callback for a tile at a specific location on the map then see setTileLocationCallback.
*
* @method Phaser.Tilemap#setTileIndexCallback
2014-02-05 06:29:17 +00:00
* @param {number|array} indexes - Either a single tile index, or an array of tile indexes to have a collision callback set for.
* @param {function} callback - The callback that will be invoked when the tile is collided with.
* @param {object} callbackContext - The context under which the callback is called.
* @param {number|string|Phaser.TilemapLayer} [layer] - The layer to operate on. If not given will default to this.currentLayer.
*/
setTileIndexCallback: function (indexes, callback, callbackContext, layer) {
layer = this.getLayer(layer);
if (typeof indexes === 'number')
{
// This may seem a bit wasteful, because it will cause empty array elements to be created, but the look-up cost is much
2014-03-24 12:41:45 +00:00
// less than having to iterate through the callbacks array hunting down tile indexes each frame, so I'll take the small memory hit.
2014-02-05 06:29:17 +00:00
this.layers[layer].callbacks[indexes] = { callback: callback, callbackContext: callbackContext };
}
else
{
for (var i = 0, len = indexes.length; i &lt; len; i++)
{
this.layers[layer].callbacks[indexes[i]] = { callback: callback, callbackContext: callbackContext };
}
}
},
/**
* Sets a global collision callback for the given map location within the layer. This will affect all tiles on this layer found in the given area.
2014-02-05 06:29:17 +00:00
* If a callback is already set for the tile index it will be replaced. Set the callback to null to remove it.
* If you want to set a callback for a tile at a specific location on the map then see setTileLocationCallback.
*
* @method Phaser.Tilemap#setTileLocationCallback
2014-02-05 06:29:17 +00:00
* @param {number} x - X position of the top left of the area to copy (given in tiles, not pixels)
* @param {number} y - Y position of the top left of the area to copy (given in tiles, not pixels)
* @param {number} width - The width of the area to copy (given in tiles, not pixels)
* @param {number} height - The height of the area to copy (given in tiles, not pixels)
* @param {function} callback - The callback that will be invoked when the tile is collided with.
* @param {object} callbackContext - The context under which the callback is called.
* @param {number|string|Phaser.TilemapLayer} [layer] - The layer to operate on. If not given will default to this.currentLayer.
*/
setTileLocationCallback: function (x, y, width, height, callback, callbackContext, layer) {
layer = this.getLayer(layer);
this.copy(x, y, width, height, layer);
if (this._results.length &lt; 2)
{
return;
}
for (var i = 1; i &lt; this._results.length; i++)
{
this._results[i].setCollisionCallback(callback, callbackContext);
}
},
/**
* Sets collision the given tile or tiles. You can pass in either a single numeric index or an array of indexes: [ 2, 3, 15, 20].
* The `collides` parameter controls if collision will be enabled (true) or disabled (false).
*
* @method Phaser.Tilemap#setCollision
2014-02-05 06:29:17 +00:00
* @param {number|array} indexes - Either a single tile index, or an array of tile IDs to be checked for collision.
* @param {boolean} [collides=true] - If true it will enable collision. If false it will clear collision.
* @param {number|string|Phaser.TilemapLayer} [layer] - The layer to operate on. If not given will default to this.currentLayer.
*/
setCollision: function (indexes, collides, layer) {
if (typeof collides === 'undefined') { collides = true; }
layer = this.getLayer(layer);
if (typeof indexes === 'number')
{
return this.setCollisionByIndex(indexes, collides, layer, true);
}
else
{
// Collide all of the IDs given in the indexes array
for (var i = 0, len = indexes.length; i &lt; len; i++)
{
this.setCollisionByIndex(indexes[i], collides, layer, false);
}
// Now re-calculate interesting faces
this.calculateFaces(layer);
}
2013-11-28 15:57:09 +00:00
},
/**
2014-02-05 06:29:17 +00:00
* Sets collision on a range of tiles where the tile IDs increment sequentially.
* Calling this with a start value of 10 and a stop value of 14 would set collision for tiles 10, 11, 12, 13 and 14.
* The `collides` parameter controls if collision will be enabled (true) or disabled (false).
2013-11-28 15:57:09 +00:00
*
* @method Phaser.Tilemap#setCollisionBetween
2014-02-05 06:29:17 +00:00
* @param {number} start - The first index of the tile to be set for collision.
* @param {number} stop - The last index of the tile to be set for collision.
* @param {boolean} [collides=true] - If true it will enable collision. If false it will clear collision.
* @param {number|string|Phaser.TilemapLayer} [layer] - The layer to operate on. If not given will default to this.currentLayer.
2013-11-28 15:57:09 +00:00
*/
2014-02-05 06:29:17 +00:00
setCollisionBetween: function (start, stop, collides, layer) {
if (typeof collides === 'undefined') { collides = true; }
layer = this.getLayer(layer);
2013-11-28 15:57:09 +00:00
2014-02-05 06:29:17 +00:00
if (start > stop)
2013-11-28 15:57:09 +00:00
{
2014-02-05 06:29:17 +00:00
return;
}
2013-11-28 15:57:09 +00:00
2014-02-05 06:29:17 +00:00
for (var index = start; index &lt;= stop; index++)
{
this.setCollisionByIndex(index, collides, layer, false);
}
// Now re-calculate interesting faces
this.calculateFaces(layer);
},
/**
* Sets collision on all tiles in the given layer, except for the IDs of those in the given array.
* The `collides` parameter controls if collision will be enabled (true) or disabled (false).
*
* @method Phaser.Tilemap#setCollisionByExclusion
2014-02-05 06:29:17 +00:00
* @param {array} indexes - An array of the tile IDs to not be counted for collision.
* @param {boolean} [collides=true] - If true it will enable collision. If false it will clear collision.
* @param {number|string|Phaser.TilemapLayer} [layer] - The layer to operate on. If not given will default to this.currentLayer.
*/
setCollisionByExclusion: function (indexes, collides, layer) {
if (typeof collides === 'undefined') { collides = true; }
layer = this.getLayer(layer);
// Collide everything, except the IDs given in the indexes array
for (var i = 0, len = this.tiles.length; i &lt; len; i++)
{
if (indexes.indexOf(i) === -1)
2013-11-28 15:57:09 +00:00
{
2014-02-05 06:29:17 +00:00
this.setCollisionByIndex(i, collides, layer, false);
}
}
// Now re-calculate interesting faces
this.calculateFaces(layer);
},
/**
* Sets collision values on a tile in the set.
* You shouldn't usually call this method directly, instead use setCollision, setCollisionBetween or setCollisionByExclusion.
*
* @method Phaser.Tilemap#setCollisionByIndex
2014-02-05 06:29:17 +00:00
* @protected
* @param {number} index - The index of the tile on the layer.
* @param {boolean} [collides=true] - If true it will enable collision on the tile. If false it will clear collision values from the tile.
* @param {number} [layer] - The layer to operate on. If not given will default to this.currentLayer.
* @param {boolean} [recalculate=true] - Recalculates the tile faces after the update.
*/
setCollisionByIndex: function (index, collides, layer, recalculate) {
if (typeof collides === 'undefined') { collides = true; }
if (typeof layer === 'undefined') { layer = this.currentLayer; }
if (typeof recalculate === 'undefined') { recalculate = true; }
if (collides)
{
this.collideIndexes.push(index);
}
else
{
var i = this.collideIndexes.indexOf(index);
if (i > -1)
{
this.collideIndexes.splice(i, 1);
}
}
for (var y = 0; y &lt; this.layers[layer].height; y++)
2014-02-05 06:29:17 +00:00
{
for (var x = 0; x &lt; this.layers[layer].width; x++)
{
var tile = this.layers[layer].data[y][x];
if (tile && tile.index === index)
2013-11-28 15:57:09 +00:00
{
if (collides)
{
tile.setCollision(true, true, true, true);
}
else
{
tile.resetCollision();
}
2014-02-05 06:29:17 +00:00
tile.faceTop = collides;
tile.faceBottom = collides;
tile.faceLeft = collides;
tile.faceRight = collides;
}
}
}
if (recalculate)
{
// Now re-calculate interesting faces
this.calculateFaces(layer);
}
return layer;
},
/**
* Gets the TilemapLayer index as used in the setCollision calls.
*
* @method Phaser.Tilemap#getLayer
2014-02-05 06:29:17 +00:00
* @protected
* @param {number|string|Phaser.TilemapLayer} layer - The layer to operate on. If not given will default to this.currentLayer.
* @return {number} The TilemapLayer index.
*/
getLayer: function (layer) {
2013-11-28 15:57:09 +00:00
2014-02-05 06:29:17 +00:00
if (typeof layer === 'undefined')
{
layer = this.currentLayer;
}
// else if (typeof layer === 'number')
// {
// layer = layer;
// }
else if (typeof layer === 'string')
{
layer = this.getLayerIndex(layer);
}
else if (layer instanceof Phaser.TilemapLayer)
{
layer = layer.index;
}
return layer;
},
/**
* Internal function.
*
* @method Phaser.Tilemap#calculateFaces
2014-02-05 06:29:17 +00:00
* @protected
* @param {number} layer - The index of the TilemapLayer to operate on.
*/
calculateFaces: function (layer) {
var above = null;
var below = null;
var left = null;
var right = null;
for (var y = 0, h = this.layers[layer].height; y &lt; h; y++)
{
for (var x = 0, w = this.layers[layer].width; x &lt; w; x++)
{
var tile = this.layers[layer].data[y][x];
if (tile)
{
above = this.getTileAbove(layer, x, y);
below = this.getTileBelow(layer, x, y);
left = this.getTileLeft(layer, x, y);
right = this.getTileRight(layer, x, y);
if (tile.collides)
{
tile.faceTop = true;
tile.faceBottom = true;
tile.faceLeft = true;
tile.faceRight = true;
}
2014-02-05 06:29:17 +00:00
if (above && above.collides)
{
// There is a tile above this one that also collides, so the top of this tile is no longer interesting
tile.faceTop = false;
}
if (below && below.collides)
{
// There is a tile below this one that also collides, so the bottom of this tile is no longer interesting
tile.faceBottom = false;
}
if (left && left.collides)
2013-11-28 15:57:09 +00:00
{
2014-02-05 06:29:17 +00:00
// There is a tile left this one that also collides, so the left of this tile is no longer interesting
tile.faceLeft = false;
}
if (right && right.collides)
{
// There is a tile right this one that also collides, so the right of this tile is no longer interesting
tile.faceRight = false;
2013-11-28 15:57:09 +00:00
}
}
}
}
},
2014-02-05 06:29:17 +00:00
/**
* Gets the tile above the tile coordinates given.
* Mostly used as an internal function by calculateFaces.
*
* @method Phaser.Tilemap#getTileAbove
2014-02-05 06:29:17 +00:00
* @param {number} layer - The local layer index to get the tile from. Can be determined by Tilemap.getLayer().
* @param {number} x - The x coordinate to get the tile from. In tiles, not pixels.
* @param {number} y - The y coordinate to get the tile from. In tiles, not pixels.
*/
getTileAbove: function (layer, x, y) {
if (y > 0)
{
return this.layers[layer].data[y - 1][x];
}
return null;
},
/**
* Gets the tile below the tile coordinates given.
* Mostly used as an internal function by calculateFaces.
*
* @method Phaser.Tilemap#getTileBelow
2014-02-05 06:29:17 +00:00
* @param {number} layer - The local layer index to get the tile from. Can be determined by Tilemap.getLayer().
* @param {number} x - The x coordinate to get the tile from. In tiles, not pixels.
* @param {number} y - The y coordinate to get the tile from. In tiles, not pixels.
*/
getTileBelow: function (layer, x, y) {
if (y &lt; this.layers[layer].height - 1)
{
return this.layers[layer].data[y + 1][x];
}
return null;
},
/**
* Gets the tile to the left of the tile coordinates given.
* Mostly used as an internal function by calculateFaces.
*
* @method Phaser.Tilemap#getTileLeft
2014-02-05 06:29:17 +00:00
* @param {number} layer - The local layer index to get the tile from. Can be determined by Tilemap.getLayer().
* @param {number} x - The x coordinate to get the tile from. In tiles, not pixels.
* @param {number} y - The y coordinate to get the tile from. In tiles, not pixels.
*/
getTileLeft: function (layer, x, y) {
if (x > 0)
{
return this.layers[layer].data[y][x - 1];
}
return null;
},
/**
* Gets the tile to the right of the tile coordinates given.
* Mostly used as an internal function by calculateFaces.
*
* @method Phaser.Tilemap#getTileRight
2014-02-05 06:29:17 +00:00
* @param {number} layer - The local layer index to get the tile from. Can be determined by Tilemap.getLayer().
* @param {number} x - The x coordinate to get the tile from. In tiles, not pixels.
* @param {number} y - The y coordinate to get the tile from. In tiles, not pixels.
*/
getTileRight: function (layer, x, y) {
if (x &lt; this.layers[layer].width - 1)
{
return this.layers[layer].data[y][x + 1];
}
return null;
},
2013-11-28 15:57:09 +00:00
/**
* Sets the current layer to the given index.
*
* @method Phaser.Tilemap#setLayer
2014-02-05 06:29:17 +00:00
* @param {number|string|Phaser.TilemapLayer} layer - The layer to set as current.
2013-11-28 15:57:09 +00:00
*/
setLayer: function (layer) {
2014-02-05 06:29:17 +00:00
layer = this.getLayer(layer);
2013-11-28 15:57:09 +00:00
if (this.layers[layer])
{
this.currentLayer = layer;
}
},
/**
* Checks if there is a tile at the given location.
*
* @method Phaser.Tilemap#hasTile
* @param {number} x - X position to check if a tile exists at (given in tile units, not pixels)
* @param {number} y - Y position to check if a tile exists at (given in tile units, not pixels)
* @param {number|string|Phaser.TilemapLayer} layer - The layer to set as current.
* @return {boolean} True if there is a tile at the given location, otherwise false.
*/
hasTile: function (x, y, layer) {
return (this.layers[layer].data[y] !== null && this.layers[layer].data[y][x] !== null);
},
2013-11-28 15:57:09 +00:00
/**
* Puts a tile of the given index value at the coordinate specified.
2014-02-05 06:29:17 +00:00
*
2013-11-28 15:57:09 +00:00
* @method Phaser.Tilemap#putTile
2014-02-05 06:29:17 +00:00
* @param {Phaser.Tile|number} tile - The index of this tile to set or a Phaser.Tile object.
2013-11-28 15:57:09 +00:00
* @param {number} x - X position to place the tile (given in tile units, not pixels)
* @param {number} y - Y position to place the tile (given in tile units, not pixels)
2014-02-05 06:29:17 +00:00
* @param {number|string|Phaser.TilemapLayer} [layer] - The layer to modify.
2014-03-24 12:41:45 +00:00
* @return {Phaser.Tile} The Tile object that was created or added to this map.
2013-11-28 15:57:09 +00:00
*/
2014-02-05 06:29:17 +00:00
putTile: function (tile, x, y, layer) {
2013-11-28 15:57:09 +00:00
2014-02-05 06:29:17 +00:00
layer = this.getLayer(layer);
2013-11-28 15:57:09 +00:00
if (x >= 0 && x &lt; this.layers[layer].width && y >= 0 && y &lt; this.layers[layer].height)
{
var index;
2014-02-05 06:29:17 +00:00
if (tile instanceof Phaser.Tile)
{
index = tile.index;
if (this.hasTile(x, y, layer))
{
this.layers[layer].data[y][x].copy(tile);
}
else
{
this.layers[layer].data[y][x] = new Phaser.Tile(layer, index, x, y, tile.width, tile.height);
}
}
else
{
index = tile;
if (this.hasTile(x, y, layer))
{
this.layers[layer].data[y][x].index = index;
}
else
{
2014-03-24 12:41:45 +00:00
this.layers[layer].data[y][x] = new Phaser.Tile(this.layers[layer], index, x, y, this.tileWidth, this.tileHeight);
}
}
if (this.collideIndexes.indexOf(index) > -1)
{
this.layers[layer].data[y][x].setCollision(true, true, true, true);
2014-02-05 06:29:17 +00:00
}
else
{
this.layers[layer].data[y][x].resetCollision();
2014-02-05 06:29:17 +00:00
}
2014-03-24 12:41:45 +00:00
this.layers[layer].dirty = true;
2014-02-05 06:29:17 +00:00
this.calculateFaces(layer);
2014-03-24 12:41:45 +00:00
return this.layers[layer].data[y][x];
2013-11-28 15:57:09 +00:00
}
2014-03-24 12:41:45 +00:00
return null;
2014-02-05 06:29:17 +00:00
},
/**
* Puts a tile into the Tilemap layer. The coordinates are given in pixel values.
*
* @method Phaser.Tilemap#putTileWorldXY
* @param {Phaser.Tile|number} tile - The index of this tile to set or a Phaser.Tile object.
* @param {number} x - X position to insert the tile (given in pixels)
* @param {number} y - Y position to insert the tile (given in pixels)
* @param {number} tileWidth - The width of the tile in pixels.
* @param {number} tileHeight - The height of the tile in pixels.
* @param {number|string|Phaser.TilemapLayer} [layer] - The layer to modify.
*/
putTileWorldXY: function (tile, x, y, tileWidth, tileHeight, layer) {
layer = this.getLayer(layer);
x = this.game.math.snapToFloor(x, tileWidth) / tileWidth;
y = this.game.math.snapToFloor(y, tileHeight) / tileHeight;
this.putTile(tile, x, y, layer);
2013-11-28 15:57:09 +00:00
},
/**
* Gets a tile from the Tilemap Layer. The coordinates are given in tile values.
2014-02-05 06:29:17 +00:00
*
2013-11-28 15:57:09 +00:00
* @method Phaser.Tilemap#getTile
* @param {number} x - X position to get the tile from (given in tile units, not pixels)
* @param {number} y - Y position to get the tile from (given in tile units, not pixels)
2014-02-05 06:29:17 +00:00
* @param {number|string|Phaser.TilemapLayer} [layer] - The layer to get the tile from.
* @return {Phaser.Tile} The tile at the given coordinates.
2013-11-28 15:57:09 +00:00
*/
getTile: function (x, y, layer) {
2014-02-05 06:29:17 +00:00
layer = this.getLayer(layer);
2013-11-28 15:57:09 +00:00
if (x >= 0 && x &lt; this.layers[layer].width && y >= 0 && y &lt; this.layers[layer].height)
{
return this.layers[layer].data[y][x];
}
},
/**
* Gets a tile from the Tilemap layer. The coordinates are given in pixel values.
2014-02-05 06:29:17 +00:00
*
2013-11-28 15:57:09 +00:00
* @method Phaser.Tilemap#getTileWorldXY
* @param {number} x - X position to get the tile from (given in pixels)
* @param {number} y - Y position to get the tile from (given in pixels)
* @param {number} [tileWidth] - The width of the tiles. If not given the map default is used.
* @param {number} [tileHeight] - The height of the tiles. If not given the map default is used.
2014-02-05 06:29:17 +00:00
* @param {number|string|Phaser.TilemapLayer} [layer] - The layer to get the tile from.
* @return {Phaser.Tile} The tile at the given coordinates.
2013-11-28 15:57:09 +00:00
*/
getTileWorldXY: function (x, y, tileWidth, tileHeight, layer) {
if (typeof tileWidth === 'undefined') { tileWidth = this.tileWidth; }
if (typeof tileHeight === 'undefined') { tileHeight = this.tileHeight; }
2014-02-05 06:29:17 +00:00
layer = this.getLayer(layer);
2013-11-28 15:57:09 +00:00
x = this.game.math.snapToFloor(x, tileWidth) / tileWidth;
y = this.game.math.snapToFloor(y, tileHeight) / tileHeight;
2014-02-05 06:29:17 +00:00
return this.getTile(x, y, layer);
2013-11-28 15:57:09 +00:00
},
/**
* Copies all of the tiles in the given rectangular block into the tilemap data buffer.
2014-02-05 06:29:17 +00:00
*
2013-11-28 15:57:09 +00:00
* @method Phaser.Tilemap#copy
* @param {number} x - X position of the top left of the area to copy (given in tiles, not pixels)
* @param {number} y - Y position of the top left of the area to copy (given in tiles, not pixels)
* @param {number} width - The width of the area to copy (given in tiles, not pixels)
* @param {number} height - The height of the area to copy (given in tiles, not pixels)
2014-02-05 06:29:17 +00:00
* @param {number|string|Phaser.TilemapLayer} [layer] - The layer to copy the tiles from.
2013-11-28 15:57:09 +00:00
* @return {array} An array of the tiles that were copied.
*/
copy: function (x, y, width, height, layer) {
2014-02-05 06:29:17 +00:00
layer = this.getLayer(layer);
2013-11-28 15:57:09 +00:00
if (!this.layers[layer])
{
this._results.length = 0;
return;
}
if (typeof x === "undefined") { x = 0; }
if (typeof y === "undefined") { y = 0; }
if (typeof width === "undefined") { width = this.layers[layer].width; }
if (typeof height === "undefined") { height = this.layers[layer].height; }
if (x &lt; 0)
{
x = 0;
}
if (y &lt; 0)
{
y = 0;
}
if (width > this.layers[layer].width)
{
width = this.layers[layer].width;
}
if (height > this.layers[layer].height)
{
height = this.layers[layer].height;
}
this._results.length = 0;
this._results.push({ x: x, y: y, width: width, height: height, layer: layer });
2013-11-28 15:57:09 +00:00
for (var ty = y; ty &lt; y + height; ty++)
{
for (var tx = x; tx &lt; x + width; tx++)
{
2014-02-05 06:29:17 +00:00
this._results.push(this.layers[layer].data[ty][tx]);
2013-11-28 15:57:09 +00:00
}
}
return this._results;
},
/**
* Pastes a previously copied block of tile data into the given x/y coordinates. Data should have been prepared with Tilemap.copy.
2014-02-05 06:29:17 +00:00
*
2013-11-28 15:57:09 +00:00
* @method Phaser.Tilemap#paste
* @param {number} x - X position of the top left of the area to paste to (given in tiles, not pixels)
* @param {number} y - Y position of the top left of the area to paste to (given in tiles, not pixels)
* @param {array} tileblock - The block of tiles to paste.
2014-02-05 06:29:17 +00:00
* @param {number|string|Phaser.TilemapLayer} [layer] - The layer to paste the tiles into.
2013-11-28 15:57:09 +00:00
*/
paste: function (x, y, tileblock, layer) {
if (typeof x === "undefined") { x = 0; }
if (typeof y === "undefined") { y = 0; }
2014-02-05 06:29:17 +00:00
layer = this.getLayer(layer);
2013-11-28 15:57:09 +00:00
if (!tileblock || tileblock.length &lt; 2)
{
return;
}
// Find out the difference between tileblock[1].x/y and x/y and use it as an offset, as it's the top left of the block to paste
var diffX = tileblock[1].x - x;
var diffY = tileblock[1].y - y;
for (var i = 1; i &lt; tileblock.length; i++)
{
2014-02-05 06:29:17 +00:00
this.layers[layer].data[ diffY + tileblock[i].y ][ diffX + tileblock[i].x ].copy(tileblock[i]);
2013-11-28 15:57:09 +00:00
}
2014-02-05 06:29:17 +00:00
this.layers[layer].dirty = true;
this.calculateFaces(layer);
2013-11-28 15:57:09 +00:00
},
/**
2014-02-05 06:29:17 +00:00
* Scans the given area for tiles with an index matching tileA and swaps them with tileB.
*
* @method Phaser.Tilemap#swap
2013-11-28 15:57:09 +00:00
* @param {number} tileA - First tile index.
* @param {number} tileB - Second tile index.
* @param {number} x - X position of the top left of the area to operate one, given in tiles, not pixels.
* @param {number} y - Y position of the top left of the area to operate one, given in tiles, not pixels.
* @param {number} width - The width in tiles of the area to operate on.
* @param {number} height - The height in tiles of the area to operate on.
2014-02-05 06:29:17 +00:00
* @param {number|string|Phaser.TilemapLayer} [layer] - The layer to operate on.
2013-11-28 15:57:09 +00:00
*/
swap: function (tileA, tileB, x, y, width, height, layer) {
2014-02-05 06:29:17 +00:00
layer = this.getLayer(layer);
2013-11-28 15:57:09 +00:00
this.copy(x, y, width, height, layer);
if (this._results.length &lt; 2)
{
return;
}
this._tempA = tileA;
this._tempB = tileB;
this._results.forEach(this.swapHandler, this);
2014-02-05 06:29:17 +00:00
this.paste(x, y, this._results, layer);
2013-11-28 15:57:09 +00:00
},
/**
* Internal function that handles the swapping of tiles.
2014-02-05 06:29:17 +00:00
*
2013-11-28 15:57:09 +00:00
* @method Phaser.Tilemap#swapHandler
2014-02-05 06:29:17 +00:00
* @private
2013-11-28 15:57:09 +00:00
* @param {number} value
* @param {number} index
*/
swapHandler: function (value, index) {
if (value.index === this._tempA)
{
this._results[index].index = this._tempB;
}
if (value.index === this._tempB)
2013-11-28 15:57:09 +00:00
{
this._results[index].index = this._tempA;
}
},
/**
2014-02-05 06:29:17 +00:00
* For each tile in the given area defined by x/y and width/height run the given callback.
*
2013-11-28 15:57:09 +00:00
* @method Phaser.Tilemap#forEach
* @param {number} callback - The callback. Each tile in the given area will be passed to this callback as the first and only parameter.
* @param {number} context - The context under which the callback should be run.
* @param {number} x - X position of the top left of the area to operate one, given in tiles, not pixels.
* @param {number} y - Y position of the top left of the area to operate one, given in tiles, not pixels.
* @param {number} width - The width in tiles of the area to operate on.
* @param {number} height - The height in tiles of the area to operate on.
2014-02-05 06:29:17 +00:00
* @param {number|string|Phaser.TilemapLayer} [layer] - The layer to operate on.
2013-11-28 15:57:09 +00:00
*/
forEach: function (callback, context, x, y, width, height, layer) {
2014-02-05 06:29:17 +00:00
layer = this.getLayer(layer);
2013-11-28 15:57:09 +00:00
this.copy(x, y, width, height, layer);
if (this._results.length &lt; 2)
{
return;
}
this._results.forEach(callback, context);
2014-02-05 06:29:17 +00:00
this.paste(x, y, this._results, layer);
2013-11-28 15:57:09 +00:00
},
/**
2014-02-05 06:29:17 +00:00
* Scans the given area for tiles with an index matching `source` and updates their index to match `dest`.
*
2013-11-28 15:57:09 +00:00
* @method Phaser.Tilemap#replace
2014-02-05 06:29:17 +00:00
* @param {number} source - The tile index value to scan for.
* @param {number} dest - The tile index value to replace found tiles with.
2013-11-28 15:57:09 +00:00
* @param {number} x - X position of the top left of the area to operate one, given in tiles, not pixels.
* @param {number} y - Y position of the top left of the area to operate one, given in tiles, not pixels.
* @param {number} width - The width in tiles of the area to operate on.
* @param {number} height - The height in tiles of the area to operate on.
2014-02-05 06:29:17 +00:00
* @param {number|string|Phaser.TilemapLayer} [layer] - The layer to operate on.
2013-11-28 15:57:09 +00:00
*/
2014-02-05 06:29:17 +00:00
replace: function (source, dest, x, y, width, height, layer) {
layer = this.getLayer(layer);
2013-11-28 15:57:09 +00:00
this.copy(x, y, width, height, layer);
if (this._results.length &lt; 2)
{
return;
}
for (var i = 1; i &lt; this._results.length; i++)
{
2014-02-05 06:29:17 +00:00
if (this._results[i].index === source)
2013-11-28 15:57:09 +00:00
{
2014-02-05 06:29:17 +00:00
this._results[i].index = dest;
2013-11-28 15:57:09 +00:00
}
}
2014-02-05 06:29:17 +00:00
this.paste(x, y, this._results, layer);
2013-11-28 15:57:09 +00:00
},
/**
* Randomises a set of tiles in a given area.
2014-02-05 06:29:17 +00:00
*
2013-11-28 15:57:09 +00:00
* @method Phaser.Tilemap#random
* @param {number} x - X position of the top left of the area to operate one, given in tiles, not pixels.
* @param {number} y - Y position of the top left of the area to operate one, given in tiles, not pixels.
* @param {number} width - The width in tiles of the area to operate on.
* @param {number} height - The height in tiles of the area to operate on.
2014-02-05 06:29:17 +00:00
* @param {number|string|Phaser.TilemapLayer} [layer] - The layer to operate on.
2013-11-28 15:57:09 +00:00
*/
random: function (x, y, width, height, layer) {
2014-02-05 06:29:17 +00:00
layer = this.getLayer(layer);
2013-11-28 15:57:09 +00:00
this.copy(x, y, width, height, layer);
if (this._results.length &lt; 2)
{
return;
}
var indexes = [];
for (var t = 1; t &lt; this._results.length; t++)
{
2014-02-05 06:29:17 +00:00
if (this._results[t].index)
2013-11-28 15:57:09 +00:00
{
2014-02-05 06:29:17 +00:00
var idx = this._results[t].index;
if (indexes.indexOf(idx) === -1)
{
indexes.push(idx);
}
2013-11-28 15:57:09 +00:00
}
}
for (var i = 1; i &lt; this._results.length; i++)
{
this._results[i].index = this.game.rnd.pick(indexes);
}
2014-02-05 06:29:17 +00:00
this.paste(x, y, this._results, layer);
2013-11-28 15:57:09 +00:00
},
/**
* Shuffles a set of tiles in a given area. It will only randomise the tiles in that area, so if they're all the same nothing will appear to have changed!
2014-02-05 06:29:17 +00:00
*
2013-11-28 15:57:09 +00:00
* @method Phaser.Tilemap#shuffle
* @param {number} x - X position of the top left of the area to operate one, given in tiles, not pixels.
* @param {number} y - Y position of the top left of the area to operate one, given in tiles, not pixels.
* @param {number} width - The width in tiles of the area to operate on.
* @param {number} height - The height in tiles of the area to operate on.
2014-02-05 06:29:17 +00:00
* @param {number|string|Phaser.TilemapLayer} [layer] - The layer to operate on.
2013-11-28 15:57:09 +00:00
*/
shuffle: function (x, y, width, height, layer) {
2014-02-05 06:29:17 +00:00
layer = this.getLayer(layer);
2013-11-28 15:57:09 +00:00
this.copy(x, y, width, height, layer);
if (this._results.length &lt; 2)
{
return;
}
2014-02-05 06:29:17 +00:00
var indexes = [];
for (var t = 1; t &lt; this._results.length; t++)
{
if (this._results[t].index)
{
indexes.push(this._results[t].index);
}
}
2013-11-28 15:57:09 +00:00
2014-02-05 06:29:17 +00:00
Phaser.Utils.shuffle(indexes);
2013-11-28 15:57:09 +00:00
2014-02-05 06:29:17 +00:00
for (var i = 1; i &lt; this._results.length; i++)
{
this._results[i].index = indexes[i - 1];
}
2013-11-28 15:57:09 +00:00
2014-02-05 06:29:17 +00:00
this.paste(x, y, this._results, layer);
2013-11-28 15:57:09 +00:00
},
/**
2014-02-05 06:29:17 +00:00
* Fills the given area with the specified tile.
*
2013-11-28 15:57:09 +00:00
* @method Phaser.Tilemap#fill
2014-02-05 06:29:17 +00:00
* @param {number} index - The index of the tile that the area will be filled with.
2013-11-28 15:57:09 +00:00
* @param {number} x - X position of the top left of the area to operate one, given in tiles, not pixels.
* @param {number} y - Y position of the top left of the area to operate one, given in tiles, not pixels.
* @param {number} width - The width in tiles of the area to operate on.
* @param {number} height - The height in tiles of the area to operate on.
2014-02-05 06:29:17 +00:00
* @param {number|string|Phaser.TilemapLayer} [layer] - The layer to operate on.
2013-11-28 15:57:09 +00:00
*/
fill: function (index, x, y, width, height, layer) {
2014-02-05 06:29:17 +00:00
layer = this.getLayer(layer);
2013-11-28 15:57:09 +00:00
this.copy(x, y, width, height, layer);
if (this._results.length &lt; 2)
{
return;
}
for (var i = 1; i &lt; this._results.length; i++)
{
this._results[i].index = index;
}
2014-02-05 06:29:17 +00:00
this.paste(x, y, this._results, layer);
2013-11-28 15:57:09 +00:00
},
/**
* Removes all layers from this tile map.
2014-02-05 06:29:17 +00:00
*
2013-11-28 15:57:09 +00:00
* @method Phaser.Tilemap#removeAllLayers
*/
removeAllLayers: function () {
this.layers.length = 0;
this.currentLayer = 0;
},
/**
* Dumps the tilemap data out to the console.
2014-02-05 06:29:17 +00:00
*
2013-11-28 15:57:09 +00:00
* @method Phaser.Tilemap#dump
*/
dump: function () {
var txt = '';
var args = [''];
for (var y = 0; y &lt; this.layers[this.currentLayer].height; y++)
{
for (var x = 0; x &lt; this.layers[this.currentLayer].width; x++)
{
txt += "%c ";
if (this.layers[this.currentLayer].data[y][x] > 1)
{
if (this.debugMap[this.layers[this.currentLayer].data[y][x]])
{
args.push("background: " + this.debugMap[this.layers[this.currentLayer].data[y][x]]);
}
else
{
args.push("background: #ffffff");
}
}
else
{
args.push("background: rgb(0, 0, 0)");
}
}
txt += "\n";
}
args[0] = txt;
console.log.apply(console, args);
},
/**
* Removes all layers from this tile map and nulls the game reference.
2014-02-05 06:29:17 +00:00
*
2013-11-28 15:57:09 +00:00
* @method Phaser.Tilemap#destroy
*/
destroy: function () {
this.removeAllLayers();
2014-02-05 06:29:17 +00:00
this.data = [];
2013-11-28 15:57:09 +00:00
this.game = null;
}
};
2014-02-05 06:29:17 +00:00
Phaser.Tilemap.prototype.constructor = Phaser.Tilemap;
2013-11-28 15:57:09 +00:00
</pre>
</article>
</section>
</div>
<div class="clearfix"></div>
<footer>
<span class="copyright">
2014-02-05 06:29:17 +00:00
Phaser Copyright © 2012-2014 Photon Storm Ltd.
2013-11-28 15:57:09 +00:00
</span>
<br />
<span class="jsdoc-message">
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.0-dev</a>
on Fri Mar 28 2014 01:41:23 GMT-0000 (GMT) using the <a href="https://github.com/terryweiss/docstrap">DocStrap template</a>.
2013-11-28 15:57:09 +00:00
</span>
</footer>
</div>
<br clear="both">
</div>
</div>
<script src="scripts/sunlight.js"></script>
<script src="scripts/sunlight.javascript.js"></script>
<script src="scripts/sunlight-plugin.doclinks.js"></script>
<script src="scripts/sunlight-plugin.linenumbers.js"></script>
<script src="scripts/sunlight-plugin.menu.js"></script>
<script src="scripts/jquery.min.js"></script>
<script src="scripts/jquery.scrollTo.js"></script>
<script src="scripts/jquery.localScroll.js"></script>
<script src="scripts/bootstrap-dropdown.js"></script>
<script src="scripts/toc.js"></script>
<script> Sunlight.highlightAll({lineNumbers:true, showMenu: true, enableDoclinks :true}); </script>
<script>
$( function () {
$( "#toc" ).toc( {
anchorName : function(i, heading, prefix) {
return $(heading).attr("id") || ( prefix + i );
},
2013-11-28 15:57:09 +00:00
selectors : "h1,h2,h3,h4",
showAndHide : false,
scrollTo : 60
} );
$( "#toc>ul" ).addClass( "nav nav-pills nav-stacked" );
$( "#main span[id^='toc']" ).addClass( "toc-shim" );
} );
</script>
</body>
</html>