I've been following a tutorial and modifying it to make my own game. I've come to this section and am working on implementing projectile creation and animation.
In his tutorial, he starts the class with public Animation LaserAnimation;
but I don't seem to have the namespace with an "Animation" type.
So I decided to just try and implement it with using regular Texture2D type and came up with the following...
This is the texture declared in the bullet class.
public Texture2D BulletTexture;
And in the main game class, I converted his two functions to...
protected void FireBullet(GameTime gameTime) {
if(gameTime.TotalGameTime - previousBulletSpawnTime > bulletSpawnTime) {
previousBulletSpawnTime = gameTime.TotalGameTime;
AddBullet();
}
}
protected void AddBullet() {
//Load Bullet Texture
Texture2D bulletAnimation = Content.Load<Texture2D>("Graphics\\bullet2");
//Draw Bullet
spriteBatch.Begin();
spriteBatch.Draw(bulletAnimation, player.Position, null, Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0f);
spriteBatch.End();
Bullet bullet = new Bullet();
var bulletPosition = player.Position;
bulletPosition.Y += 60;
bulletPosition.X += 44;
bullet.Initialize(bulletAnimation, bulletPosition);
bullets.Add(bullet);
But it doesn't seem to be working. And by that I mean when I press the space bar in order to make it fire, nothing is drawn. What part of this conversion did I get wrong, or am I missing something fundamental about it.
EDIT: Okay, so it seems that I was in too much of a hurry to test out this portion and didn't see the very last section of this that I needed to add in...
// Draw the lasers.
foreach (var l in laserBeams)
{
l.Draw(spriteBatch);
}
So I've gone ahead and done that(although I changed the "1" to a different name as I was getting errors(I don't know how he got it to work)).
And now the bullets are in fact firing, but instead of coming out one at a time, it shows several in a row.
I'm wondering if it has something to do with his Update function that he makes in the class. Because I changed it over to a Texture2D, it wasn't working like his for an Animation type.
public void Update(GameTime gameTime)
{
Position.X += laserMoveSpeed;
LaserAnimation.Position = Position;
LaserAnimation.Update(gameTime);
}
This is what his looks like. But on mine, I had to comment out the 2nd and 3rd lines because Texture2D doesn't have those function calls. What is it that those are doing that would cause my bullets to "ghost" and not disappear before the new one is being drawn.
And now the bullets are in fact firing, but instead of coming out one at a time, it shows several in a row.
If you look at the FireLaser()
invokation in UpdatePlayer()
of Game1
class
if (_currentKeyboardState.IsKeyDown(Keys.Space) || _currentGamePadState.Buttons.X == ButtonState.Pressed)
{
FireLaser(gameTime);
}
you'll see that FireLaser() is called while the spacebar or gamepad's X button is down / pressed. Since MonoGame's Update
is invoked 60 times per second, it leads to FireLaser
being invoked several times until you release the button.
If you want to avoid it, save the previous state and check that it wasn't pressed before.
if (_currentKeyboardState.IsKeyDown(Keys.Space) && _previousKeyboardState.IsKeyReleased(Keys.Space))
{
FireLaser(gameTime);
}
_previousKeyboardState = _currentKeyboardState;
FireLaser()
will be invoked once per spacebar click