nb0yc33 Posted October 21, 2017 Share Posted October 21, 2017 Hey, I'm relatively new to Phaser, it'd be great if anyone could help with getting the collision between the user and the laserBoundaries group to work. The main code is below: var user; var laserBoundaries; var collisionHandler; var Level1State = { create: function(){ game.physics.startSystem(Phaser.Physics.ARCADE); var backdrop = game.add.image(0, 0, 'backdrop'); var exit = game.add.image(1066, 3156, 'staircaseexit'); var bomb = game.add.image(900, 25, 'bomb'); game.world.setBounds(0, 0, 1152, 3240); user = game.add.sprite(560, 5, 'player'); game.physics.enable(user, Phaser.Physics.ARCADE); game.camera.follow(user); user.body.collideWorldBounds = true; laserBoundaries = this.add.group(); game.physics.enable(laserBoundaries, Phaser.Physics.ARCADE); var laser1 = game.add.image(650, 273, 'laser'); var laser2 = game.add.image(650, 0, 'laser2'); var laser3 = game.add.image(470, 0, 'laser2'); var laser4 = game.add.image(72, 273, 'laser'); laserBoundaries.add(laser1); laserBoundaries.add(laser2); laserBoundaries.add(laser3); laserBoundaries.add(laser4); cursors = game.input.keyboard.createCursorKeys(); }, update: function(){ game.physics.arcade.overlap(user, laserBoundaries, collisionHandler, null, this); if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT)) { user.x = user.x-4; } else if (game.input.keyboard.isDown(Phaser.Keyboard.RIGHT)) { user.x = user.x+4; } if (game.input.keyboard.isDown(Phaser.Keyboard.UP)) { user.y = user.y-4; } else if (game.input.keyboard.isDown(Phaser.Keyboard.DOWN)) { user.y = user.y+4; } }, collisionHandler: function() { console.log('COLLISION!'); } }; Link to comment Share on other sites More sharing options...
samid737 Posted October 23, 2017 Share Posted October 23, 2017 A couple of things noticeable: -Your using images instead of sprites. - Adding physics to group objects can be done by using game.add.physicsGroup(), enabling them on each body individually or by ysing enableBody, but not by enabling physics on the group: //you can do this laserBoundaries = this.add.group(); laserBoundaries.enableBody = true; //or the above in one line laserBoundaries = this.add.physicsGroup(); //but this won't have any effect. game.physics.enable(laserBoundaries, Phaser.Physics.ARCADE); var laser1 = game.add.sprite(650, 273, 'laser');//change to sprite var laser2 = game.add.sprite(650, 0, 'laser2');//change to sprite var laser3 = game.add.sprite(470, 0, 'laser2');//change to sprite var laser4 = game.add.sprite(72, 273, 'laser');//change to sprite - collisionHandler is called, but it is never defined (the global variable is not required, you have defined it within your state object). try calling the right function: console.log(collisionHandler); //will show undefined console.log(this.collisionHandler);//will show the correct one game.physics.arcade.overlap(user, laserBoundaries,this.collisionHandler); //this will work Link to comment Share on other sites More sharing options...
Recommended Posts