XNA for WP7 and multitouch update every frame - c#

I'm having trouble with touch/multitouch input.
I want to draw a small rectangle, 100x100 in dimensions, wherever the user presses (mission accomplished) but I also want them to move as the user moves his fingers (that's not happening atm).
I'm also getting weird behaviour besides the not-moving part, let's say I touch first with my thumb, then with my middle finger. Two cubes appear bellow each finger, but if I remove the finger I place first (thumb in this scenario) the cube under the finger I placed second (middle finger) will disappear and the one where my thumb was will still be there. I guess this issue will solve itself once I get this to update correctly whenever there is movement.
This are the Draw and Update snippets. Any help appreciated:
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
TouchCollection touchLocations = TouchPanel.GetState();
i = 0;
foreach (TouchLocation touchLocation in touchLocations)
{
if (touchLocation.State == TouchLocationState.Pressed)
{
pos[i] = touchLocation.Position;
}
i++;
}
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
for (j = 0; j < i; j++)
{
spriteBatch.Draw(whiteRectangle, new Rectangle(((int)pos[j].X - 50), ((int)pos[j].Y - 50), 100, 100), Color.Chocolate);
}
spriteBatch.End();
base.Draw(gameTime);
}

I would recommend using a Dictionary assuming you want to associate touch locations to their position. The key should be the TouchLocation.Id
TouchLocation.Id will remain the same for a given interaction. There is no guarantee that the order of TouchLocations will be the same from one frame to another, so you have to use their ID and not the order they appear in the collection.

I'm a bit late on this, but well here is what was wrong and how I solve it...
foreach (TouchLocation touchLocation in touchLocations)
{
if (touchLocation.State == TouchLocationState.Pressed)
{
pos[i] = touchLocation.Position;
}
i++;
}
I guess I was sleepy when I wrote this part of the code (nothing good happens after 2 AM... not even good code) I don't know why I was checking if the location state was pressed... that's why it didn't move when I move... first frame it is indeed pressed but once you start moving it it's .Moved ... all in all, removed that if and it works like a charm...
foreach (TouchLocation touchLocation in touchLocations)
{
pos[i] = touchLocation.Position;
i++;
}
All in all now it behaves as intended.
Cheers!

Related

XNA Point Counter

I am making a game where a player runs around collecting Kiwis. I want the point counter to increment by only one each time. What I have so far is that the pointer increments, but keeps incrementing. i.e. when the player touches a kiwi the counter keeps on increasing by 1.
Here is the Intersect method:
public void Intersect(Rectangle playerRect, SpriteBatch spriteBatch)
{
foreach (Rectangle kiwiRect in kiwiRectangle.Except(collectedKiwis))
{
if (kiwiRect.Intersects(playerRect))
{
collectedKiwis.Add(kiwiRect);
isCollected = true;
}
else
{
spriteBatch.Draw(kiwiTexture, kiwiRect, Color.White);
}
}
}
And here are the Update and Draw methods:
public void Update()
{
points = "Points: " + counter;
if (isCollected)
{
counter++;
}
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.DrawString(pointCounter, points, new Vector2(500, 10), Color.Red);
}
Well, you should check for 2 conditions at least:
If player is currently intersected with kiwi
If that kiwi hasn't already been collected (isCollected prop i suppose)
So if you are intersected and kiwi isn't yet collected, you set isCollected flag and increase counter. On the next Update() run you will skip that kiwi (if you haven't deleted it yet or want to perform some animation) as you've already set isCollected flag, like:
if (Intersect(...) && !isCollected)
{
counter++;
isCollected=true;
}
Though this approach answers your question, i advice you to think about the whole game logic you will want to implement, for ex. how are all of the kiwis will be managed and how to check only active kiwis for intersection to improve performance :)

C# Sound Collision Breakout

So, I'm having trouble with my Breakout again. The score and lives keep going. I've tried to change it to score += or score = score+= 1, but nothing. Also, my sound keeps playing besides the times where the paddle and puck collide. I've tried putting Instance. Stop but it just keeps it from playing. I've done my research for the past two days and still haven't been able to figure out it out. Should I just make a different sound class like in Repeating sound from one Instance ? Just asking if y'all could point me in the right direction. I know the problem lies in the segment I've included. This is in my main game.cs:
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
paddle.Update();
puck.Update();
foreach (Brick brick in bricks)
{
brick.CheckCollision(puck);
{ score = score += 1 ; }
}
if (puck.OffBottom())
{
lives+= 1;
if (lives == 0)
StartGame();
};
puck.PaddleCollision(paddle.GetBounds());
crashSoundInstance.Play();
// This bottom statement I tried adding to see if it worked, but nothing...
// if (puck.PaddleCollision(paddle.GetBounds(false)))
// { crashSoundInstance.Pause(); }
base.Update(gameTime);
}

Retrieving Multitouch Data in XNA

