Edit: fix'd, sorry. Just had to only run the action if the Enter key had been released between loops.
I've been learning C# for a week or so (in combination with XNA) just to play with the basics of game development.
Anyway, I'm trying to allow a certain action to be performed up to 5 times in the course of the game (Enter key to 'warp' around the screen).
The following snippets are where I try to do this and increment a counter up to 5, and past 5 not allow the action to run.
However, currently it seems I can only warp via the Enter key once, sometimes twice if I hit it a second time very quickly? That may just be my imagination though.
public class Game1 : Microsoft.Xna.Framework.Game
{
int warpCount = 0;
int warpMax = 5;
protected void winMove()
{
if (curState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Enter))
{
if (warpCount < warpMax)
{
winWarp();
warpCount += 1;
}
}
protected void winWarp()
{
Random randr = new Random(); //One Random to randomize them all.
//I did not just write that.
warping = true;
if (randr.NextDouble() > 0.5)
winLocation.X += (randr.Next(200, 300));
else
winLocation.X -= (randr.Next(200, 300));
if (randr.NextDouble() > 0.5)
winLocation.Y += (randr.Next(200, 300));
else
winLocation.Y -= (randr.Next(200, 300));
}
Any help is appreciated. The above snippets mostly don't close etc because I've only shown the key parts to the warping method.
Question
callummr
Edit: fix'd, sorry. Just had to only run the action if the Enter key had been released between loops.
I've been learning C# for a week or so (in combination with XNA) just to play with the basics of game development.
Anyway, I'm trying to allow a certain action to be performed up to 5 times in the course of the game (Enter key to 'warp' around the screen).
The following snippets are where I try to do this and increment a counter up to 5, and past 5 not allow the action to run.
However, currently it seems I can only warp via the Enter key once, sometimes twice if I hit it a second time very quickly? That may just be my imagination though.
public class Game1 : Microsoft.Xna.Framework.Game { int warpCount = 0; int warpMax = 5;protected override void Update(GameTime gameTime) { winMove();protected void winMove() { if (curState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Enter)) { if (warpCount < warpMax) { winWarp(); warpCount += 1; } }protected void winWarp() { Random randr = new Random(); //One Random to randomize them all. //I did not just write that. warping = true; if (randr.NextDouble() > 0.5) winLocation.X += (randr.Next(200, 300)); else winLocation.X -= (randr.Next(200, 300)); if (randr.NextDouble() > 0.5) winLocation.Y += (randr.Next(200, 300)); else winLocation.Y -= (randr.Next(200, 300)); }Any help is appreciated. The above snippets mostly don't close etc because I've only shown the key parts to the warping method.
Thanks very much.
Edited by Callum M-RLink to comment
Share on other sites
3 answers to this question
Recommended Posts