r00 Posted March 14, 2014 Share Posted March 14, 2014 I'm migrating my game from 1.1.6 to 2.0, but I'm having problems getting Tiled json to work. 1) When using Tilemap.createLayer Phaser uses a tile from image B when it should use a tile from image A. As far as I have tested it's only doing this for one gid. I'm loading both images to the same tilemap, as one of them is used in the main layer and the other one in an object layer. Tiles were working properly prior to changing phaser version, and I haven't changed the spritesheets or the Tiled json files since. 2) Objects created with Tilemap.createFromObjects seem to be either in wrong place or their bodies appear to be in wrong place when using group.enablebody = true. When enabling group's bodies prior to generating objects with Tilemap.createFromObjects, objects will show in the wrong place, but their bodies appear to be more or less at the same place as their sprites. This is the way that was used at least in some of the examples.var coins = game.add.group();coins.enableBody = true;tilemap.createFromObjects('coins', 21, 'coin', 0, true, false, coins);When enabling group's bodies only after generating the objects with Tilemap.createFromObjects, all the objects show at their correct positions, but they don't have bodies. I take the bodies are supposed to be generated at createFromObjects if the group has the enableBody property set to true.var coins = game.add.group();tilemap.createFromObjects('coins', 21, 'coin', 0, true, false, coins);coins.enableBody = true;Again, if I individually enable physics on the objects after their generation, they get thrown into a wrong location. The location is not random, it just appears to be off by roughly the height of the object and possibly a bit to right.coins.forEach( function(coin) {// coin.anchor.setTo(0.5, 0.5) // does nothing (tried values from 0 to 1) game.physics.enable(coin, Phaser.Physics.ARCADE);}, this);I get the feeling that the problem is related to the anchor point of the sprites generated with createFromObjects. But why doesn't it help if I try to set the anchor before enabling physics? It works fine if I create an individual sprite that has nothing to do with tilemaps and set it's anchor before I enable physics for it. To further complicate the situation, I'm using Tiled's version 0.81, as it's the latest one available straight from my distro's repository. I'm not sure if the syntax differs between 0.81 and 0.91. Has anyone else had similar problems? Any suggestions how to solve either of the problems? Could I be missing some necessary call somewhere? Link to comment Share on other sites More sharing options...
r00 Posted March 14, 2014 Author Share Posted March 14, 2014 I figured 1) right after posting this. I was loading the image B unnecessarily to the tilemap with tilemap.addTilesetImage(). I didn't need to load the image to the map at all, as it was only used on object layer and loaded from cache when calling tilemap.createFromObject. It was apparently an old piece of code that i hadn't noticed to clean off. Still, it wasn't creating any problems with version 1.1.6. Link to comment Share on other sites More sharing options...
rich Posted March 14, 2014 Share Posted March 14, 2014 What happens if you just create the group and once added you then loop through it and add bodies to them all? Link to comment Share on other sites More sharing options...
r00 Posted March 14, 2014 Author Share Posted March 14, 2014 What happens if you just create the group and once added you then loop through it and add bodies to them all? I'm not sure what you mean. Do you mean something else than what I did in the third code example of 2)?this.coins = this.game.add.group();this.tilemap.createFromObjects('coins', 21, 'coin', 0, true, false, this.coins);this.coins.forEach( function(coin) { this.game.physics.enable(coin, Phaser.Physics.ARCADE);}, this);The result was the same as when I used group.enableBody = true before calling createFromObjects. That is, the sprites shifted from their intended places after the bodies were enabled. Or actually, the x and y do stay the same, but the anchoring changes. I'm thinking that's the problem i'm having: How to anchor sprites created with createFromObjects to their center points (or bottom points) after their bodies have been created. Previously in 1.1.6 when body was created with sprite, you could have done it just with:sprite.anchor.setTo(0.5, 0.5);and the body would have been anchored simultaneously with the sprite. But how do I do it now with arcade physics, after I have created the body? If I normally create a sprite and enable a body for it, i can change it's anchor:this.sprite = this.game.add.sprite(x, y, "player", 3);this.game.physics.enable(this.sprite, Phaser.Physics.ARCADE);this.sprite.anchor.setTo(0.5, 1); //worksBut if i try to change anchor of a sprite from a group i generated with createFromObjects(), it doesn't do anything:this.coins.forEach( function(coin) { this.game.physics.enable(coin, Phaser.Physics.ARCADE); coin.anchor.setTo(0.5, 1); // doesn't work}, this); Link to comment Share on other sites More sharing options...
r00 Posted March 14, 2014 Author Share Posted March 14, 2014 I think I figured it out, sort of: Sprites generated with createFromObjects() have their anchor initially set to 0, 1. I had to set anchor to 0, 0 before enabling the body, and only then setting the anchor of the sprite to 0,1 to get the sprite and its body in the wanted location.this.coins = this.game.add.group();this.tilemap.createFromObjects('coins', 21, 'coin', 0, true, false, this.coins);this.coins.forEach( function(coin) {// coin.anchor.x is 0 and coin.anchor.y is 1 coin.anchor.setTo(0, 0); this.game.physics.enable(coin, Phaser.Physics.ARCADE); coin.anchor.setTo(0, 1); // now it works}, this);EDIT: This didn't do the trick either. It just moved the sprite to the correct location while the body stayed at the location it was created. In the end I just set coin.y = coin.y - coin.height inside the forEach loop. Link to comment Share on other sites More sharing options...
Payou Posted March 17, 2014 Share Posted March 17, 2014 Hello, I'm in front of the same problem of you but when I try your solution the body rectangle stay at a bad place...I didn't understand your last sentence In the end I just set coin.y = coin.y - coin.height inside the forEach loop Can you post your final code to patch this plz? EDIT : I think Tiled Editor is the problem, when you check object location, he didn't take the correct coordinateExample :You place a tile at the position (1,4) and your tile size is 32, you should expect the tile will be at (32,128) If you check Tiled export this tile will be placed at (32,160)... Finally I have understand your final sentance . Link to comment Share on other sites More sharing options...
morriq Posted March 17, 2014 Share Posted March 17, 2014 I had the same problem(but Im not currently working on that), so let me put post here. It will be much easier for me to find solution . Thanks Link to comment Share on other sites More sharing options...
valueerror Posted March 26, 2014 Share Posted March 26, 2014 i ran into the very same problem.. (using P2) changing the anchor works but then the pyhsics body stays at it's initial place (just the sprite moves) and collisions happen with invisible objectborders.. that's no solution.. because in my case only Y is in need of correction i do the following: callsign.body.y += 32;i move the whole thing down exactly one tile.. but thats a workaround.. it does not matter if i apply the anchor change in a forEach() loop or with setAll() what does rich say to this "problem" ? Link to comment Share on other sites More sharing options...
ianmcgregor Posted March 26, 2014 Share Posted March 26, 2014 Have you seen the new adjustY param on createFromObjects? (2.01) It defaults to true, and shifts the sprite up by its height.createFromObjects: function (name, gid, key, frame, exists, autoCull, group, CustomClass, adjustY) {if (typeof adjustY === 'undefined') { adjustY = true; }if (adjustY){ sprite.y -= sprite.height;} Link to comment Share on other sites More sharing options...
valueerror Posted March 27, 2014 Share Posted March 27, 2014 oke.. looks like this is already fixed... btw. CustomClass doesn't accept something like false, or null.. i had to write Phaser.Sprite in it... ?? Link to comment Share on other sites More sharing options...
Recommended Posts