Jump to content

changeable tiles, best way


xronn
 Share

Recommended Posts

Whats the best way to add changeable tiles to your game for example buttons, should they be added in when you create the map and added to the base json array. Or should I draw them on on-top of the map, whats the best why to go about it.

 

I was thinking if I added them with the map data I could call the tile ID and change it for another tile or do as I wish to it.

or 

Could I draw the tile on-top of the map and position it were I want then destroy it when required. 

 

 

What method should I take?

Link to comment
Share on other sites

there are two basic ways to go about this:

 

1) set the tile in the map data (there are examples of this in the tilemap examples). the advantage to this is that it is simple and easy

2) use a Sprite at the given location. the advantage to this is flexibility

 

#1 is perfectly adequate if you just want to change the value of the tile. if you want it to be animated etc then #2 has the advantage

Link to comment
Share on other sites


I would personally do this using the createFromObjects Tilemap function that is in 1.1.4.

 

You could simply add your objects in your map inside Tiled, export the JSON, and load it like you normally would.

 

The way you would use this function is below:




map.createFromObjects('Object Layer 1’, 5, ‘coin', 0, true, true, coins);



Here you can see the doc comments and the signature of this function from within the Phaser source as well:






/**
* 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 {string} name - The name of the Object Group to create Sprites from.
* @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 (name, gid, key, frame, exists, autoCull, group)


This can be very useful, as it allows you to create a group of sprites, which you can interface with in the same ways you can with any other group inside Phaser. This means you can check the collision and perform a function on the specific object that was collided with.

 

This works just like any other group:




this.game.physics.collide(player, coins, this.addMoney, null, this);



addMoney: function (p, i) {
player.money += this.game.rnd.integerInRange(1, 5);
i.destroy();
}




Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...