1
\$\begingroup\$

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(); 
}

protected override void LoadContent()
{
    _spriteBatch = new SpriteBatch(GraphicsDevice);
    base.LoadContent();
}

protected override void UnloadContent()
{
    base.UnloadContent();
}

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

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

Thanks

\$\endgroup\$
4
\$\begingroup\$

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));

    }
}
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – Moses Aprico May 15 '13 at 16:32
  • \$\begingroup\$ 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. \$\endgroup\$ – Moses Aprico May 15 '13 at 17:34
  • \$\begingroup\$ True enough, I've modified the answer to fit with Touch instead of Keyboard. \$\endgroup\$ – MichaelHouse May 15 '13 at 17:47
  • \$\begingroup\$ 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. \$\endgroup\$ – Moses Aprico May 18 '13 at 13:24
  • \$\begingroup\$ Since your draw and update loops are different, you just add whatever you want to draw to the world and the draw loop picks it up the next time it processes (instantly as far as the user is concerned). So to draw a Texture2D, you have a list of Texture2D objects that your draw loop iterates through every loop. When a touch is detected, you add an item to that list. \$\endgroup\$ – MichaelHouse May 18 '13 at 14:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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