Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

So below is the usual XNA Game Component override method. If I want to add a touch input detector (lets say, I want to draw a rectangle under touched screen area), where do I put it and how? Googled it, but I found no satisfying solutions.

public override void Initialize() 

protected override void LoadContent()
    _spriteBatch = new SpriteBatch(GraphicsDevice);

protected override void UnloadContent()

public override void Update(GameTime gameTime)

public override void Draw(GameTime gameTime)


share|improve this question
up vote 4 down vote accepted

You check for input in your Update method.

  • Update is where you'll respond to input, move entities around, do collision detection, update counters, update and data that needs updating.
  • Draw is where you draw everything...
  • Initialize is where you set initialize whatever data structures you're using.
  • LoadContent is where you load save states, load textures/models/sounds, etc.
  • UnloadContent is where you unload all the stuff you loaded in LoadContent.

As for how, you can get the touch state:

//inside update at the start of the function
TouchCollection touchState = TouchPanel.GetState();

Then you can perform actions based off the touch locations:

//inside update
foreach (TouchLocation t in touchState)
    //do something with t.Position

Or, if you wanted to check for gestures and perform an action based off that:

//inside update
if(touchState.IsGestureAvailable()) {
    GestureSample gesture = touchState.ReadGesture();
    //do something with gesture

The GestureSample (doc) will hold information about position, deltas and gesture type. These values will help you decide what type of action to perform and how to perform it.

Try following a small tutorial like this one to get you started. The code is very similar to what I wrote:

TouchCollection touchCollection = TouchPanel.GetState();
foreach (TouchLocation tl in touchCollection)
    if ((tl.State == TouchLocationState.Pressed)
            || (tl.State == TouchLocationState.Moved))

        // add sparkles based on the touch location
        sparkles.Add(new Sparkle(tl.Position.X,
                 tl.Position.Y, ttms));

share|improve this answer
And how to do it? Can you give a sample code in the update? Sorry, because so far, my search haven't find anything good. – Moses Aprico May 15 '13 at 16:32
Hmmm. Sorry, this is useful for sure and can make me understand the principal, but, WP8 doesn't have keyboard input. And also I've stated in my question that the input I want is Touch. – Moses Aprico May 15 '13 at 17:34
True enough, I've modified the answer to fit with Touch instead of Keyboard. – Byte56 May 15 '13 at 17:47
Okay, I'll try it soon enough, thanks. – Moses Aprico May 17 '13 at 1:49
Hmm. sorry but, I still don't know how to apply it. Can you give me an example to, lets say, draw a Texture2D on a tapped coordinate? I know how to draw a Texture2D, but doesn't know how to draw WHEN there is a tap committed. Thanks. – Moses Aprico May 18 '13 at 13:24

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.