Help playBouncedSound spams me

Hello, I’m using this code to try to generate 4 sprites that have a collision to simulate like a candy box being shaken (using the gyroscope).
It works more or less, however the “playBouncedSound” function which allows me to play a sound, spams me with messages/sounds when the sprites are close. How to fix it please?

window.onload = function() {

	// game definition, 320x480
	var game = new Phaser.Game("100%","100%",Phaser.CANVAS,"",{preload:onPreload, create:onCreate, update:onUpdate});                

     // the player
     var player

     // function executed on preload
	function onPreload() {
	    	game.load.image("player","player.png");	
	}
     
     function onUpdate() {
          
          game.physics.arcade.collide(player, player2);
          game.physics.arcade.collide(player2, player3);
          game.physics.arcade.collide(player3, player);
          game.physics.arcade.overlap(player, player2, playBouncedSound, null, this);
          game.physics.arcade.overlap(player2, player3, playBouncedSound, null, this);
          game.physics.arcade.overlap(player3, player, playBouncedSound, null, this);
          function playBouncedSound() {
               console.log('play sound...');
          }
     }

	// function to scale up the game to full screen
	function goFullScreen(){
		game.scale.pageAlignHorizontally = true;
		game.scale.pageAlignVertically = true;
		game.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;
		game.scale.setScreenSize(true);
	}

	// function to be called when the game has been created
	function onCreate() {
          // initializing physics system
          game.physics.startSystem(Phaser.Physics.ARCADE);
		// going full screen
          goFullScreen();
          // adding the player on stage
          var TmpImg = game.cache.getImage('player');
          
          player = game.add.sprite(game.world.centerX - TmpImg.width/4.0,game.world.centerY - TmpImg.height/4.0,"player");
          player.width = player.width *2;
          player.height = player.height *2;
          // setting player anchor point
          player.anchor.setTo(1);
          // enabling physics car.body.collideWorldBounds = true;
          game.physics.enable(player, Phaser.Physics.ARCADE);
          // the player will collide with bounds
          player.body.collideWorldBounds = true;
          // setting player bounce
          player.body.bounce.set(0.8);
	     // setting gyroscope update frequency
          gyro.frequency = 1;
		// start gyroscope detection
          gyro.startTracking(function(o) {
               // updating player velocity
               player.body.velocity.x += o.gamma/20;
               player.body.velocity.y += o.beta/20;
          });	
          
          player2 = game.add.sprite(game.world.centerX+100 - TmpImg.width/4.0,game.world.centerY - TmpImg.height/4.0,"player");
          player2.width = player2.width *2;
          player2.height = player2.height *2;
          // setting player2 anchor point
          player2.anchor.setTo(1);
          // enabling physics car.body.collideWorldBounds = true;
          game.physics.enable(player2, Phaser.Physics.ARCADE);
          // the player2 will collide with bounds
          player2.body.collideWorldBounds = true;
          // setting player2 bounce
          player2.body.bounce.set(0.8);
	     // setting gyroscope update frequency
          gyro.frequency = 1;
		// start gyroscope detection
          gyro.startTracking(function(o) {
               // updating player2 velocity
               player2.body.velocity.x += o.gamma/20;
               player2.body.velocity.y += o.beta/20;
          });
          
          player3 = game.add.sprite(game.world.centerX-100 - TmpImg.width/4.0,game.world.centerY - TmpImg.height/4.0,"player");
          player3.width = player3.width *2;
          player3.height = player3.height *2;
          // setting player3 anchor point
          player3.anchor.setTo(1);
          // enabling physics car.body.collideWorldBounds = true;
          game.physics.enable(player3, Phaser.Physics.ARCADE);
          // the player3 will collide with bounds
          player3.body.collideWorldBounds = true;
          // setting player3 bounce
          player3.body.bounce.set(0.8);
	     // setting gyroscope update frequency
          gyro.frequency = 1;
		// start gyroscope detection
          gyro.startTracking(function(o) {
               // updating player3 velocity
               player3.body.velocity.x += o.gamma/20;
               player3.body.velocity.y += o.beta/20;
          });
	}
}

image

Pass playBouncedSound to collide(…) instead of overlap(…).


Thank you for your answer but it doesn’t seem to work, when they touch, the message keeps getting spammed

     function onUpdate() {
          
          game.physics.arcade.collide(player, player2);
          game.physics.arcade.collide(player2, player3);
          game.physics.arcade.collide(player3, player);
          game.physics.arcade.collide(player, player2, playBouncedSound, null, this);
          game.physics.arcade.collide(player2, player3, playBouncedSound, null, this);
          game.physics.arcade.collide(player3, player, playBouncedSound, null, this);
          function playBouncedSound() {
               console.log('play sound...');
          }
     }

This worked as expected for me:

function playBouncedSound() {
  console.log("play sound...");
}

function update() {
  game.physics.arcade.collide(player, player2, playBouncedSound, null, this);
  game.physics.arcade.collide(player2, player3, playBouncedSound, null, this);
  game.physics.arcade.collide(player3, player, playBouncedSound, null, this);
}

But I also removed the gyro code.

Are the sprites bouncing off each other?

I need the gyroscope to use with my phone.
But yes the sprites bounce between them until they are close or blocked.

If the sprites are touching then the callback will get called. What do you want to happen instead?

I really want to simulate a box of candy. So if they touch each other it should make a noise but if they don’t move (even if they touch each other), the callback shouldn’t work.

You could try something like

function playBouncedSound(sprite1, sprite2) {
  if (sprite1.body.speed > 10 || sprite2.body.speed > 10) {/*…*/}
}