xna GameTime
#1 Members - Reputation: 264
Posted 31 October 2012 - 07:25 PM
[source lang="csharp"] if (state.IsKeyDown(Keys.Space)) { bullets[bulletCount] = Content.Load<Texture2D>("bullet"); bulletsPos[bulletCount].X = shipPos.X; bulletsPos[bulletCount].Y = shipPos.Y; bulletCount++; }[/source]
this happens every time a bullet is created, but i only want one created for each press of the spacebar.
#2 GDNet+ - Reputation: 1154
Posted 31 October 2012 - 07:31 PM
You are cheking if the space bar is pressed, on each check another bullet is created. Create another keyboard state variable called laststate. On the end of the update method you'll update laststate with the current state. Then you can check if the key has been pressed, which means laststate.IsKeyUp(Keys.Space) && state.IsKeyDown(Keys.Space)
Got it?
Edited by Arthur Souza, 31 October 2012 - 07:32 PM.
A.
Lotus RPG Engine - My Journal: http://www.gamedev.n...die-rpg-engine/ |
Action RPG In development using XNA 4.0. | Blog in English: en.lotusrpg.com.br |
Personal blog In Portuguese: lotuzgames.wordpress.com |
#4 GDNet+ - Reputation: 1154
Posted 01 November 2012 - 08:54 AM
The if statement checks if the key is currently down. You also need to check if the key was released before. This way, on the next loop, the previous keyboard state will have the key as already being down, and won't create another bullet. Only if you release the key and press it again, which is the exact effect you are looking for.
A.
Lotus RPG Engine - My Journal: http://www.gamedev.n...die-rpg-engine/ |
Action RPG In development using XNA 4.0. | Blog in English: en.lotusrpg.com.br |
Personal blog In Portuguese: lotuzgames.wordpress.com |
#5 Members - Reputation: 1613
Posted 01 November 2012 - 09:06 AM
Edited by BeerNutts, 01 November 2012 - 09:20 AM.
---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)
#6 Members - Reputation: 1683
Posted 01 November 2012 - 11:12 AM
In other words, your boolean needs to look more like:ah. I thought the if statement i made was checking for the key being down. So at the end of the if statement i need to switch the currentKeyState to previousKeyState. I just confused myself
if (state.IsKeyDown(Keys.Space) && prevState.IsKeyUp(Keys.Space))
This is one of the reasons I roll an input-handling class that abstracts the various comparison combinations into things like "inputHandler.isKeyJustPressed(Keys.Space)" and "inputHandler.isKeyBeingHeld(Keys.Space)". This is mainly acheived by giving your input handler an update method that gets called every frame and does your keystate swapping (i.e. prevState = currentState; currentState = getState()).
Edited by BCullis, 01 November 2012 - 11:16 AM.
#7 Members - Reputation: 270
Posted 01 November 2012 - 07:27 PM






