Jump to content

play sound at bouncing collideWorldBounds


qubedozzer
 Share

Recommended Posts

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

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

Thanks for your code!!

 

with

if(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

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...