How to prevent overlapping of gunshot sounds when using fast-firing weapons

Posted by G3tinmybelly on Game Development See other posts from Game Development or by G3tinmybelly
Published on 2014-06-08T01:08:15Z Indexed on 2014/06/10 15:44 UTC
Read the original article Hit count: 188

Filed under:
|
|

So I am now trying to find sounds for my guns but when I grab a gun sound effect and play it in my game a lot of the sounds are either terrible sounding or have this horrible echoing effect because as a gun shoots sometimes the previous sound is playing still.

public void shoot(float x, float y, float direction){

    if(empty){
        PlayHUD.message = "No more bullets!";
        return;
    }

    if(reloading){
        return;
    }

    if(System.currentTimeMillis() - lastShot < fireRate){
        //AssetsLoader.lmgSound.stop();
        return;
    }

    float dx = (float) (-13 * Math.cos(direction) + 75 * Math.sin(direction));
    float dy = (float) (-14 * -Math.sin(direction) + 75 * Math.cos(direction));

    float dx1 = (float) (-13 * Math.cos(direction) + 75 * Math.sin(direction));
    float dy1 = (float) (-14 * -Math.sin(direction) + 75 * Math.cos(direction));

    PlayState.effects.add(new MuzzleFlashEffect(x + dx1, y + dy1, (float) Math.toDegrees(-direction)));

    PlayState.projectiles.add(new Bullet(this, x + dx, y + dy, (float) (direction + (Math.toRadians(MathUtils.random(-accuracy, accuracy))))));

    if(OptionState.soundOn){
        AssetsLoader.lmgSound.play(OptionState.volume);
    }

    bulletsInClip--;
    lastShot = System.currentTimeMillis();

}

Here is the code for where the sound plays. Every time this method is called the sound is called but it happens so often in this case that there is this terrible echoing. Any idea on how to fix this?

© Game Development or respective owner

Related posts about libgdx

Related posts about sound