I have a little problem. I am working in a Title Screen or main menu for my game. Here is a screenshot of the main window:
and the code:
class MenuPrincipal
{
public Texture2D Fondo { get; set; }
public Texture2D Cursor { get; set; }
int cambiar = 0;
int tiempoTranscurrido;
KeyboardState teclaActual;
bool menuActivo;
public bool MenuActivo
{
get { return menuActivo; }
set { menuActivo = value; }
}
public MenuPrincipal(Texture2D fondo, Texture2D cursor)
{
Fondo = fondo;
Cursor = cursor;
}
public void Update(GameTime gameTime)
{
teclaActual = Keyboard.GetState();
tiempoTranscurrido = gameTime.ElapsedGameTime.Milliseconds;
if (tiempoTranscurrido > 50)
{
tiempoTranscurrido = 0;
if (teclaActual.IsKeyDown(Keys.Down))
{
if (cambiar > 2)
cambiar = 0;
else
cambiar++;
}
}
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(Fondo, new Rectangle(0, 0, 800, 600), Color.White);
switch(cambiar)
{
case 0: spriteBatch.Draw(Cursor, new Rectangle(325, 225, 16, 12), Color.White);
break;
case 1: spriteBatch.Draw(Cursor, new Rectangle(325, 281, 16, 12), Color.White);
break;
case 2: spriteBatch.Draw(Cursor, new Rectangle(325, 336, 16, 12), Color.White);
break;
}
}
}
}
I want the cursor to move from “un jugador” to “opciones,” and from “opciones” to “creditos” when the user presses the Down arrow key. However, when the user presses the Down key, the cursor moves very fast. I want a speed limit so that when I press the key one time, the cursor moves one option. If I put a “limit frame,” (variable tiempoTranscurrido), the cursor never moves. If I don’t put anything, the cursor moves very fast.
Add a flag until the key comes up, something like this…