Jump to content

PHASER Tiled maps object layer position


craidencool
 Share

Recommended Posts

Phaser doesn't implement this because there are so many different ways of interpreting this data - it becomes game-specific very quickly.

Try starting from something like this:

// Create the Phaser tilemap
var map = game.make.tilemap('map');

// Loop over each object layer
for (var ol in map.objects) {
	// Loop over each object in the object layer
	for (var o in map.objects[ol]) {
		var object = map.objects[ol][o];
		
		console.log(object);
		
		// Do something with the object data here; game.add.sprite(object.name)
		// for example, or even game.add[object.type](object.name)
	}
}

 

Link to comment
Share on other sites

Yeah, if you truly only want to use one object on the map then you could just index into it directly to get the data you're interested in.

var object = map.objects[0][0];

game.add.sprite('player', object.x, object.y);

Probably best to make sure that you get the layer and object indexes correct though - they could be prone to change depending on how many object layers, and how many objects in the desired layer, in your map.

There are other ways in the API to search for layers and objects with specific names, but I can't remember the methods off the top of my head. Check out the API docs for tilemaps. 

Link to comment
Share on other sites

  • 2 months later...

so... How I know those indexes? and in map.objects is where I will change it to the name of the object layer I have? ex map.playerPos

I have been doing it all like this tutorial and I don't have any problems: https://gamedevacademy.org/html5-phaser-tutorial-top-down-games-with-tiled/

but when I saw this http://phaser.io/examples/v2/tilemaps/create-from-objects

//  And now we convert all of the Tiled objects with an ID of 34 into sprites within the coins group
    map.createFromObjects('Object Layer 1', 34, 'coin', 0, true, false, coins);

I thought, wow that's less complicated and easier but couldn't make it work, any idea?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

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