Skip to content

Commit bf72b4d

Browse files
committed
Tilemap.createFromObjects can now turn a bunch of Tiled objects into Sprites in one single call, and copies across all properties as well.
1 parent 63d90a0 commit bf72b4d

7 files changed

Lines changed: 102 additions & 15 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ Significant API changes:
4848

4949
* Loader.tileset has a new method signature. Please use the new format: load.tileset(key, url, tileWidth, tileHeight, tileMargin, tileSpacing, rows, columns, total).
5050
* TilemapLayers are now created via the Tilemap object itself: map.createLayer(x, y, width, height, tileset, layer, group) and no longer via the GameObjectFactory.
51+
* Tilemap.createFromObjects can now turn a bunch of Tiled objects into Sprites in one single call, and copies across all properties as well.
5152

5253

5354
New features:
@@ -60,6 +61,7 @@ New features:
6061
* TilemapLayers now have debug and debugAlpha values, this turns on the drawing of the collision edges (very handy for debugging, as the name implies!)
6162
* Tweens have a new event: onLoop.
6263
* You can now load any binary file via the Loader: game.load.binary(key, url, callback) - the optional callback allows for post-load processing before entering the Cache.
64+
* Group.set will let you deep set a new propery on a single child of the Group.
6365

6466

6567
New Examples:

