user2640299
user2640299

Reputation: 35

xna zoom using mouse scroll wheel

How do i zoom using the mouse scroll wheel, i got try this

        if (currentMouseState.ScrollWheelValue < originalMouseState.ScrollWheelValue)
        {
            cameraPosition += new Vector3(0, -1, 0);
            UpdateViewMatrix();
            currentMouseState.ScrollWheelValue.Equals(0);
        }
        if (currentMouseState.ScrollWheelValue > originalMouseState.ScrollWheelValue)
        {
            cameraPosition += new Vector3(0, 1, 0);
            UpdateViewMatrix();
            currentMouseState.ScrollWheelValue.Equals(0);
        }

But i keep zooming in even if i scroll once and i am kinda new to XNA. Please help.

Upvotes: 2

Views: 8140

Answers (1)

pinckerman
pinckerman

Reputation: 4213

ScrollWheelValue gets the cumulative mouse scroll wheel value since the game was started, so every time you get it you need to copy that value in a variable, in order to compare it the next cycle.

Moreover, you can't set ScrollWheelValue, and this line is wrong:

currentMouseState.ScrollWheelValue.Equals(0);

I think your idea was to set its value as 0, but that instruction compares its value with 0 and gives you a boolean.

EDIT:
You should do something like this:

Declare a global variable

private int previousScrollValue;

And set it in the Initialize method as:

previousScrollValue = originalMouseState.ScrollWheelValue;

Then edit your code as this:

if (currentMouseState.ScrollWheelValue < previousScrollValue)
{
    cameraPosition += new Vector3(0, -1, 0);
    UpdateViewMatrix();
}
else if (currentMouseState.ScrollWheelValue > previousScrollValue)
{
    cameraPosition += new Vector3(0, 1, 0);
    UpdateViewMatrix();
}
previousScrollValue = currentMouseState.ScrollWheelValue;

It should work.

Upvotes: 7

Related Questions