qubedozzer Posted May 28, 2015 Share Posted May 28, 2015 Hi, I have a small ARCADE gamegame.physics.startSystem(Phaser.Physics.ARCADE); and if a sprite bouncing the world should play a sound... player.body.collideWorldBounds = true; how can I use this? Link to comment Share on other sites More sharing options...
qubedozzer Posted May 28, 2015 Author Share Posted May 28, 2015 hmm - this not work......game = new Phaser.Game(320,480,Phaser.CANVAS,"playground",{preload:onPreload, create:onCreate, update:onUpdate}, true); ...player = game.add.sprite(160,240,"player");...function onUpdate() { game.physics.arcade.collide(player, game); game.physics.arcade.overlap(player, game, playBouncedSound, null, this); function playBouncedSound(player, game) { console.log('play sound...'); } } Link to comment Share on other sites More sharing options...
fariazz Posted May 29, 2015 Share Posted May 29, 2015 When a sprite hits the edges of the world, the sprite's "blocked" object is set to true according to the side of the sprite that hit the edge of the world, so you could listen in update for this and play a sound: creating the sprite with the checks and bounce properties:this.bouncingSprite = this.add.sprite(10, 10, 'key');//enable arcade physicsthis.game.physics.arcade.enable(this.bounchingSprite);//check world boundsthis.bouncingSprite.body.collideWorldBounds = true;//bouncing behaviorthis.body.bounce.set(1,1);Then on update:if(this.bouncingSprite.body.blocked.up || this.bouncingSprite.body.blocked.down || this.bouncingSprite.body.blocked.left || this.bouncingSprite.body.blocked.right) {//play sound here}This is of course you don't have tile sprites in your level, as those will also trigger "blocked" to true. Link to comment Share on other sites More sharing options...
fariazz Posted May 29, 2015 Share Posted May 29, 2015 Another option would be to create invisible rectangles on the 4 bounds of your world and check collide between your sprite and them, and play a sound on the collision callback. Link to comment Share on other sites More sharing options...
qubedozzer Posted May 29, 2015 Author Share Posted May 29, 2015 Thanks for your code!! withif(player.body.blocked.up || player.body.blocked.down || player.body.blocked.left || player.body.blocked.right) { console.log('play sound <.:| ');}works fine... at the next, I look at the velocity of "player" and set the volume of sound like a table tennis ball fariazz 1 Link to comment Share on other sites More sharing options...
Recommended Posts