locked
Left and right side of the screen RRS feed

  • Question

  • Hi everyone, 

    I was wondering how you can divide the screen in half (in XNA) so that you can read touch input from the left and right side of the screen. 

    Thanks to anyone who helps!
    Tuesday, January 18, 2011 8:15 PM

Answers

  • You can't do it automatically but yes, you can definitely code it to detect this. All you'd have to do is define rectangular areas for the different sides of the screens and then check to see if the touch input occurred in the left of the right side (or the top or the bottom depending on how you define them). Really not that hard to code since XNA's Rectangle has a .Contains() method and the touch input can return a touch point (Vector2)
    Tuesday, January 18, 2011 8:34 PM
  • This would be a basic implementation:
                TouchCollection touches = TouchPanel.GetState();  
                Point[] touchPoints = new Point[touches.Count];  
     
     
                // Loop through each point  
                for (int i = 0; i < touches.Count; i++)  
                {  
                    // convert the touch position into a Point for hit testing  
                    touchPoints[i] = new Point((int)touches[i].Position.X, (int)touches[i].Position.Y);  
     
                    if (touchPoints[i].X < GraphicsDevice.Viewport.Width / 2) System.Diagnostics.Debug.WriteLine("touching left side of screen");  
                    if (touchPoints[i].X > GraphicsDevice.Viewport.Width / 2) System.Diagnostics.Debug.WriteLine("touching right side of screen");  
                }  
     
    Put that in your Update() function and it should tell you which half of the screen you are touching.
    Tuesday, January 18, 2011 8:46 PM

All replies

  • You can't do it automatically but yes, you can definitely code it to detect this. All you'd have to do is define rectangular areas for the different sides of the screens and then check to see if the touch input occurred in the left of the right side (or the top or the bottom depending on how you define them). Really not that hard to code since XNA's Rectangle has a .Contains() method and the touch input can return a touch point (Vector2)
    Tuesday, January 18, 2011 8:34 PM
  • This would be a basic implementation:
                TouchCollection touches = TouchPanel.GetState();  
                Point[] touchPoints = new Point[touches.Count];  
     
     
                // Loop through each point  
                for (int i = 0; i < touches.Count; i++)  
                {  
                    // convert the touch position into a Point for hit testing  
                    touchPoints[i] = new Point((int)touches[i].Position.X, (int)touches[i].Position.Y);  
     
                    if (touchPoints[i].X < GraphicsDevice.Viewport.Width / 2) System.Diagnostics.Debug.WriteLine("touching left side of screen");  
                    if (touchPoints[i].X > GraphicsDevice.Viewport.Width / 2) System.Diagnostics.Debug.WriteLine("touching right side of screen");  
                }  
     
    Put that in your Update() function and it should tell you which half of the screen you are touching.
    Tuesday, January 18, 2011 8:46 PM
  • Wow thanks so much for the help everyone!
    Tuesday, January 18, 2011 10:14 PM