crmepham
crmepham

Reputation: 4750

Trying to perform an action whilst a variable is above zero in XNA

I am creating a basic 2D space shooter game in XNA 4.0.

I have a player class and in its Update method it calls a shoot function if the space bar is pressed. It checks this every frame. In my main game class I have a check on whether the player intersects with a ammo boost item. When the player intersects with this item it should set the players bullet delay to 1 (instead of 10) and sets the variable ammoBoost to 500 which reduces by 1 every frame.

So now when the player presses space bar it should fire the bullets with only 1 delay for 500 frames.

So far I have everything working and it fires with only the small delay, but I can't get it to reset the bullet delay to 10 when the ammoBoost variable has reached 0 after the 500 frames.

I've including relevant code snippets:

main game class

// Update boosts and check for collisions
foreach (Boost b in boostList)
{
    b.Update(gameTime);

    if(b.boundingBox.Intersects(p.boundingBox))
    {
        // if life boost 
        if(b.boost == 1)
        {
            // add life to player lives
            hud.playerLives += 1;
            // make boost invisible
            b.isVisible = false;
        }
        // if bullet boost
        else if(b.boost == 2)
        {
            b.isVisible = false;

            // set ammo for gun
            hud.boostAmmo = 500;
            p.boostAmmo = 500;
        }
    }
}

player class update method:

    // fire bullets
    if (keyboard.IsKeyDown(Keys.Space)) 
    {
        shoot(boostAmmo); 
    }

    // shoot (used to set starting position of bullets)
    public void shoot(int boostAmmo)
    {


       // if boostammo is above zero boost is active and there should be no delay on bullets firing
if (boostAmmo > 0)
{
    bulletDelay = 1;
    Bullet newBullet = new Bullet(bulletTexture);
    newBullet.position = new Vector2(playerPosition.X + 32 - newBullet.texture.Width / 2, playerPosition.Y + 30);
    if (playSounds)
    {
        sm.playerShootSound.Play();
    }

    newBullet.isVisible = true;

    if (bulletList.Count() < 20)
    {
        bulletList.Add(newBullet);
    }
    boostAmmo = boostAmmo -1;
}
else if (boostAmmo <= 0)
{

    // shoot only if the bullet delay resets
    if (bulletDelay > 0)
    {
        bulletDelay--;
    }

    // if bullet delay is at zero then create a new bullet at player position and make it visible on the screen, then ad that bullet to the list
    if (bulletDelay <= 0)
    {
        Bullet newBullet = new Bullet(bulletTexture);
        newBullet.position = new Vector2(playerPosition.X + 32 - newBullet.texture.Width / 2, playerPosition.Y + 30);
        if (playSounds)
        {
            sm.playerShootSound.Play();
        }

        newBullet.isVisible = true;

        if (bulletList.Count() < 20)
        {
            bulletList.Add(newBullet);
        }
    }

    // reset bullet delay
    if (bulletDelay == 0)
    {
        bulletDelay = 10;
    }
}

Why is the bullet delay not reseting back to 10 after the 500 frames have passed?

Upvotes: 1

Views: 55

Answers (1)

Lind
Lind

Reputation: 71

I think what you are doing is setting bulletDelay back to 10 ... repeatedly. Right now, when boostAmmo <= 0 you set bulletDelay back to 10, then you do some more checks. Then, repeat, you set bulletDelay back to 10. bulletDelay never reaches 0!

Also, you're never firing a bullet if boostAmmo > 0. I'll let you see if you can figure the rest out on your own, but I'll edit this answer if you need me to give you a full solution~

Upvotes: 1

Related Questions