Touch in XNA in Monogame

So unsurprisingly the initial version of Super Pong was rejected on a selection of grounds which means going back to deal with them one by one. Some of them have been small changes, while some have involved me learning new way of doing things.

Once I know I’ve solved the bugs and that my solutions have been approved I’ll talk about what I did to solve them, but their are a few thing I wanted to mention first; namely a website which has been a constant help for my fledgling attempts to get an app on the store, and the ability to use touch in my application.

The website which has been really helpful is http://blogs.msdn.com/b/bobfamiliar/archive/2012/08/07/windows-8-xna-and-monogame-part-3-code-migration-and-windows-8-feature-support.aspx. It’s a exceptional blog post for anyone trying to get a XNA game into Monogame and then into the store, with details about information that needs to go into trying to get an app on the store; from details about the Monogame content pipeline, to sizes of all the images that the store needs, to information on dealing with snapped apps in Windows 8.  I highly recommend using this website if you’re thinking of developing apps for the Windows 8 store.

As far as touch goes, it was considerably simpler than I had anticipated. Following a misleading post I found which said mouse counted as touch (it doesn’t), I discovered that for XNA for Windows 8 using Monogame, firstly I had to add

 using Microsoft.Xna.Framework.Input.Touch;  

Which allows for the use of touch inputs for Windows Phones as well as tablets and desktops. Then I had to add a global variable:

 TouchCollection touchCollection; 

This “TouchCollection” holds all of the touches on screen, allowing coding for multiple touch inputs later in development, as well as just for one. To get the touches into the TouchCollection you need to call:

 touchCollection = TouchPanel.GetState(); 

For the instances when I need to check if something has been touched, I use a simple foreach loop;

 foreach (TouchLocation tl in touchCollection)  
 {  
 }  

tl giving options such as State, Position, Pressure and ID. With this I built a method which I use to detect if a touch is within certain bounds, such as a button or just the screen:

 bool touchSelect(Rectangle target)  
 {  
   foreach (TouchLocation tl in touchCollection)  
   {  
     if (tl.State == TouchLocationState.Pressed &&  
       tl.Position.X > target.Left &&  
       tl.Position.X < target.Right &&  
       tl.Position.Y > target.Top &&  
       tl.Position.Y < target.Bottom)  
     {  
       return true;  
     }  
   }  
   return false;  
 }  

Certain situations need me to specify where the touch actually is and whether it is continuous or just pressed, but as a rule of thumb the above method works very well.

I hope this was helpful,

Adam

Advertisements

3 thoughts on “Touch in XNA in Monogame

Add yours

  1. Good stuff. Bob Familiar does do some good stuff. One tip, you might like to format your code segments in Courier or some other monospaced font so that they stand out from the text. Not sure how you write your blog posts, but I use Live Writer, which has quite a nice code plugin which will format code samples and even does syntax highlighting.

    Anyhoo, keep up the good work.

  2. this code are more helpfull!

    private bool CheckRectangleTouch(Rectangle target, TouchCollection touchCollection)
    {
    if (touchCollection.Count > 0)
    {
    foreach (var touch in touchCollection)
    {
    if (target.Contains(touch.Position))
    {
    return true;
    }
    }
    }
    return false;
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Powered by WordPress.com.

Up ↑

%d bloggers like this: