Jump to content

Tilemap collision with Phaser 2.0


julacariote
 Share

Recommended Posts

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!

post-6203-0-03106600-1395135586_thumb.jp

Link to comment
Share on other sites

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

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:15

I've checked the name in Tiled and the JSON file and it matches.

Link to comment
Share on other sites

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

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

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;    },
Link to comment
Share on other sites

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

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

  • 4 weeks later...
 Share

  • Recently Browsing   0 members

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