Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


xna GameTime


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 burnt_casadilla   Members   -  Reputation: 443

Like
0Likes
Like

Posted 31 October 2012 - 07:25 PM

I have a class that creates bullets that are stored in an array. Only 10 bullets are allowed on the screen at a time. The issue I'm having is that for each press of the spacebar, a lot of bullets are being created instead of just one. I know that I have to create a gametime variable, but I dont know where to use it

[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.

If you see a post from me, you can safely assume its C# and XNA :)


Sponsor:

#2 Arthur Souza   Members   -  Reputation: 1419

Like
0Likes
Like

Posted 31 October 2012 - 07:31 PM

What is the definition of a keypress? The key is not pressed and then you press it. You have to check for that.

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 - Action RPG In development http://www.gamedev.n...die-rpg-engine/ |
Personal blog In Portuguese: lotuzgames.wordpress.com |


#3 burnt_casadilla   Members   -  Reputation: 443

Like
0Likes
Like

Posted 31 October 2012 - 07:43 PM

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 you see a post from me, you can safely assume its C# and XNA :)


#4 Arthur Souza   Members   -  Reputation: 1419

Like
0Likes
Like

Posted 01 November 2012 - 08:54 AM

Don't know if I misunderstood you on the last reply you made.

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 - Action RPG In development http://www.gamedev.n...die-rpg-engine/ |
Personal blog In Portuguese: lotuzgames.wordpress.com |


#5 BeerNutts   Crossbones+   -  Reputation: 2985

Like
2Likes
Like

Posted 01 November 2012 - 09:06 AM

A typical solution to this is to limit the refire rate for the bullet, say make a variable RefireTime be 200 milliseconds. So, once a bullet is fired, record the current time as LastShot (in milliseconds). Don't allow another bullet to be fired until the time is at or past LastShot + RefireTime

Edited by BeerNutts, 01 November 2012 - 09:20 AM.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

#6 BCullis   Crossbones+   -  Reputation: 1813

Like
0Likes
Like

Posted 01 November 2012 - 11:12 AM

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

In other words, your boolean needs to look more like:

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.

Hazard Pay :: FPS/RTS in SharpDX
DeviantArt :: Because right-brain needs love too

#7 ShadowValence   Members   -  Reputation: 380

Like
0Likes
Like

Posted 01 November 2012 - 07:27 PM

Adding to what BCullis has mentioned: I prefer for my input handler(s) to inherit the GameComponent class. That way, they're guaranteed to have an update method and I can add them to the game components during initialization of the Game class. The Game class will then ensure that the Input Handler is updated behind the scenes and at the requested framerate should FixedTimeStepping be active.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS