Subscribe

RSS Feed (xml)

Powered By

Powered by Blogger

Google
 
xnahelp.blogspot.com

Kamis, 03 April 2008

Working with mouse Devices

This input device is only available for Windows, so if we are deploying our game for the
Xbox 360 we will need to put the XBOX360 compilation directive check we learned about
in Chapter 2 around any code that references the mouse as an input device. So we will
create a private member field with this preprocessor check inside of our InputHandler
class. We need to set up a private member field to hold our previous mouse state and
another one to hold our current mouse state:
#if !XBOX360
private MouseState mouseState;
private MouseState prevMouseState;
#endif
Then in our constructor we tell XNA that we want the mouse icon visible in our window
and we store our current mouse state:
#if !XBOX360
Game.IsMouseVisible = true;
prevMouseState = Mouse.GetState();
#endif
In our Update method of the input handler game component we need to set the previous
state to what our current state is and then reset our current state as follows:
#if !XBOX360
prevMouseState = mouseState;
mouseState = Mouse.GetState();
#endif
98 CHAPTER 5 Input Devices and Cameras
Now we need to expose these internal fields so our camera (and any other) object can get
their values. First we need to add the properties to our interface as follows:
#if !XBOX360
MouseState MouseState { get; }
MouseState PreviousMouseState { get; }
#endif
Now we can implement those properties in our class:
#if !XBOX360
public MouseState MouseState
{
get { return(mouseState); }
}
public MouseState PreviousMouseState
{
get { return(prevMouseState); }
}
#endif
In our camera’s Update method we want to get the latest state of our mouse and compare
the current X value to the previous X value to determine if we moved the mouse left or
right. We also want to check if the left mouse button is pushed before updating our
cameraYaw variable. Of course, all of this is wrapped in our compilation preprocessor
condition as follows:
#if !XBOX360
if ((input.PreviousMouseState.X > input.MouseState.X) &&
(input.MouseState.LeftButton == ButtonState.Pressed))
{
cameraYaw += (spinRate * timeDelta);
}
else if ((input.PreviousMouseState.X < input.MouseState.X) &&
(input.MouseState.LeftButton == ButtonState.Pressed))
{
cameraYaw -= (spinRate * timeDelta);
}
#endif
We can compile and run the code and test the latest functionality on Windows. If the
preprocessor checks are in place correctly, we should be able to deploy this demo to the
Xbox 360 as well, although it will not do anything different than it did before we added
the mouse support.

0 komentar: