XNA 4.0: Planet Boy responds to multi-touch on the Win Phone emulator

In my past few posts, I have shown how to create a really, really simple game, it isn’t all that fun, but with the exception of scoring and writing on the screen, it shows most of the simple elements that can be used in a game. 

After an excellent online talk by the dynamic and organized Dan Waters (a compliment I can safely state since it is very unlikely he reads my blog, I wouldn’t want him to get a big head), I realized that I need to change my general approach to use the multi-touch API, instead of the Mouse.

It could be (note: my opinion) that Mouse Input may not handled in the future releases of Win Phone.  Using the code from BoundingBox.Intersects versus rectangle.Intersect blog, make the following modifications

  • comment the HandleInput line in the Update method
  • Add the method call “HandleInputMultiTouch();”
  • No changes to the method CollisionTest() method
  • No changes to the “HandleInput” method (you can delete it, but I may use it in future posts)
  • Add the method “HandleInputMultiTouch()” method
  • When you successfully make the changes, PlanetBoy will move, not PinkGirl
  • If you don’t comment the call to HandleInput, when you use the mouse PinkGirl and PlanetBoy will stick together
  • Note that the TouchLocation X and Y coordinates are floats (which means they will work directly with vectors), so you need to cast the float to int


//Start code

protected override void Update(GameTime gameTime)

  private void CollisionTest()
      if (r_PinkGirl.Intersects(r_PlanetBoy))
          b_collision = true;

  private void HandleInput()
      MouseState currentMouse = Mouse.GetState();
      r_PinkGirl.X = currentMouse.X;
      r_PinkGirl.Y = currentMouse.Y;

  private void HandleInputMultiTouch()
      TouchCollection touchCollection = TouchPanel.GetState();
      foreach (TouchLocation touchLoc in touchCollection)
          if ((touchLoc.State == TouchLocationState.Pressed) ||
              (touchLoc.State == TouchLocationState.Moved))
              r_PlanetBoy.X = (int)touchLoc.Position.X;
              r_PlanetBoy.Y = (int)touchLoc.Position.Y;                   

//end code

Comments (0)

Skip to main content