I am using Tiled to create a map with multiple layers. One of those layers is an object layer with tile objects. My intent is for those objects to have special properties so that on, say, a collision, I can take an appropriate action (such as loading the appropriate scene when the player’s sprite collides with a door).
For the life of me, I cannot figure out how to do this. The closest I can get is using createFromObjects, but I seem to be required to specify the image I want to use (which isn’t what I want…I have multiple tilesets and am trying to keep this generic, so I am trying to use whatever tile in whatever asset was used in Tiled). I’d rather not encode all of the possible tiles that could have special properties by id; instead, I want to iterate through the objects, and render the tile as it shows in Tiled. Right now, I have something like this within my create:
const gids = {}
const group = this.add.group()
layer = map.getObjectLayer(name)
layer.objects.forEach(object => {
const {gid, id} = object
//I do this check because createFromObjects will
//have already created objects once I use the same gid.
if (!gids[gid]) {
const objects = map.createFromObjects(name, gid)
objects.reduce((group, sprite) => {
group.add(sprite)
this.physics.world.enable(sprite)
sprite.body.setImmovable()
return group
}, group)
}
})
In another section of my code, I set the collisions between the player and the objects, and that all works as expected. My problem right now is only with rendering the tile. I’m assuming I just need to pass the right secret sauce to createFromObjects, but I have two constraints:
- It seems like I need to specify a key to a texture asset. As far as I can tell, I don’t know the asset, but perhaps I can find the asset from the gid?
- I need to tell it which actual tile within the asset to render.
Honestly, I would have assumed there was a simpler way to do this…my understanding is that the gid uniquely identifies a tile across all tilesets within a tilemap, so presumably that alone should be enough for Phaser to render the object with the correct texture. Am I missing something?