Dlost1 Posted March 19, 2017 Share Posted March 19, 2017 Hi!! I would like to know: How do I refer to a Tiled/Json object id/name from phaser? I created an object in Tiled and when the user enters the area of this object (called tavern) a new game world/map should be opened (the player will enter the Tavern-map). How can I refer to an object in the object layer in my Json/Tiled map? something like: var one = map.object.id(x) if(playerX,playerY == oneX, oneY){game.state.start('GameMap2');} Any idea's? An example to do this perhaps? Thanks in advance:) (I have also tried: createFromObjects(name, gid, key, frame, exists, autoCull, group, CustomClass, adjustY) Also attempted to copy the following example, but did does not work (it uses a gid...I do not have a gid in my Json I tried to us the "id" in its stead. This resulted in "undefinded" errors). https://phaser.io/examples/v2/tilemaps/create-from-objects But in my Json there is no "gid" Json code: Quote "draworder":"topdown", "height":204, "name":"ObjectLayer", "objects":[ { "height":80, "id":5, "name":"Tavern", "rotation":0, "type":"", "visible":true, "width":60, "x":1169, "y":1001 }, Link to comment Share on other sites More sharing options...
Dlost1 Posted March 21, 2017 Author Share Posted March 21, 2017 Still hoping to get some help (please?;))... apparantly the coin example is ancient. (https://phaser.io/examples/v2/tilemaps/create-from-objects) So I tried to auto add a gid myself (the game will have lots of objects): http://stackoverflow.com/questions/38544978/gid-in-tile-map Quote this.map.ObjectLayer.objects.forEach(function(element){ element.gid=1; }, this); However I get: Uncaught TypeError: Cannot read property 'objects' of undefined <-----------------Huh? at Game.create (<anonymous>:177:10) at Phaser.StateManager.loadComplete (phaser.js:28530) at Phaser.StateManager.preUpdate (phaser.js:28307) at Phaser.Game.updateLogic (phaser.js:34908) at Phaser.Game.update (phaser.js:34801) at Phaser.RequestAnimationFrame.updateRAF (phaser.js:62538) at _onLoop (phaser.js:62521) So I CAN acces "ObjectLayer" but "objects" gives undefined? What gives? Again Json: Quote "draworder":"topdown", "height":204, "name":"ObjectLayer", "objects":[ { "height":80, "id":5, "name":"Tavern", "rotation":0, "type":"", "visible":true, "width":60, "x":1169, "y":1001 }, Anyone got any idea why I get undefined? Link to comment Share on other sites More sharing options...
Dlost1 Posted March 23, 2017 Author Share Posted March 23, 2017 Ok, I finally got it:) For other people struggling with this: I refered to the wrong labels here is what I did to solve it. It is highly likely that there is a better way than my rugged sollution, though. And ofcourse I d be happy to see comments improving my solution;) I did as follows to fix this: Quote preload: function(){ game.load.tilemap('GameWorld1616', 'assets/images/GameWorld.json', null, Phaser.Tilemap.TILED_JSON); //preloading the Tiled map game.load.text('GameWorld1616', 'assets/images/GameWorld.json'); //preloading the same map again, but this time as text so I can parse it } create: function(){ this.b = JSON.parse(this.game.cache.getText('GameWorld1616')); //parse the Json file so you can use it in Jscript/phaser this.c = this.b.layers[4].objects; //now you can refer to the objects in the Json file as long as you follow the document structure correctly. In my case layer 4 contained the objects. } The whole structure of my Json file to complete my solution: Quote Object height:204 layers:Array[5] 0:Object 1:Object 2:Object 3:Object 4:Object length:5 __proto__:Array[0] nextobjectid:9 orientation:"orthogonal" renderorder:"right-down" tileheight:16 tilesets:Array[20] tilewidth:16 version:1 width:204 __proto__:Object Link to comment Share on other sites More sharing options...
Recommended Posts