examples/assets/maps/newtest.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,13 @@
8484
"name":"",
8585
"properties":
8686
{
87-
87+
"alpha":"0.5"
8888
},
8989
"type":"",
9090
"visible":true,
9191
"width":0,
92-
"x":454,
93-
"y":307
92+
"x":443,
93+
"y":244
9494
},
9595
{
9696
"gid":34,
@@ -112,7 +112,7 @@
112112
"name":"",
113113
"properties":
114114
{
115-
115+
"alpha":"0.5"
116116
},
117117
"type":"",
118118
"visible":true,
@@ -140,7 +140,7 @@
140140
"name":"",
141141
"properties":
142142
{
143-
143+
"body.gravity.y":"5"
144144
},
145145
"type":"",
146146
"visible":true,

examples/assets/maps/newtest.tmx

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,23 @@
4949
<object name="sun" type="collision" x="793" y="340" width="77" height="70">
5050
<ellipse/>
5151
</object>
52-
<object gid="34" x="454" y="307"/>
52+
<object gid="34" x="443" y="244">
53+
<properties>
54+
<property name="alpha" value="0.5"/>
55+
</properties>
56+
</object>
5357
<object gid="34" x="483" y="267"/>
54-
<object gid="34" x="513" y="237"/>
58+
<object gid="34" x="513" y="237">
59+
<properties>
60+
<property name="alpha" value="0.5"/>
61+
</properties>
62+
</object>
5563
<object gid="34" x="549" y="229"/>
56-
<object gid="34" x="580" y="249"/>
64+
<object gid="34" x="580" y="249">
65+
<properties>
66+
<property name="body.gravity.y" value="5"/>
67+
</properties>
68+
</object>
5769
<object gid="34" x="610" y="278"/>
5870
<object gid="34" x="639" y="309"/>
5971
<object gid="34" x="662" y="342"/>

examples/wip/tilemap.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@ function preload() {
2020
// game.load.image('phaser', 'assets/sprites/mushroom2.png');
2121
// game.load.image('phaser', 'assets/sprites/wabbit.png');
2222
game.load.image('phaser', 'assets/sprites/arrow.png');
23+
game.load.spritesheet('coin', 'assets/sprites/coin.png', 32, 32);
2324
// game.load.image('phaser', 'assets/sprites/darkwing_crazy.png');
2425

2526
}
2627

2728
var cursors;
2829
var map;
30+
var coins;
2931

3032
var layer;
3133
var layer2;
@@ -57,13 +59,13 @@ function create() {
5759
// layer2.alpha = 0.5;
5860

5961
layer = map.createLayer('Tile Layer 1');
60-
layer.scrollFactorX = 0.5;
6162

6263
// layer.debug = true;
6364

6465
layer.resizeWorld();
6566

66-
67+
//coins =
68+
map.createFromObjects(34, 'coin', 0)
6769

6870
// layer2 = game.add.tilemapLayer(0, 0, 400, 600, null, map, 0);
6971
// layer.cameraOffset.x = 400;

src/core/Group.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,11 @@ Phaser.Group.prototype = {
335335

336336
},
337337

338+
/**
339+
* Internal test.
340+
*
341+
* @method Phaser.Group#childTest
342+
*/
338343
childTest: function (prefix, child) {
339344

340345
var s = prefix + ' next: ';
@@ -363,6 +368,11 @@ Phaser.Group.prototype = {
363368

364369
},
365370

371+
/**
372+
* Internal test.
373+
*
374+
* @method Phaser.Group#swapIndex
375+
*/
366376
swapIndex: function (index1, index2) {
367377

368378
var child1 = this.getAt(index1);
@@ -634,6 +644,32 @@ Phaser.Group.prototype = {
634644

635645
},
636646

647+
/**
648+
* This function allows you to quickly set a property on a single child of this Group to a new value.
649+
* The operation parameter controls how the new value is assigned to the property, from simple replacement to addition and multiplication.
650+
*
651+
* @method Phaser.Group#set
652+
* @param {Phaser.Sprite} child - The child to set the property on.
653+
* @param {string} key - The property, as a string, to be set. For example: 'body.velocity.x'
654+
* @param {*} value - The value that will be set.
655+
* @param {boolean} [checkAlive=false] - If set then the child will only be updated if alive=true.
656+
* @param {boolean} [checkVisible=false] - If set then the child will only be updated if visible=true.
657+
* @param {number} [operation=0] - Controls how the value is assigned. A value of 0 replaces the value with the new one. A value of 1 adds it, 2 subtracts it, 3 multiplies it and 4 divides it.
658+
*/
659+
set: function (child, key, value, checkAlive, checkVisible, operation) {
660+
661+
key = key.split('.');
662+
663+
if (typeof checkAlive === 'undefined') { checkAlive = false; }
664+
if (typeof checkVisible === 'undefined') { checkVisible = false; }
665+
666+
if ((checkAlive === false || (checkAlive && child.alive)) && (checkVisible === false || (checkVisible && child.visible)))
667+
{
668+
this.setProperty(child, key, value, operation);
669+
}
670+
671+
},
672+
637673
/**
638674
* This function allows you to quickly set the same property across all children of this Group to a new value.
639675
* The operation parameter controls how the new value is assigned to the property, from simple replacement to addition and multiplication.

src/tilemap/Tilemap.js

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,16 +184,51 @@ Phaser.Tilemap.prototype = {
184184
},
185185

186186
// Region? Remove tile from map data?
187-
createSpritesFromTiles: function (layer, tileIndex, key, frame, group) {
187+
createFromTiles: function (layer, tileIndex, key, frame, group) {
188188

189189
if (typeof group === 'undefined') { group = this.game.world; }
190190

191191
},
192192

193-
createGroup: function (layer, parent) {
193+
/**
194+
* 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
195+
* 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
196+
* 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
197+
* Sprite is created. You could also give it a value like: body.velocity.x: 100 to set it moving automatically.
198+
*
199+
* @method Phaser.Tileset#createFromObjects
200+
* @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.
201+
* @param {string} key - The Game.cache key of the image that this Sprite will use.
202+
* @param {number|string} [frame] - If the Sprite image contains multiple frames you can specify which one to use here.
203+
* @param {boolean} [exists=true] - The default exists state of the Sprite.
204+
* @param {boolean} [autoCull=true] - The default autoCull state of the Sprite. Sprites that are autoCulled are culled from the camera if out of its range.
205+
* @param {Phaser.Group} [group] - Optional Group to add the Sprite to. If not specified it will be added to the World group.
206+
*/
207+
createFromObjects: function (gid, key, frame, exists, autoCull, group) {
208+
209+
if (typeof exists === 'undefined') { exists = true; }
210+
if (typeof autoCull === 'undefined') { autoCull = true; }
211+
if (typeof group === 'undefined') { group = this.game.world; }
212+
213+
var sprite;
214+
215+
for (var i = 0; i < this.objects.length; i++)
216+
{
217+
if (this.objects[i].gid === gid)
218+
{
219+
sprite = group.create(this.objects[i].x, this.objects[i].y, key, frame, exists);
194220

195-
// Creates a Group based on an objectgroup, with one Sprite per gid entry, using the tilemap image + frame number
196-
// Will only work if image is loaded as spritesheet
221+
sprite.anchor.setTo(0, 1);
222+
sprite.name = this.objects[i].name;
223+
sprite.visible = this.objects[i].visible;
224+
sprite.autoCull = autoCull;
225+
226+
for (property in this.objects[i].properties)
227+
{
228+
group.set(sprite, property, this.objects[i].properties[property], false, false, 0);
229+
}
230+
}
231+
}
197232

198233
},
199234

src/tilemap/TilemapLayer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ Phaser.TilemapLayer.prototype.postUpdate = function () {
304304
*/
305305
Phaser.TilemapLayer.prototype.resizeWorld = function () {
306306

307-
this.game.world.setBounds(0, 0, this.layer.widthInPixels * 4, this.layer.heightInPixels);
307+
this.game.world.setBounds(0, 0, this.layer.widthInPixels, this.layer.heightInPixels);
308308

309309
}
310310

0 commit comments

Comments
 (0)