
One of the first “games” I made for the Zune HD was a simple little application to test how accurate and quick the touch panel is. This How To gives you the code to get started with your own basic testing application.
After creating a Zune game with XNA 3.1, the first thing you need to do is define a basic particle struct to hold some data that we track as the user touches the screen. Specifically we need to track whether or not a particle is active (do we update it and draw it?), its position and its life (how long it has been there). I created this structure within the Game1 class but really it can go anywhere.
private struct TouchSpot
{
public Vector2 Position;
public float Life, MaxLife;
public bool IsActive;
}
After creating the structure, two new private members need to be added to the game so we can actually, you know, do stuff.
private Texture2D texture;
private TouchSpot[] spots = new TouchSpot[2048];
Note: I found it useful to update the TargetElapsedTime property to reflect 60 frames per second rather than 30 frames per second. This allows the update routine to be run more often and therefore more particles to be created!
Next on the list is to load the texture. You can use any texture you want; I chose to use a simple circle that fades out.
texture = Content.Load("Touch");
Finally we get to the real meat of the game: the Update function. First we update each “particle” by decreasing its life and checking whether or not its “clock” has run out. This allows us to fade the particles out over time and creates a nice effect.
float et = (float)gameTime.ElapsedGameTime.TotalSeconds;
for (int i = 0; i < spots.Length; i++)
{
if (!spots[i].IsActive) continue;
spots[i].Life -= (et * 10);
if (spots[i].Life <= 0)
spots[i].IsActive = false;
}
Next we gather the touch points from the panel and reset particles in our array if possible. Note that we scale the pressure so that the particles don't instantly disappear.
TouchCollection touchPoints = TouchPanel.GetState();
foreach (TouchLocation location in touchPoints)
{
int index = FindInactiveSpot();
if (index == -1) break;
spots[index].Position = location.Position;
spots[index].Life = location.Pressure * 1000;
spots[index].MaxLife = location.Pressure * 1000;
spots[index].IsActive = true;
}
The Update method places a call to the FindInactiveSpot function which searches the array for particles that are no longer active and returns the index.
private int FindInactiveSpot()
{
for (int i = 0; i < spots.Length; i++)
{
if (!spots[i].IsActive)
return i;
}
return -1;
}
Last but certainly important is the draw method which draws all currently active particles. We divide the Life by the MaxLife to get a percentage of how "healthy" a particle is. This is an implementation of a fade out for each particle.
spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
for (int i = 0; i < spots.Length; i++)
{
if (!spots[i].IsActive) continue;
spriteBatch.Draw(
texture,
spots[i].Position,
null,
new Color(1f, 1f, 1f, spots[i].Life / spots[i].MaxLife),
0f,
new Vector2(16, 16),
spots[i].Life / spots[i].MaxLife * 1.5f,
SpriteEffects.None,
1f
);
}
spriteBatch.End();
And there you have it, a method for testing the capabilities of your Zune HD's touch panel!
Sample Code: Zune HD Touch Panel Testing (546)