Windows Phone 7 example game Platformer practice 5-multi-touch Programming

Source: Internet
Author: User

Even experienced XNA programmers have to learn the new implementation of multi-touch when starting game development on Windows Phone 7. Although some Windows Phone 7 mobile phones are now equipped with a keyboard, it is best for our games to adopt the multi-touch input method to ensure compatibility with all WP7 mobile phones.

 

Currently, popular games on Windows Phone 7 include the "Assassin's Creed", "need for speed", and "Babylon brothers" that use touch and acceleration sensors. This is a brand new mode for programmers on the Windows platform.

 

Assassin's Creed uses multi-touch to implement hero's movement, attack, and weapon Switching

Windows Phone 7 uses a capacitive screen that supports at least four contacts. It is no longer as simple as simulating and recognizing a pen. How to Deal with multi-touch has become a new challenge for developers. In XNA programming, multi-touch input is detected in the Update () method of XNA. One of the main purposes of the Update () method is to process multi-touch input, and Draw the changes caused by the input in the Draw () method on the screen.

Processing of underlying multi-touch input in XNA

 

In XNA, a multi-Touch device is abstracted as a Touch Panel. You can use the method in the static class TouchPanel to obtain contact information. Although multi-touch also supports Gesture Recognition such as sliding, we can start learning from the underlying touch input processing.

 

We can use the TouchPanel. GetCapabilities () method to obtain some features of the touch screen. The TouchPanelCapabilities object returned by this method contains two attributes. They are as follows:

 

IsConnected: Check whether it is connected to a touch screen device. Because WP7 phones always have touch screens, this attribute always returns true.

MaximumTouchCount: This attribute returns the maximum number of contacts supported by the WP7 mobile Phone capacitive screen. For Windows Phone 7, the minimum value of this attribute is 4, that is, a minimum of four contacts are supported at the same time.

 

Generally, we only need to use two static methods in the TouchPanel. To obtain the underlying touch input information, you can use the following method in the Update () method:

 

TouchCollection touchLocations = TouchPanel. GetState ();

 

The TouchPanel. GetState () method returns a set of contacts that contain 0 or more TouchLocation objects. The TouchLocation object contains three important attributes:

 

State: this attribute is an enumeration member of TouchLocation. It contains three states: Pressed, Moved, and Released. Whether the current contacts are pressed, moved, and released.
Position: This attribute is a Vector2 vector coordinate used to confirm the Coordinate Position of the current contact relative to the upper left corner.

Id: This attribute is an Int integer that indicates the status from press to release.Finger.

 

If no finger is pressed on the touch screen, the TouchCollection is empty. Once a finger is in touch with the touch screen, the TouchCollection will contain a TouchLocation object whose State attribute is Pressed.

 

In the next TouchPanel. GetState () method call, the TouchLocation State attribute will be changed to Moved even if your fingers are not Moved. Once your finger leaves the touch screen, the TouchLocation State is uncertain and the status is Released. In the next call to the TouchPanel. GetState () method, the TouchCollection will be empty.

 

There is also a special case, that is, when you press your finger on the touch screen to release in 1/30 seconds, the TouchLocation State attribute will be changed from Pressed to Released directly, skipping the Moved process. It is estimated that few fingers can be so fast on the touch screen, 1/30 seconds? Hey, why don't you try it.

 

In general, we may use multiple fingers to perform various operations on the WP7 capacitive screen. Each finger can implement the process of pressing, moving, and releasing independently.

 

You need to use the TouchLocation. Id attribute mentioned above to identify multiple fingers for tracking the corresponding status. The finger on each touch screen is marked with a specific Id to confirm that the finger is in the Pressed, Moved, and Released states.

 

TouchLocation also has a very useful trygetpreviuslocation () method. Call the code:

 

TouchLocation previousTouchLocation;
Bool success = touchLocation. trygetpreviuslocation (out previousTouchLocation );

 

We can call this method when touchLocation. State is equal to Moved, so that you can obtain the previous contact position and calculate the distance between finger movement. If touchLocation. State is equal to Pressed, The trygetpreviuslocation () method returns false, and the value of previousTouchLocation. State is TouchLocationState. Invalid.

So far, we have a general understanding of the implementation and programming methods of multi-touch on WP7. In Platformer games, hero movement and jumping are controlled by the accelerometer and touch screen respectively. When you click on the mobile phone screen, the hero will jump, and the left and right sides will tilt the mobile phone, the accelerator will determine whether the hero is moving left or right.

 

The following describes the multi-touch Encapsulation Method in Platformer games. The specific use of xuanyuan will be highlighted in the subsequent article on game scenario rendering. The Code is as short as ever:

 

1 /// <summary>
2 // extension of TouchCollection
3 /// </summary>
4 public static class TouchCollectionExtensions
5 {
6 /// <summary>
7 // determine whether the touch screen has contacts
8 /// </summary>
9 /// <param name = "touchState"> current TouchCollection set </param>
10 /// <returns> If the contact status is Pressed or Moved, true is returned. Otherwise, false is returned. </returns>
11 public static bool AnyTouch (this TouchCollection touchState)
12 {
13 foreach (TouchLocation location in touchState)
14 {
15 if (location. State = TouchLocationState. Pressed | location. State = TouchLocationState. Moved)
16 {
17 return true;
18}
19}
20 return false;
21}
22}

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.