casarock Posted September 25, 2013 Share Posted September 25, 2013 Hi, I want to detect a collision or better sayed overlapping of a sprite with a group. I wasn't able to overlap the objects using the game.physic.collide method. I always get a collision and my "character" stops moving. I used a code inspiered by collision detection examples:(function () { var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update }); function preload() { game.load.image('phaser', 'phaser-dude.png'); game.load.spritesheet('veggies', 'fruitnveg64wh37.png', 64, 64); } var sprite; var veggies; function create() { game.stage.backgroundColor = '#2d2d2d'; // This will check Group vs. Group collision (bullets vs. veggies!) veggies = game.add.group(); for (var i = 0; i < 50; i++) { var c = veggies.create(game.world.randomX, Math.random() * 500, 'veggies', game.rnd.integerInRange(0, 36)); c.name = 'veg' + i; c.body.immovable = true; //c.body.allowCollision = false; //c.body.touching = { none: false, up: true, down: true, left: true, right: true }; } sprite = game.add.sprite(400, 550, 'phaser'); } function update() { sprite.velocity.x = 0; sprite.velocity.y = 0; if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT)) { sprite.velocity.x = -200; } else if (game.input.keyboard.isDown(Phaser.Keyboard.RIGHT)) { sprite.velocity.x = 200; } else if (game.input.keyboard.isDown(Phaser.Keyboard.UP)) { sprite.velocity.y = -200; } else if (game.input.keyboard.isDown(Phaser.Keyboard.DOWN)) { sprite.velocity.y = 200; } game.physics.collide(sprite, veggies, collisionHandler, null, this); } function collisionHandler(sprite, veg) { console.log(sprite, veg); }})();as you could see I've experimented with allowcollison (colision could be deactivated easily) and body.touching. It seems like I am doing it wrong. I am not able to detect overlapping/collision. Or do I have to implement this by my own routine? Before I start I just want to be sure if phaser could support me Thanks! Link to comment Share on other sites More sharing options...
cang Posted September 25, 2013 Share Posted September 25, 2013 If you don't want collision and just want overlap I found that you can set customSeparateX and customSeparateY to true on the sprite body. Link to comment Share on other sites More sharing options...
rich Posted September 25, 2013 Share Posted September 25, 2013 Right - exactly as cang said. However I'm redoing the collision system at the moment and have included a straight 'overlaps' check with separation optional. The current quadtree implementation isn't correctly updated for objects in Groups. You can checkout the dev branch and you'll see the progress I've been making. I need to resolve some tilemap issues and then it will be part of the 1.0.7 release towards the end of next week. michaelcalkins 1 Link to comment Share on other sites More sharing options...
claire Posted October 2, 2013 Share Posted October 2, 2013 I don't really understand what cang said, can please elaborate more? Link to comment Share on other sites More sharing options...
cang Posted October 2, 2013 Share Posted October 2, 2013 Sorry I wasn't clear enough. I assume you want to do it on your veggie objects so it would be something like this. for (var i = 0; i < 50; i++) { var c = veggies.create(game.world.randomX, Math.random() * 500, 'veggies', game.rnd.integerInRange(0, 36)); c.name = 'veg' + i; c.body.immovable = true; c.body.customSeparateX = true; c.body.customSeparateY = true; }Now when you call the collide method it won't perform that separation logic. Edit: Ah didn't notice the person who asked the question was different than the OP. Anyway example still applies. Son of Bryce 1 Link to comment Share on other sites More sharing options...
claire Posted October 3, 2013 Share Posted October 3, 2013 Ok got it. Thanks a lot. Link to comment Share on other sites More sharing options...
Son of Bryce Posted October 23, 2013 Share Posted October 23, 2013 Thanks for this explanation cang! I couldn't figure out why my player was jumping back every time an item was collected. Link to comment Share on other sites More sharing options...
Recommended Posts