简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM