Jump to content

Create Objectlayers ?!


gotenxds
 Share

Recommended Posts

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

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

 Share

  • Recently Browsing   0 members

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