gotenxds Posted June 10, 2016 Share Posted June 10, 2016 Hello I've been trying to create an object layer for a tilemap for the last hour or so, only after debugging the code realizing the phaser complitly skips those, Any why of using them ? Link to comment Share on other sites More sharing options...
LTNGames Posted June 11, 2016 Share Posted June 11, 2016 Phaser handles them a bit differently, it took me a little while to understand how it worked. Basically, you can still read the objects from the JSON but what I did was create a sprite for each object, so you will need a separate sprite from the tileset, or you can try and use the tilesheet and get the position from there and use that as the image. This is how I iterated through the objects and created a sprite from them, maybe it will help if you see an example. Game.State_Level.prototype.createObjects = function(){ // this.map is my tilemap refrence, here I am going through each tile used in the object layer // Then I'm going to create an object from each one. if(this.map.objects.hasOwnProperty('objectLayer')){ this.map.objects.objectLayer.forEach(this.createObjectSprite, this); } }; //Here I get the properties from the tile then create a new Sprite based off the properties. // The properties can be set in TileD, in my case I just put the entire object into the extended sprite class. Game.State_Level.prototype.createObjectSprite = function(object){ this.mapObjects = {}; var x = object.x; var y = object.y; var mapObject; switch(object.type) { case 'box': mapObject = new Game.Sprite_Object(this.game, x, y, object, this); break; case 'gem': mapObject = new Game.Sprite_Object(this.game, x, y, object, this); break; } this.mapObjects[object.name] = mapObject; }; //Shorteneded version of my extended sprite, here you can see I used a //property I set in TileD named sprite, and this is the name of the image I will use, also the //key for the loaded image. Game.Sprite_Object = function () { this.init.apply(this, arguments); }; Game.Sprite_Object.prototype = Object.create(Phaser.Sprite.prototype); Game.Sprite_Object.prototype.constructor = Game.Sprite_Object; Game.Sprite_Object.prototype.init = function (game, x, y, object, state) { var key = object ? object.properties.sprite : ''; Phaser.Sprite.call(this, game, x, y, key); this.setProperties(object, state); this.state.boxGroup.add(this); this.setPhysics(); }; Link to comment Share on other sites More sharing options...
gotenxds Posted June 11, 2016 Author Share Posted June 11, 2016 Yaa I already figured this out, I think its really wired phaser does not support this as a layer. One thing I cant get tough, tiled doest not seem to set the Y position correctly, Its as if the x axis starts from the top-left (as in phaser) but the y axix starts from the bottom-left. Like so (0,10) ...........(10,10) . . . . (0,0)..............(10,0) so a picture that is positioned at 0.10 in tiled is positioned in 0,0 in phaser. Am I doing anything wrong here? Edit - figured it out for some strange reason tiled is using a pivot point of 0,1 for images, just needed to set anchors accordingly. Link to comment Share on other sites More sharing options...
drhayes Posted June 13, 2016 Share Posted June 13, 2016 There's also the Phaser function Phaser.Tilemap#createFromObjects: http://phaser.io/docs/2.4.8/Phaser.Tilemap.html#createFromObjects That basically does what you're doing there, so might not be a real win. It'll also assign anything in the "properties" to the sprite after it's been instantiated. Link to comment Share on other sites More sharing options...
LTNGames Posted June 13, 2016 Share Posted June 13, 2016 Omg drhayes that is so damn helpful lol I wish I seen that before doing it my way. For now on I'm just going to ask questions here on the forum before I proceed with the things I do lol. Link to comment Share on other sites More sharing options...
Recommended Posts