I've recently made a topdown shooter in P5.js (still new). However, this function seems to cause a memory leak, making all audio sound glitchy and slightly decreasing the FPS. The sounds play normally if I pause the "draw()" loop.
The problem is definitely caused by the sounds, as when I remove the code for playing prepareSound, the problem occurs after significantly more time.
function shoot() {
if (reloadLeft <= 0 && preparingLeft <= 0) {
shotSound.play();
tmp = dirTo(playerX,playerY, mouseX+cameraX-offsetX, mouseY+cameraY-offsetY);
for (let i=0; i<bulletCount; i++) {
bullets.push(new Bullet (playerX-stepsX(tmp,recoil),playerY-stepsY(tmp,recoil), tmp))
}
recoilLeft += recoil;
light += (16-sqrt(muzzleFlash))*6;
if (ammoLeft <= 1) {
reloadWeapon()
}
else {
ammoLeft -= 1;
preparingLeft = fireRate;
prepareSound.play();
}
}
}
On most weapons, prepareSound is a blank file. I only made it for ones such as the revolver and shotgun.
I have tried restarting the sound instead of playing it again with "jump" when it's over, however it didn't fix the issue. It still occured after about the same amount of time.