przemoo83 Posted October 7, 2015 Share Posted October 7, 2015 Could anyone please look at this example and tell me why the player won't collide with the tiles that are created during runtime by clicking? What do I do wrong?http://phaser.io/sandbox/FJIiHNZQ/play Link to comment Share on other sites More sharing options...
przemoo83 Posted October 7, 2015 Author Share Posted October 7, 2015 Ok I sort of detected the problem. I recreated the example using ARCADE Physics and it works well.http://phaser.io/sandbox/roUUxPZu/playSo now can someone please tell me how to do the same using P2? Link to comment Share on other sites More sharing options...
jmp909 Posted October 7, 2015 Share Posted October 7, 2015 you're only colliding one type of tile? (tile 40)map.setCollision(40,true);http://phaser.io/docs/2.4.3/Phaser.Tilemap.html#setCollision Link to comment Share on other sites More sharing options...
przemoo83 Posted October 7, 2015 Author Share Posted October 7, 2015 For the sake of this example yes but if I get this working I will probably want to collide several more types. Does it have to do something with my problem? Link to comment Share on other sites More sharing options...
jmp909 Posted October 7, 2015 Share Posted October 7, 2015 ah sorry, didn't read the question properly. convertTilemap... Goes through all tiles in the given Tilemap and TilemapLayer and converts those set to collide into physics bodies. Only call this after you have specified all of the tiles you wish to collide with calls like Tilemap.setCollisionBetween, etc. Every time you call this method it will destroy any previously created bodies and remove them from the world. Therefore understand it's a very expensive operation and not to be done in a core game update loop. it looks like you're going to have to manually add the body to the tile you created at runtime look around line 1758https://github.com/photonstorm/phaser/blob/858ad5161028d45d65679c88fc1e9a08ceed1f8f/src/physics/p2/World.js I don't know if there's a function to do it ... przemoo83 1 Link to comment Share on other sites More sharing options...
jmp909 Posted October 7, 2015 Share Posted October 7, 2015 this is guesswork based on that code... it works but i don't know if i've added it to the right place in the end if(this.game.input.mousePointer.isDown) { var tile = map.putTile(40, layer1.getTileX(marker.x), layer1.getTileY(marker.y), layer1); // add the body var body = game.physics.p2.createBody(marker.x, marker.y, 0, false); body.addRectangle(tile.width, tile.height, tile.width / 2, tile.height / 2, 0); game.physics.p2.addBody(body); layer1.bodies.push(body); } przemoo83 1 Link to comment Share on other sites More sharing options...
przemoo83 Posted October 7, 2015 Author Share Posted October 7, 2015 It does work:) thanks a lot! I wonder whether I could remove tiles with their bodies using the same method you described. Link to comment Share on other sites More sharing options...
Recommended Posts