How to continuously do an action as the user holds down their finger in XNA
I have an action for when the user holds down their finger, but how do I continuously do this action as the user holds down his finger? For example I want to: moveRight += 10; continuously while the user holds down their finger on the right part of the screen. Doe开发者_C百科s anyone know how to do this in XNA?
Use either the mouse input API (for easy, single-touch input on WP7), specifically check:
MouseState ms = Mouse.GetState();
if(ms.LeftButton == ButtonState.Pressed && ms.X > centerOfScreen)
{
DoSomething();
}
Or use the touch input API on WP7:
TouchCollection touches = TouchPanel.GetState();
foreach(TouchLocation touch in touches)
{
if((touch.State == TouchLocationState.Pressed
|| touch.State == TouchLocationState.Moved)
&& touch.Position.X > centerOfScreen)
{
DoSomething();
break;
}
}
Is there an action for when they release their finger? If so, then use a looping structure (game loop) and add some input state, rather than only responding to events.
- Set that state to "true" when you get the "hold" event
- Set it to "false" when you get the event stating that the "hold" event is over
- Check that boolean in your loop, and increment your x value (or x velocity) if it is set
Looking at The docs for Gesture Support for Windows Phone.
Maybe your "release" event that you want to handle is the ManipulationCompleted
event. I can't tell if it is Silverlight-only, or both Silverlight and XNA, though...
And if that fails, maybe there are lower level events you can handle.
Gestures are abstractions for entire sets of motion. The API is designed to do all the dirty work of figuring out what happened, and telling you that it did happen, not how it happened. If you want to have finer grain control than that, then you need a finer grained API.
See: http://msdn.microsoft.com/en-us/library/ff607287(v=VS.95).aspx (possibly the Touch class).
精彩评论