# Where to put input detector?

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()
{
base.Initialize();
}

{
_spriteBatch = new SpriteBatch(GraphicsDevice);
}

{
}

public override void Update(GameTime gameTime)
{
base.Update(gameTime);
}

public override void Draw(GameTime gameTime)
{
base.Draw(gameTime);
}


Thanks

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()) {
//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