I am having a problem retrieving touch input on my XNA game. Here is my starting code:
TouchCollection touchPositions;
List<Vector2> touchReleases = new List<Vector2>();
Then this is my update code:
protected override void Update(GameTime gameTime)
{
// Other update logic here
touchReleases.Clear();
foreach (TouchLocation tl in touchPositions)
{
if (tl.State == TouchLocationState.Released)
touchReleases.Add(tl.Position);
}
base.Update(gameTime);
}
The idea is to load all of the releases into a Vector2 list. Then (during the draw code) a function checks for a release to do stuff.
EDIT: I tested this using Guide.BeginShowMessageBox() during the foreach loop. It popped up every time I released.
bool released = false;
foreach (TouchLocation tl in touchPositions)
{
// checks to see if the button is being hovered on
}
foreach (Vector2 tr in touchReleases)
{
if (PointInArea(tr, new Rectangle((int)position.X, (int)position.Y, buttonimage[0].Width, buttonimage[0].Height)))
{
released = true;
break;
}
}
return released;
The PointInArea function checks to see if a Vector2 is in a Rectangle. I know this function is working right.
What's funny is that the buttons respond to releases about once every 4 taps. Does anyone know why this is?

Draw() method in XNA stops working inside a for loop

So far, this is my first XNA game and I'm having real trouble trying to learn this.
I'm following a tutorial from Microsoft, found here: XNA Xbox Live Indie Games
Every now and again, the code breaks. Admittedly, I have taken a couple of bits out that I didn't think I'd need and I've created two enemy classes rather than just the one, but I don't think I hit any major faultlines with my adjustments.
In the Draw() method in the main Game1.cs file, I've had to include a for loop that will iterate through a list of available enemies and draw them upon update. However, the line of code flags up as incorrect, and I have absolutely no idea why. I followed the tutorial, and it looks like it should work, but it doesn't. Here's the entire Draw() method:
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.ForestGreen);
backRect.Width = 800;
backRect.Height = 480;
// TODO: Add your drawing code here
// Start drawing
spriteBatch.Begin();
spriteBatch.Draw(backgroundTexture, backRect, Color.White);
// Draw the Player
player.Draw(spriteBatch);
for (int i = 0; i < goblins.Count; i++)
{
goblins[i].Draw(spriteBatch);
}
// Stop drawing
spriteBatch.End();
base.Draw(gameTime);
}
It's the code inside the for loop that won't work. Any ideas how to fix it and/or any suggestions for a better tutorial?
I like this tutorial a lot:
http://xbox.create.msdn.com/en-US/education/tutorial/2dgame/getting_started
It got me started off pretty well.
You always need to call SpriteBatch.Begin() and SpriteBatch.End() on your sprite-batches. I am not sure about mixing them, but try to avoid it and use as few spritebatches as possible.
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.ForestGreen);
backRect.Width = 800;
backRect.Height = 480;
// TODO: Add your drawing code here
// Start drawing
spriteBatch.Begin();
spriteBatch.Draw(backgroundTexture, backRect, Color.White);
// Draw the Player
spriteBatch.Draw(playerTexture, playerRect, Color.White);
for (int i = 0; i < goblins.Count; i++)
{
spriteBatch.Draw(goblins[i].Texture, goblins[i].Rect, Color.White);
}
// Stop drawing
spriteBatch.End();
base.Draw(gameTime);
}
See here for the documentation.

adding a 2D Sprite with a mouse click

i'm just beginning to code in C# / XNA
I've made a very simple little program in XNA, its a drawn rectangle, with 3 randomly generated balls inside,
the balls are defined in their own class and I've been following this tutorial
http://www.bluerosegames.com/brg/xna101.aspx
the balls are generated using
int ballCount = 3;
and what i wanted to do is make it so a mouse click would increase the int by 1, adding another ball to the screen
my code looks like this, but I'm not sure if it's right / possible
mouseStateCurrent = Mouse.GetState();
if (mouseStateCurrent.LeftButton == ButtonState.Pressed &&
mouseStatePrevious.LeftButton == ButtonState.Released)
{
ballCount = ballCount+1;
}
mouseStatePrevious = mouseStateCurrent;
any help advice would be helpful :)
i am using a code to draw the balls already that looks like this
spriteBatch.Begin();
spriteBatch.Draw(debugColor, TextBox, Color.White);
spriteBatch.Draw(background, backgroundRectangle, Color.White);
foreach (BouncingBall ball in balls)
{
ball.Draw(spriteBatch);
}
spriteBatch.End();
base.Draw(gameTime);
is it possible to edit this to get the "click to add balls" effect?
If balls is defined as a List<BouncingBall> that is accessible to the Game class, in your MouseClick event you can use balls.Add(new BouncingBall());. Because you are using a foreach loop, it will increment the number of balls each loop and your Draw code will already cater for any new balls added.
In your draw method you can do something like
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
for(var i=0;i<ballcount;i++)
{
spriteBatch.Draw()
}
spriteBatch.End();
base.Draw(gameTime);
}

Categories

Resources