craidencool Posted June 26, 2016 Share Posted June 26, 2016 Helo! Is there a way in which an object from the object layer can be the starting location of the game and player? and how do you implement it? thank you! Link to comment Share on other sites More sharing options...
hexus Posted June 26, 2016 Share Posted June 26, 2016 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) } } drhayes 1 Link to comment Share on other sites More sharing options...
drhayes Posted June 27, 2016 Share Posted June 27, 2016 What hexus has written is what I do in my game. I have an object layer named "portals". One of the rectangles in that object layer is named "spawnPoint". The position of that rectangle become the spawn point for the Player sprite. Link to comment Share on other sites More sharing options...
hexus Posted June 27, 2016 Share Posted June 27, 2016 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 More sharing options...
balrog_ Posted August 29, 2016 Share Posted August 29, 2016 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 More sharing options...
Recommended Posts