Skip to content

Instantly share code, notes, and snippets.

@jdfight
Last active January 6, 2018 17:19
Show Gist options
  • Save jdfight/9646833f9bbdcb1104db to your computer and use it in GitHub Desktop.
Save jdfight/9646833f9bbdcb1104db to your computer and use it in GitHub Desktop.
Phaser - Creating objects from Tiled Map Object layers
//From http://www.gamedevacademy.org/html5-phaser-tutorial-top-down-games-with-tiled/
//find objects in a Tiled layer that containt a property called "type" equal to a certain value
findObjectsByType: function(type, map, layer) {
var result = new Array();
map.objects[layer].forEach(function(element) {
console.log(element);
if (element.properties.type === type) {
//Phaser uses top left, Tiled bottom left so we have to adjust the y position
//also keep in mind that the cup images are a bit smaller than the tile which is 16x16
//so they might not be placed in the exact pixel position as in Tiled
console.log("Found " + element.name);
element.y -= map.tileHeight;
result.push(element);
}
});
return result;
},
//create a sprite from an object
createFromTiledObject: function(element, group) {
var sprite = group.add(new Coin(this.game, element.x, element.y)); // coin prefab
//copy all properties to the sprite
Object.keys(element).forEach(function(key) {
sprite[key] = element[key];
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment