Decktonic Posted February 10, 2016 Share Posted February 10, 2016 Hi, I'm building a little game where a ship is contained within a square in the middle of the game world and "bounces" off the walls. It's easier to just see it in action so here's what I have so far: http://mappdev.com/spin/ The ship starts in the middle of the canvas, spinning endlessly. When you click or touch the canvas, the ship moves forward in the angle it's facing. So I have a 640x960 canvas with a red background and then I draw a black 600x600 rounded rectangle in the center, then I add 4 objects on top, bottom and sides that are the "walls" meant to keep the ship within the square. I'm using a group for these walls. Right now the ship bounces off the edges of the game world but as far as I can tell it doesn't collide with the walls at all. Here's my code: var defaults = { canvasWidth: 640, canvasHeight: 960, angle: -90, velocity: 500, angularVelocity: 400, }; var game = new Phaser.Game(defaults.canvasWidth, defaults.canvasHeight, Phaser.AUTO, 'container', { preload: preload, create: create, update: update, render: render }); var walls; var ship; function preload() { game.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL; game.load.image('wall', 'assets/wall.png'); game.load.image('ship', 'assets/ship.png'); game.load.image('coin', 'assets/coin.png'); game.load.image('bomb', 'assets/bomb.png'); } function create() { game.physics.startSystem(Phaser.Physics.ARCADE); game.stage.backgroundColor = "#FF0000"; // "#37474F"; var graphics = game.add.graphics(20, 180); graphics.beginFill(0x000000, 1); graphics.drawRoundedRect(0, 0, 600, 600, 8); walls = game.add.physicsGroup(); wall_top = walls.create(0, 0, 'wall'); wall_top.scale.setTo(640, 180); wall_top.body.immovable = true; wall_right = walls.create(620, 180, 'wall'); wall_right.scale.setTo(20, 600); wall_right.body.immovable = true; wall_bottom = walls.create(0, 780, 'wall'); wall_bottom.scale.setTo(640, 180); wall_bottom.body.immovable = true; wall_left = walls.create(0, 180, 'wall'); wall_left.scale.setTo(20, 600); wall_left.body.immovable = true; ship = game.add.sprite(defaults.canvasWidth * 0.5, defaults.canvasHeight * 0.5, 'ship'); ship.scale.setTo(0.5, 0.5); ship.anchor.set(0.5, 0.5); ship.angle = defaults.angle; game.physics.arcade.enable(ship); ship.body.allowGravity = false; ship.body.immovable = true; ship.body.collideWorldBounds = true; ship.body.bounce.setTo(1, 1); ship.body.angularVelocity = defaults.angularVelocity; } function update() { game.physics.arcade.collide(ship, walls); if(game.input.activePointer.isDown) { ship.body.angularVelocity = 0; ship.body.velocity.copyFrom(game.physics.arcade.velocityFromAngle(ship.angle, defaults.velocity)); } else { ship.body.angularVelocity = defaults.angularVelocity; ship.body.velocity.set(0, 0); } if(ship.body.blocked.up || ship.body.blocked.down || ship.body.touching.up || ship.body.touching.down) { ship.angle = 0 - ship.angle; ship.body.velocity.copyFrom(game.physics.arcade.velocityFromAngle(ship.angle, defaults.velocity)); } if(ship.body.blocked.right || ship.body.blocked.left || ship.body.touching.right || ship.body.touching.left) { ship.angle = 180 - ship.angle; ship.body.velocity.copyFrom(game.physics.arcade.velocityFromAngle(ship.angle, defaults.velocity)); } } function render() { // game.debug.spriteInfo(ship, 32, 32); } Any idea what I'm missing? I'm using latest Phaser version 2.4.4 by the way. Link to comment Share on other sites More sharing options...
Decktonic Posted February 11, 2016 Author Share Posted February 11, 2016 OK rich provided the fix via Slack: Quote two immovable objects cannot collide, most likely your ship didn’t ought to be immovable Removing this line: ship.body.immovable = true; Fixed it! Link to comment Share on other sites More sharing options...
Recommended Posts