SoundSaturation

Im using a sound effect to shoot in my game, and the problem is that when I shoot like 45 times the game begins to saturate, and the lag make it unplayable. I checked my code, and the problem is with the sound, without it I can shoot 100 times without problems. What could be the problem ? The sound is 3 seconds long. To play it I have something like this.

let sound;
let Shoots = [];
function preload() { 
sound = loadsound("shootSound.mp3);
}
function setup(){
createCanvas(1000, 650);
}
function draw(){
  for(shoots of Shoots){
   shoots.show();
   shoots.move();  
 }
}
function keyPressed(){
  if(keyCode === 32){
    Shoots.push(new Bullet());
    sound.play();
  }
}

Is it a common problem ? Or there is something wrong in my game code ? Thankss

Tagged:

Answers

  • edited April 2018 Answer ✓

    "Sounds" strange to me too. Perhaps ask them directly: :-??
    https://GitHub.com/processing/p5.js-sound/issues

    Some observations though: L-)

    • By Java & JS conventions, we don't capitalize the 1st letter of a variable.
    • Unless it refers to a constructor function or class. Or it's immutable.
    • So your array should be named shoots instead of Shoots.
    • And in your for (shoots of Shoots) { loop, the 1st variable (the iterator) represents 1 element of the 2nd variable (the container); so it should have a singular name: for (const shoot of shoots) {
    • Notice you've also forgotten to declare the iterator shoot as a local variable.
    • Therefore shoot became a global variable instead!
    • In order to avoid such logical issues, place "use strict"; before the 1st statement for all of your ".js" files: https://Developer.Mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode
  • You right, I forgot to declare de shoots in the loop. I will take the other advices ! I tried this :

    let x;
    let vel;
    let sound;
    function preload(){
      sound = loadSound("disparosMagia.mp3");
    }
    function setup(){
      createCanvas(1000, 650);
      x = 100;
      vel = 1;
    }
    
    function draw(){
      background(200);
      ellipse(x, 300, 200, 200);
      x+= vel * 5;
      if(x > 900 || x < 100){
        vel *= -1;
      }
    
    }
    function keyPressed(){
      sound.play();
    
    }
    

    And the lag still appear when I press any key more than 35 times (average), the numer depend on the computer though. I´ll ask them ! Thanks

  • The problem was comented in a older thread

    https://github.com/processing/p5.js-sound/issues/88

    From what I understood, the problem only occurs in Firefox, and because the thread is old I am not sure if they solved it or not, so I decided to develop the game in chrome, where the problem dont happen. Thanks !

  • Thxs for opening the tix in github and sharing your solution.

    Kf

Sign In or Register to comment.