From bf72b4d3b0e8a58724b6cdc1c2e60d628cf465f8 Mon Sep 17 00:00:00 2001 From: photonstorm Date: Sun, 22 Dec 2013 04:27:12 +0000 Subject: [PATCH] Tilemap.createFromObjects can now turn a bunch of Tiled objects into Sprites in one single call, and copies across all properties as well. --- README.md | 2 ++ examples/assets/maps/newtest.json | 10 +++---- examples/assets/maps/newtest.tmx | 18 ++++++++++--- examples/wip/tilemap.js | 6 +++-- src/core/Group.js | 36 ++++++++++++++++++++++++++ src/tilemap/Tilemap.js | 43 ++++++++++++++++++++++++++++--- src/tilemap/TilemapLayer.js | 2 +- 7 files changed, 102 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 750a2ec9..07b6a39e 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,7 @@ Significant API changes: * Loader.tileset has a new method signature. Please use the new format: load.tileset(key, url, tileWidth, tileHeight, tileMargin, tileSpacing, rows, columns, total). * TilemapLayers are now created via the Tilemap object itself: map.createLayer(x, y, width, height, tileset, layer, group) and no longer via the GameObjectFactory. +* Tilemap.createFromObjects can now turn a bunch of Tiled objects into Sprites in one single call, and copies across all properties as well. New features: @@ -60,6 +61,7 @@ New features: * TilemapLayers now have debug and debugAlpha values, this turns on the drawing of the collision edges (very handy for debugging, as the name implies!) * Tweens have a new event: onLoop. * 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. +* Group.set will let you deep set a new propery on a single child of the Group. New Examples: diff --git a/examples/assets/maps/newtest.json b/examples/assets/maps/newtest.json index 49cdaea5..f0610494 100644 --- a/examples/assets/maps/newtest.json +++ b/examples/assets/maps/newtest.json @@ -84,13 +84,13 @@ "name":"", "properties": { - + "alpha":"0.5" }, "type":"", "visible":true, "width":0, - "x":454, - "y":307 + "x":443, + "y":244 }, { "gid":34, @@ -112,7 +112,7 @@ "name":"", "properties": { - + "alpha":"0.5" }, "type":"", "visible":true, @@ -140,7 +140,7 @@ "name":"", "properties": { - + "body.gravity.y":"5" }, "type":"", "visible":true, diff --git a/examples/assets/maps/newtest.tmx b/examples/assets/maps/newtest.tmx index 6e448313..294759c0 100644 --- a/examples/assets/maps/newtest.tmx +++ b/examples/assets/maps/newtest.tmx @@ -49,11 +49,23 @@ - + + + + + - + + + + + - + + + + + diff --git a/examples/wip/tilemap.js b/examples/wip/tilemap.js index 8169ddc7..3d32ca17 100644 --- a/examples/wip/tilemap.js +++ b/examples/wip/tilemap.js @@ -20,12 +20,14 @@ function preload() { // game.load.image('phaser', 'assets/sprites/mushroom2.png'); // game.load.image('phaser', 'assets/sprites/wabbit.png'); game.load.image('phaser', 'assets/sprites/arrow.png'); + game.load.spritesheet('coin', 'assets/sprites/coin.png', 32, 32); // game.load.image('phaser', 'assets/sprites/darkwing_crazy.png'); } var cursors; var map; +var coins; var layer; var layer2; @@ -57,13 +59,13 @@ function create() { // layer2.alpha = 0.5; layer = map.createLayer('Tile Layer 1'); - layer.scrollFactorX = 0.5; // layer.debug = true; layer.resizeWorld(); - + //coins = + map.createFromObjects(34, 'coin', 0) // layer2 = game.add.tilemapLayer(0, 0, 400, 600, null, map, 0); // layer.cameraOffset.x = 400; diff --git a/src/core/Group.js b/src/core/Group.js index 76c062c1..a2f52160 100644 --- a/src/core/Group.js +++ b/src/core/Group.js @@ -335,6 +335,11 @@ Phaser.Group.prototype = { }, + /** + * Internal test. + * + * @method Phaser.Group#childTest + */ childTest: function (prefix, child) { var s = prefix + ' next: '; @@ -363,6 +368,11 @@ Phaser.Group.prototype = { }, + /** + * Internal test. + * + * @method Phaser.Group#swapIndex + */ swapIndex: function (index1, index2) { var child1 = this.getAt(index1); @@ -634,6 +644,32 @@ Phaser.Group.prototype = { }, + /** + * This function allows you to quickly set a property on a single child of this Group to a new value. + * The operation parameter controls how the new value is assigned to the property, from simple replacement to addition and multiplication. + * + * @method Phaser.Group#set + * @param {Phaser.Sprite} child - The child to set the property on. + * @param {string} key - The property, as a string, to be set. For example: 'body.velocity.x' + * @param {*} value - The value that will be set. + * @param {boolean} [checkAlive=false] - If set then the child will only be updated if alive=true. + * @param {boolean} [checkVisible=false] - If set then the child will only be updated if visible=true. + * @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. + */ + set: function (child, key, value, checkAlive, checkVisible, operation) { + + key = key.split('.'); + + if (typeof checkAlive === 'undefined') { checkAlive = false; } + if (typeof checkVisible === 'undefined') { checkVisible = false; } + + if ((checkAlive === false || (checkAlive && child.alive)) && (checkVisible === false || (checkVisible && child.visible))) + { + this.setProperty(child, key, value, operation); + } + + }, + /** * This function allows you to quickly set the same property across all children of this Group to a new value. * The operation parameter controls how the new value is assigned to the property, from simple replacement to addition and multiplication. diff --git a/src/tilemap/Tilemap.js b/src/tilemap/Tilemap.js index 90c64a9d..75f02fd2 100644 --- a/src/tilemap/Tilemap.js +++ b/src/tilemap/Tilemap.js @@ -184,16 +184,51 @@ Phaser.Tilemap.prototype = { }, // Region? Remove tile from map data? - createSpritesFromTiles: function (layer, tileIndex, key, frame, group) { + createFromTiles: function (layer, tileIndex, key, frame, group) { if (typeof group === 'undefined') { group = this.game.world; } }, - createGroup: function (layer, parent) { + /** + * 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 + * 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 + * Sprite is created. You could also give it a value like: body.velocity.x: 100 to set it moving automatically. + * + * @method Phaser.Tileset#createFromObjects + * @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. + * @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=true] - 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] - Optional Group to add the Sprite to. If not specified it will be added to the World group. + */ + createFromObjects: function (gid, key, frame, exists, autoCull, group) { - // Creates a Group based on an objectgroup, with one Sprite per gid entry, using the tilemap image + frame number - // Will only work if image is loaded as spritesheet + if (typeof exists === 'undefined') { exists = true; } + if (typeof autoCull === 'undefined') { autoCull = true; } + if (typeof group === 'undefined') { group = this.game.world; } + + var sprite; + + for (var i = 0; i < this.objects.length; i++) + { + if (this.objects[i].gid === gid) + { + sprite = group.create(this.objects[i].x, this.objects[i].y, key, frame, exists); + + sprite.anchor.setTo(0, 1); + sprite.name = this.objects[i].name; + sprite.visible = this.objects[i].visible; + sprite.autoCull = autoCull; + + for (property in this.objects[i].properties) + { + group.set(sprite, property, this.objects[i].properties[property], false, false, 0); + } + } + } }, diff --git a/src/tilemap/TilemapLayer.js b/src/tilemap/TilemapLayer.js index 7d74d4eb..ab11e245 100644 --- a/src/tilemap/TilemapLayer.js +++ b/src/tilemap/TilemapLayer.js @@ -304,7 +304,7 @@ Phaser.TilemapLayer.prototype.postUpdate = function () { */ Phaser.TilemapLayer.prototype.resizeWorld = function () { - this.game.world.setBounds(0, 0, this.layer.widthInPixels * 4, this.layer.heightInPixels); + this.game.world.setBounds(0, 0, this.layer.widthInPixels, this.layer.heightInPixels); }