Skip to content

Commit 46d5069

Browse files
committed
Tilemap.createFromObjects has a new parameter: adjustY, which is true by default. Because Tiled uses a bottom-left coordinate system Phaser used to set the Sprite anchor to 0,1 to compensate. If adjustY is true it now reduces the y value by the object height instead.
1 parent 90ef694 commit 46d5069

3 files changed

Lines changed: 12 additions & 14 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ Updated:
7272
* Updated Device.isConsoleOpen as it no longer works in Chrome. Revised code and documentation accordingly (fix #593)
7373
* Removed State.destroy empty method and replaced with State.shutdown, as that is what the StateManager expects (fix #586)
7474
* P2.removeBody will check if the body is part of the world before removing, this avoids a TypeError from the p2 layer.
75-
* Tilemap.createFromObjects used to set the Sprite anchor to 0,1 because Tiled uses a bottom-left coordinate system. It now calculates the offset in TilemapParser.
75+
* Tilemap.createFromObjects has a new parameter: adjustY, which is true by default. Because Tiled uses a bottom-left coordinate system Phaser used to set the Sprite anchor to 0,1 to compensate. If adjustY is true it now reduces the y value by the object height instead.
7676

7777

7878
TODO:

src/tilemap/Tilemap.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -344,25 +344,27 @@ Phaser.Tilemap.prototype = {
344344
/**
345345
* 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
346346
* 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
347-
* configure Sprite properties from within the map editor. For example giving an object a property if alpha: 0.5 in the map editor will duplicate that when the
347+
* 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
348348
* Sprite is created. You could also give it a value like: body.velocity.x: 100 to set it moving automatically.
349349
*
350350
* @method Phaser.Tilemap#createFromObjects
351351
* @param {string} name - The name of the Object Group to create Sprites from.
352-
* @param {number} gid - The layer array index value, or if a string is given the layer name, within the map data that this TilemapLayer represents.
352+
* @param {number} gid - The layer array index value, or if a string is given the layer name within the map data.
353353
* @param {string} key - The Game.cache key of the image that this Sprite will use.
354354
* @param {number|string} [frame] - If the Sprite image contains multiple frames you can specify which one to use here.
355355
* @param {boolean} [exists=true] - The default exists state of the Sprite.
356356
* @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.
357357
* @param {Phaser.Group} [group=Phaser.World] - Group to add the Sprite to. If not specified it will be added to the World group.
358358
* @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.
359+
* @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.
359360
*/
360-
createFromObjects: function (name, gid, key, frame, exists, autoCull, group, CustomClass) {
361+
createFromObjects: function (name, gid, key, frame, exists, autoCull, group, CustomClass, adjustY) {
361362

362363
if (typeof exists === 'undefined') { exists = true; }
363364
if (typeof autoCull === 'undefined') { autoCull = false; }
364365
if (typeof group === 'undefined') { group = this.game.world; }
365366
if (typeof CustomClass === 'undefined') { CustomClass = Phaser.Sprite; }
367+
if (typeof adjustY === 'undefined') { adjustY = true; }
366368

367369
if (!this.objects[name])
368370
{
@@ -383,6 +385,11 @@ Phaser.Tilemap.prototype = {
383385
sprite.autoCull = autoCull;
384386
sprite.exists = exists;
385387

388+
if (adjustY)
389+
{
390+
sprite.y -= sprite.height;
391+
}
392+
386393
group.add(sprite);
387394

388395
for (var property in this.objects[name][i].properties)

src/tilemap/TilemapParser.js

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -312,9 +312,6 @@ Phaser.TilemapParser = {
312312
// Tilesets
313313
var tilesets = [];
314314

315-
// GID height look-up table
316-
var gidHeight = [];
317-
318315
for (var i = 0; i < json.tilesets.length; i++)
319316
{
320317
// name, firstgid, width, height, margin, spacing, properties
@@ -338,12 +335,6 @@ Phaser.TilemapParser = {
338335
{
339336
tilesets.push(newSet);
340337
}
341-
342-
// GID to height look-up
343-
for (var gd = set.firstgid; gd < set.firstgid + newSet.total; gd++)
344-
{
345-
gidHeight[gd] = set.tileheight;
346-
}
347338
}
348339

349340
map.tilesets = tilesets;
@@ -372,7 +363,7 @@ Phaser.TilemapParser = {
372363
gid: json.layers[i].objects[v].gid,
373364
name: json.layers[i].objects[v].name,
374365
x: json.layers[i].objects[v].x,
375-
y: json.layers[i].objects[v].y - gidHeight[json.layers[i].objects[v].gid],
366+
y: json.layers[i].objects[v].y,
376367
visible: json.layers[i].objects[v].visible,
377368
properties: json.layers[i].objects[v].properties
378369

0 commit comments

Comments
 (0)