julacariote Posted March 18, 2014 Share Posted March 18, 2014 Hello, I'm playing with Phaser 2.0, trying to make a very simple platform game. In Tiled Map Editor, it's possible to create an objet layer to draw shapes which could be used as "collision boxes". Is there anyway to use this layer to handle collisions between the player and the ground for example in phaser? The examples only show how to use the setCollisionBetween, setCollision or setCollisionByExclusion methods. Thanks! Link to comment Share on other sites More sharing options...
rich Posted March 18, 2014 Share Posted March 18, 2014 There is experimental support for this in the Tilemap and P2 API. You need to be using P2 physics, it won't work with any other. Then try doing:game.physics.p2.convertCollisionObjects(map, layer, addToWorld);Tweak the parameters as required. Link to comment Share on other sites More sharing options...
julacariote Posted March 18, 2014 Author Share Posted March 18, 2014 Thanks! Will try that. Link to comment Share on other sites More sharing options...
Wavertron Posted March 19, 2014 Share Posted March 19, 2014 This seems like quite a powerful feature. How to create the object layer in Phaser?In Tiled my "Collision" layer is defined as an Object Layer not a Tile Layer, but it doesnt seem to play nice with createLayer function.map = this.game.add.tilemap('map1');map.addTilesetImage('walls', 'mapTiles');visualLayer = map.createLayer('Tile Layer 1');collisionLayer = map.createLayer('Collision'); //no work :(this.game.physics.p2.convertCollisionObjects(map, collisionLayer);At line 4, it fails in the console with "Tilemap.createLayer: Invalid layer ID given: null", phaser.min.js:15I've checked the name in Tiled and the JSON file and it matches. Link to comment Share on other sites More sharing options...
julacariote Posted March 19, 2014 Author Share Posted March 19, 2014 Yes, I ran into the same problem. If you read the code, you will see that map.createLayer('mylayer') search for 'mylayer' in the layers attribute. This attribute is populated by TilemapParser with only layers of type "tilelayer". Therefore, an object layer like the collision layer can't be found here... But I can't find the right way to do that... Link to comment Share on other sites More sharing options...
Bloodberet Posted March 19, 2014 Share Posted March 19, 2014 Yes, I ran into the same problem. If you read the code, you will see that map.createLayer('mylayer') search for 'mylayer' in the layers attribute. This attribute is populated by TilemapParser with only layers of type "tilelayer". Therefore, an object layer like the collision layer can't be found here... But I can't find the right way to do that... I for sure would like this feature! I also have tried to get Object Layer from Tiled MapEditor to work. I getting same error that Rudy and you! Link to comment Share on other sites More sharing options...
julacariote Posted March 20, 2014 Author Share Posted March 20, 2014 I think there is something wrong with the Phaser.Physics.P2.convertCollisionObjects method. See my comments in the code below: convertCollisionObjects: function (map, layer, addToWorld) { if (typeof addToWorld === 'undefined') { addToWorld = true; } // This method returns the layer index, an integer layer = map.getLayer(layer); var output = []; // But map.collision is an object whose keys are the objects layers names. // It is defined as an array in Phaser.Tilemap (L117) but Phaser.TilemapParser // creates it as an object, see Phaser.TilemapParser L344 // So map.collision[layer] is undefined for (var i = 0, len = map.collision[layer].length; i < len; i++) { // name: json.layers[i].objects[v].name, // x: json.layers[i].objects[v].x, // y: json.layers[i].objects[v].y, // width: json.layers[i].objects[v].width, // height: json.layers[i].objects[v].height, // visible: json.layers[i].objects[v].visible, // properties: json.layers[i].objects[v].properties, // polyline: json.layers[i].objects[v].polyline var object = map.collision[layer][i]; var body = this.createBody(object.x, object.y, 0, addToWorld, {}, object.polyline); if (body) { output.push(body); } } return output; }, Bloodberet 1 Link to comment Share on other sites More sharing options...
onefatman Posted March 20, 2014 Share Posted March 20, 2014 does the collision layer have to be an object layer? I want to be able to build my background/foreground layers in Tiled and then build one hidden collision layer out of tiles that I can then say to the player character "this is the collision layer" - is this possible? psy_ 1 Link to comment Share on other sites More sharing options...
Bloodberet Posted March 24, 2014 Share Posted March 24, 2014 There is experimental support for this in the Tilemap and P2 API. You need to be using P2 physics, it won't work with any other. Then try doing:game.physics.p2.convertCollisionObjects(map, layer, addToWorld);Tweak the parameters as required. Have you look at @julacariote code? Link to comment Share on other sites More sharing options...
bmceldowney Posted March 26, 2014 Share Posted March 26, 2014 It works well if you comment out the line at 1385. Then you just need to pass the object layer name as a string to convertCollisionObjects. Also the only objects parsed are polyline objects. I tried at first with polygons and rectangles but they are ignored by the map parser. Link to comment Share on other sites More sharing options...
bmceldowney Posted March 26, 2014 Share Posted March 26, 2014 My pull request to remove the map.getLayer check was merged into the dev branch this morning. If you create a build off of that branch you should be able to see this feature working as intended. Link to comment Share on other sites More sharing options...
neoRiley Posted April 18, 2014 Share Posted April 18, 2014 My pull request to remove the map.getLayer check was merged into the dev branch this morning. If you create a build off of that branch you should be able to see this feature working as intended. Do you know if this is in v2.0.2? Link to comment Share on other sites More sharing options...
bmceldowney Posted April 18, 2014 Share Posted April 18, 2014 Do you know if this is in v2.0.2?Yes it is. Though to my knowledge polyline objects are still the only Tiled objects that get parsed for collisions. Link to comment Share on other sites More sharing options...
Recommended Posts