TaricDF
TaricDF

Reputation: 59

C# - How to do a volume slider

I am making a game using the XNA 4.0 Framework. I have been attempting to make a volume but I was not able to figure it out. The main problem is how to determine the length of the slider to properly represent the volume(0.0f - 1.0f). I mainly just need advice on how to clean it up. Here my code for the current slider that I have:

private void VolSlider(int moveSpeed)
    {
        if (kb.IsKeyDown(Keys.PageUp) && prevKb.IsKeyDown(Keys.PageUp))
        {
            buttonSliderRec.X += moveSpeed;
            MediaPlayer.Volume = Math.Min(1.0f, MediaPlayer.Volume + 0.001f);
        }
        if (kb.IsKeyDown(Keys.PageDown) && prevKb.IsKeyDown(Keys.PageDown))
        {
            buttonSliderRec.X -= moveSpeed;
            MediaPlayer.Volume = Math.Min(1.0f, MediaPlayer.Volume - 0.001f);
        }

        //Resets the volume to default
        if (mouse.LeftButton == ButtonState.Pressed && mouse.X >= 494 && mouse.X <= 720 &&
            mouse.Y >= 163 && mouse.Y <= 230)
        {
            MediaPlayer.Volume = 0.5f;
            YouLoseInst.Volume = 0.5f;
            YouWinInst.Volume = 0.5f;
            onClickInst.Volume = 0.5f;
        }
    }

I call this subprogram in Update inside a switch statement. I was just wondering how to properly make the slider represent the Volume function and advice on how to fix up my code shown up above.

Upvotes: 0

Views: 1148

Answers (1)

Monacraft
Monacraft

Reputation: 6630

Here is an answer elaborating on how to make a slider using sprites.

For this you would require two sprites:

  1. A line/bar representing the limits of the volume
  2. A small button that is drawn on the line representing current volume

Update Logic

Have it so that if:

a. The user hits the keyboard buttons the increase/lower volume, you appropriately change it.

b. If the user clicks on the line sprite, it set's the volume based on where on the sprite they clicked on (get mouse.X and subtract the far left x-val of the volume bar, then divide by total length of vol bar for a percentage)

(Note, you would not need to worry about them clicking and dragging the button sprite, since they would indirectly click on the volume bar so it would just use that code)

Draw Logic

Draw the button sprite completely relative to the line sprite, so that they are scaled equally.

  1. The y-value is always the center of the line sprite

  2. The x-value is the far let x-val of the line sprite + the current volume (As a percentage) * the width of the line-sprite.

Notice you require the far left x-val of the line sprite for both getting and setting the volume. This way, the volume of the game could be controlled via key-presses, dragging the bar and clicking anywhere on the bar. It also means that the position of the bar is always the actual volume, allowing people to check if it is in fact working.

I haven't provided any code examples for this but would be more than happy to (you'd have to give me a bit of time to write it up)

Upvotes: 1

Related Questions