kenshm Posted August 12, 2015 Share Posted August 12, 2015 Hello,I'm begginer of phaser and am having some problems. Indeed, I had to change my game to switch to physics.ARCADE physics.P2JS Everything works except the asteroids as they bounce when it should not be the case.When I turn off these two lines of my code, my asteroids can go off the screen. But if i add this 2 lines, my asteroids bouncing around the screen.this.fighter.body.setCollisionGroup(this.fighterCollisionGroup);this.fighter.body.collides(this.asteoridCollisionGroup, this.collisionHandler, this);Complete code :SpaceWar.Game = function (game) { this.stick; this.pad; this.fighter;};SpaceWar.Game.prototype = { init: function(){ this.game.renderer.renderSession.roundPixels = true; this.game.physics.startSystem(Phaser.Physics.P2JS); }, create: function(){ this.game.physics.p2.setImpactEvents(true); this.game.physics.p2.restitution = 0.9; /** * fighterCollisionGroup {group} ==> collision group for the fighter * asteoridCollisionGroup {group} ==> collision group for the asteorids */ this.fighterCollisionGroup = this.game.physics.p2.createCollisionGroup(); this.asteoridCollisionGroup = this.game.physics.p2.createCollisionGroup(); this.game.physics.p2.updateBoundsCollisionGroup(); /** * Load the background and sets the size */ var background = this.game.add.sprite(0, 0, 'background'); background.width = this.game.world.width; background.height = this.game.world.height; /** * Created a group called "asteorids" * Enables pyhsics & spawn a random asteorid every X seconds * Each asteorid created is added to the group "asteorids" */ this.asteorids = this.add.group(); this.asteorids.enableBody = true; this.asteorids.physicsBodyType = Phaser.Physics.P2JS; this.asteorids.setAll('checkWorldBounds', true); this.asteorids.setAll('outOfBoundsKill', false); var asteroidLoop = this.game.time.events.loop(Phaser.Timer.SECOND * 0.4, this.createAsteorid, this); /** * Spawn the fighter (ship) to the center of the bottom screen * Active physics and prevents the fighter to come out of the screen * Load polygonal data (.json) for the hit box */ this.fighter = this.add.sprite(this.game.world.width/2, this.game.world.height - 100, 'fighter'); this.fighter.anchor.set(0.5); this.game.physics.p2.enable(this.fighter); //set to true for debugging this.fighter.body.clearShapes(); this.fighter.body.loadPolygon('physicsData', 'fighter'); this.fighter.body.collideWorldBounds = true; this.fighter.body.setCollisionGroup(this.fighterCollisionGroup); this.fighter.body.collides(this.asteoridCollisionGroup, this.collisionHandler, this); /** * Adds the joystick on the screen and set its size */ /* this.pad = this.game.plugins.add(Phaser.VirtualJoystick); this.stick = this.pad.addStick(0, 0, 300,'arcade'); this.stick.scale = 1.2; this.stick.alignBottomLeft(50); */ }, update: function(){ /** * Called when joysting is down and moving the fighter (ship) */ /* var speed = 500; if(this.stick.isDown){ this.physics.arcade.velocityFromRotation(this.stick.rotation, this.stick.force * speed, this.fighter.body.velocity); this.fighter.rotation = this.stick.rotation; }else{ this.fighter.body.velocity.set(0); } */ }, /** * Debuging render */ render: function() { }, /** * Generates & create a random asteroid types * type {INT} 1, 2, 3 or 4 -> is the type of asteorid to use (texture) */ createAsteorid: function(){ var type = this.game.rnd.integerInRange(1, 4); if(type != null){ var asteorid = this.asteorids.create(this.game.world.randomX -230, this.rndPositionY(), 'asteorid0' + type); this.game.physics.p2.enable(asteorid); asteorid.body.allowRotation = true; asteorid.body.velocity.y = this.rndVelocity(); asteorid.body.clearShapes(); asteorid.body.loadPolygon('physicsData', 'asteorid0' + type); asteorid.events.onEnterBounds.add(this.killOnOutOfBounds, this); asteorid.body.setCollisionGroup(this.asteoridCollisionGroup); asteorid.body.collides([this.asteoridCollisionGroup, this.fighterCollisionGroup]); } }, /** * Called when two objects collide - Explosion * @param {body1} fighter * @param {body2} asteorid */ collisionHandler: function(body1, body2){ body1.sprite.kill(); body2.sprite.kill(); var explosion = this.game.add.sprite(body1.x - 80, body1.y - 150, 'explosion'); explosion.animations.add('DoExplosion', [0, 1, 2, 3, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16], 10, false); var ExplosionAnimation = explosion.animations.play('DoExplosion', 10, false, true); ExplosionAnimation.onComplete.add(function(){ this.gameOver(); }, this); }, /** * Set the value of "outOfBoundsKill" to true on a sprite when it is called * @param {sprite} sprite */ killOnOutOfBounds: function(sprite) { sprite.outOfBoundsKill = true; }, /** * Generates a random position (Y) between -150 and -400 (for asteorids) */ rndPositionY: function(){ return this.game.rnd.integerInRange(-150, -400); }, /** * Generates a random (int) between 100 and 350 for gravity asteorid */ rndVelocity: function(){ return this.game.rnd.integerInRange(100, 250); }, /** * Called when the player loose the game */ gameOver: function(){ //this.stick.visible = false; //this.state.start('GameOver'); }};PS : Sorry for my english, i'm from Switzerland ^^Tanks all for helping !Best regards,Kenshimdev Link to comment Share on other sites More sharing options...
icp Posted August 12, 2015 Share Posted August 12, 2015 Set asteroids to kinematic bodies. Link to comment Share on other sites More sharing options...
tips4design Posted August 12, 2015 Share Posted August 12, 2015 Remove this line: this.game.physics.p2.updateBoundsCollisionGroup(); Link to comment Share on other sites More sharing options...
kenshm Posted August 12, 2015 Author Share Posted August 12, 2015 Remove this line: this.game.physics.p2.updateBoundsCollisionGroup();Thank you very much "friend" ! Link to comment Share on other sites More sharing options...
Recommended Posts