Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualXanather

Posted 05 November 2012 - 12:13 AM

one more thing. lets say when I press the tilde key I want to set a boolean to be true and keep it true until I press it again I want to set it to false. how can I do that?

this is what I got so far.

public void Update()
{
	 newState = Keyboard.GetState();

	 if (newState.IsKeyDown(Keys.OemTilde) && oldState.IsKeyUp(Keys.OemTilde))
		  Enable = true;

	 if (newState.IsKeyUp(Keys.OemTilde) && Enable == true)
		  Enable = false;

	 oldState = newState;
}


The following code should work...
[source lang="csharp"] public KeyboardState keyboardstate; public KeyboardState keyboardstateprev; public bool enabled; protected override void Update(GameTime gameTime) { keyboardstate = Keyboard.GetState(); if (keyboardstate.IsKeyUp(Keys.Enter) && keyboardstateprev.IsKeyDown(Keys.Enter)) { if (enabled) { enabled = false; } else { enabled = true; } } keyboardstateprev = keyboardstate; base.Update(gameTime); }[/source]

#3Xanather

Posted 05 November 2012 - 12:03 AM

one more thing. lets say when I press the tilde key I want to set a boolean to be true and keep it true until I press it again I want to set it to false. how can I do that?

this is what I got so far.

public void Update()
{
	 newState = Keyboard.GetState();

	 if (newState.IsKeyDown(Keys.OemTilde) && oldState.IsKeyUp(Keys.OemTilde))
		  Enable = true;

	 if (newState.IsKeyUp(Keys.OemTilde) && Enable == true)
		  Enable = false;

	 oldState = newState;
}


The following code should work...
[source lang="csharp"] public KeyboardState keyboardstate; public KeyboardState keyboardstateprev; public bool enabled; protected override void Update(GameTime gameTime) { keyboardstate = Keyboard.GetState(); if (keyboardstate.IsKeyUp(Keys.Enter) && keyboardstateprev.IsKeyDown(Keys.Enter)) { if (enabled) { enabled = false; } else { enabled = true; } } keyboardstateprev = keyboardstate; base.Update(gameTime); }[/source]

You may want to initialize both keyboardstate the variables in the initialize method so you don't receive a null reference exception or something.

#2Xanather

Posted 05 November 2012 - 12:02 AM

one more thing. lets say when I press the tilde key I want to set a boolean to be true and keep it true until I press it again I want to set it to false. how can I do that?

this is what I got so far.

public void Update()
{
	 newState = Keyboard.GetState();

	 if (newState.IsKeyDown(Keys.OemTilde) && oldState.IsKeyUp(Keys.OemTilde))
		  Enable = true;

	 if (newState.IsKeyUp(Keys.OemTilde) && Enable == true)
		  Enable = false;

	 oldState = newState;
}


The following code should work...
[source lang="csharp"] public KeyboardState keyboardstate; public KeyboardState keyboardstateprev; public bool enabled; protected override void Update(GameTime gameTime) { keyboardstate = Keyboard.GetState(); if (keyboardstate.IsKeyUp(Keys.Enter) && keyboardstateprev.IsKeyDown(Keys.Enter)) { if (enabled) { enabled = false; } else { enabled = true; } } keyboardstateprev = keyboardstate; base.Update(gameTime); }[/source]

You may want to initalize both keyboardstate variables in the initalize so you dont receive a null reference exception or something.

#1Xanather

Posted 05 November 2012 - 12:02 AM

one more thing. lets say when I press the tilde key I want to set a boolean to be true and keep it true until I press it again I want to set it to false. how can I do that?

this is what I got so far.

public void Update()
{
	 newState = Keyboard.GetState();

	 if (newState.IsKeyDown(Keys.OemTilde) && oldState.IsKeyUp(Keys.OemTilde))
		  Enable = true;

	 if (newState.IsKeyUp(Keys.OemTilde) && Enable == true)
		  Enable = false;

	 oldState = newState;
}


The following code should work...
[source lang="csharp"] public KeyboardState keyboardstate; public KeyboardState keyboardstateprev; public bool enabled; protected override void Update(GameTime gameTime) { keyboardstate = Keyboard.GetState(); if (keyboardstate.IsKeyUp(Keys.Enter) && keyboardstateprev.IsKeyDown(Keys.Enter)) { if (enabled) { enabled = false; } else { enabled = true; } } keyboardstateprev = keyboardstate; base.Update(gameTime); }[/source]

PARTNERS