steve228uk Posted August 16, 2014 Share Posted August 16, 2014 Hi Guys, I've just started playing with Phaser and am loving it. I seem to be having some problems with collision groups though. I'm using game.physics.p2.convertCollisionObjects method but can't seem to get my player to collide with my platforms. I've uploaded what I've got so far and was hoping someone would be able to shed some light on this. Any help is extremely appreciated. http://steve228uk.webfactional.com/game/ Cheers!Steve Edit: It's probably easier if I post the code here too:var game = new Phaser.Game(window.innerWidth, window.innerHeight, Phaser.AUTO, 'game');var mainState = { preload: function() { game.load.tilemap('map', 'assets/level1.json', null, Phaser.Tilemap.TILED_JSON); game.load.image('tiles', 'assets/tilemap50.png'); game.load.spritesheet('dude', 'assets/dude.png', 32, 48); game.load.image('bg', 'assets/bg.jpg'); }, create: function() { this.setup(); this.createMap(); this.createPlayer(); }, setup: function(){ game.physics.startSystem(Phaser.Physics.P2JS); game.physics.p2.setImpactEvents(true); game.physics.p2.restitution = 0.8; var bg = game.add.sprite(0, 0, 'bg'); bg.fixedToCamera = true; this.cursors = game.input.keyboard.createCursorKeys(); }, createMap: function(){ game.stage.backgroundColor = '#787878'; this.map = game.add.tilemap('map'); // The first parameter is the tileset name, as specified in the Tiled map editor (and in the tilemap json file) // The second parameter maps this name to the Phaser.Cache key 'tiles' this.map.addTilesetImage('Tilemap50', 'tiles'); this.platforms = this.map.createLayer('Level1'); this.platforms.resizeWorld(); this.createCollisions(); }, createCollisions: function(){ this.collisionGroups = { player: game.physics.p2.createCollisionGroup(), platforms: game.physics.p2.createCollisionGroup(), }; var lines = game.physics.p2.convertCollisionObjects(this.map, 'Collisions', false); for(var line in lines){ lines[line].setCollisionGroup(this.collisionGroups.platforms); lines[line].collides(this.collisionGroups.player); } game.physics.p2.updateBoundsCollisionGroup(); }, createPlayer: function(){ this.player = game.add.sprite(32, 0, 'dude'); game.physics.p2.enable(this.player); // Player physics properties. Give the little guy a slight bounce. this.player.body.enable = true; this.player.body.gravity.y = 300; this.player.body.collideWorldBounds = true; this.player.body.setCollisionGroup(this.collisionGroups.player); console.log(this.collisionGroups.player); console.log(this.collisionGroups.platforms); this.player.body.collides(this.collisionGroups.platforms, function(){ console.log('collided'); }); game.camera.follow(this.player); // Our two animations, walking left and right. this.player.animations.add('left', [0, 1, 2, 3], 10, true); this.player.animations.add('right', [5, 6, 7, 8], 10, true); }, update: function() { // game.physics.p2.collide(this.player, this.collisions); this.handleInput(); }, handleInput: function(){ this.player.body.velocity.x = 0; if (this.cursors.left.isDown) { // Move to the left this.player.body.velocity.x = -150; this.player.animations.play('left'); } else if (this.cursors.right.isDown) { // Move to the right this.player.body.velocity.x = 150; this.player.animations.play('right'); } else { // Stand still this.player.animations.stop(); this.player.frame = 4; } // Allow the this.player to jump if they are touching the ground. if (this.cursors.up.isDown && this.player.body.touching.down) { // this.player.body.velocity.y = -350; } }};// Add and start the 'main' state to start the gamegame.state.add('main', mainState);game.state.start('main'); Link to comment Share on other sites More sharing options...
valueerror Posted August 17, 2014 Share Posted August 17, 2014 i think you forgot to define the tiles that should collide before you call createCollisionsuse collissionbyexclusion if you want all tiles to be able to collide Link to comment Share on other sites More sharing options...
steve228uk Posted August 17, 2014 Author Share Posted August 17, 2014 I was looking at using polylines and created an object layer to hold them in Tiled. I gave up with this in the end and used the collisionByExclusion method. Link to comment Share on other sites More sharing options...
valueerror Posted August 17, 2014 Share Posted August 17, 2014 oh sorry.. i should read more carefull before answering.. why are you setting the third parameter of convercollisionobjects to false (this means addToWorld) ... i leave this parameter out (the default is true) and it works great.. it is way better to work with polylines because you can have slanted tiles, slopes whatever form you like Link to comment Share on other sites More sharing options...
steve228uk Posted August 18, 2014 Author Share Posted August 18, 2014 Yeah I know, that's ideally what I'd like. Any idea how to get it to work? I initially had it left out and set to the default of true but I don't think that worked either… Link to comment Share on other sites More sharing options...
valueerror Posted August 18, 2014 Share Posted August 18, 2014 sorry.. with your code no.. i would need to test it with your assets.. convertcollisionobjects works great.. this is how i do it and it works. . i can only guess that it has something to do with your collisiongroups or the way you apply them.. does it work without collisiongroups? (without them everything should collidie with everything..map = game.add.tilemap(level);collisionareas = game.physics.p2.convertCollisionObjects(map,"collisionareas");for (i=0; i<collisionareas.length; i++){ collisionareas[i].setCollisionGroup(groundCG); collisionareas[i].collides([playerCG,enemyGroundCG]); collisionareas[i].setMaterial(iceMaterial); } Link to comment Share on other sites More sharing options...
Recommended Posts