Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8727317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:24:52+00:00 2026-06-13T08:24:52+00:00

I want that an enemy shoots a bullet if the variable nextShot is bigger

  • 0

I want that an enemy shoots a bullet if the variable nextShot is bigger than the variable shotFrequency. Each enemy should fire independently.
But for the moment, every enemy is shooting all the time. Their are no breaks between the shots. But I want that their always is a little break between the shots. I’m sure that something is wrong about the Enemy class, but I don’t know what to change. Could somebody help me, please?

public class Map
{
    Texture2D myEnemy, myBullet;
    Player Player;
    List<Enemy> enemieslist = new List<Enemy>();
    List<Bullet> bulletslist = new List<Bullet>();

    float fNextEnemy = 0.0f;
    float fEnemyFreq = 2.0f;

    Vector2 Startposition = new Vector2(200, 200);
    Vector2 CurrentEnemyPosition;

    GraphicsDeviceManager graphicsDevice;

    public Map(GraphicsDeviceManager device) 
    { 
        graphicsDevice = device; 
    } 

    public void Load(ContentManager content)
    {
        myEnemy = content.Load<Texture2D>("enemy");
        myBullet = content.Load<Texture2D>("bullet"); 
        Player = new Player(graphicsDevice);
        Player.Load(content);
    }

    public void Update(GameTime gameTime)
    {
        Player.Update(gameTime);
        float delta = (float)gameTime.ElapsedGameTime.TotalSeconds;

        for(int i = enemieslist.Count - 1; i >= 0; i--) 
        {
            // Update Enemy
            Enemy enemy = enemieslist[i];
            enemy.Update(gameTime, this.graphicsDevice, Player.playershape.Position, delta);
            CurrentEnemyPosition = enemy.Bulletstartposition;
            // Does the enemy shot?
            if (enemy.Shot == true)
            // New bullet
              {
                  Vector2 bulletDirection = Vector2.Normalize(Player.playershape.Position - CurrentEnemyPosition) * 200f;
                  bulletslist.Add(new Bullet(CurrentEnemyPosition, bulletDirection, Player.playershape.Position));
              }
        }

        this.fNextEnemy += delta;
        //New enemy
        if (this.fNextEnemy >= fEnemyFreq)
        {
            Vector2 enemyDirection = Vector2.Normalize(Player.playershape.Position - Startposition) * 100f;
            enemieslist.Add(new Enemy(Startposition, enemyDirection, Player.playershape.Position));
            fNextEnemy = 0;
        }

        for(int i = bulletslist.Count - 1; i >= 0; i--) 
        {
            // Update Bullet
            Bullet bullets = bulletslist[i];   
            bullets.Update(gameTime, this.graphicsDevice, delta);       
        }
    }

    public void Draw(SpriteBatch batch)
    {
        Player.Draw(batch);
        foreach (Enemy enemies in enemieslist)
        {
            enemies.Draw(batch, myEnemy);
        } 
        foreach (Bullet bullets in bulletslist)
        {
            bullets.Draw(batch, myBullet);
        } 
    }
}

Enemy class:

public class Enemy
{
    private float nextShot = 0;
    private float shotFrequency = 1.0f;  
    Vector2 vPos;
    Vector2 vMove;
    Vector2 vPlayer;
    public Vector2 Bulletstartposition;
    public bool Remove;
    public bool Shot;

    public Enemy(Vector2 Pos, Vector2 Move, Vector2 Player)
    {
        this.vPos = Pos;
        this.vMove = Move;
        this.vPlayer = Player;
        this.Remove = false;
        this.Shot = false;
    }

    public void Update(GameTime gameTime, GraphicsDeviceManager graphics, Vector2 PlayerPos, float delta)
    {           
        nextShot += delta;

        if (nextShot >= shotFrequency)
            {
            this.Shot = true;
            nextShot = 0;
            }

        if (!Remove)
        {
            float fMoveTime = (float)gameTime.ElapsedGameTime.TotalSeconds;
            this.vMove = Vector2.Normalize(PlayerPos - this.vPos) * 100f;
            this.vPos += this.vMove * fMoveTime;
            Bulletstartposition = this.vPos;

            if (this.vPos.X > graphics.PreferredBackBufferWidth + 1)
            {
                this.Remove = true;
            }

            else if (this.vPos.X < -20)
            {
                this.Remove = true;
            }

            if (this.vPos.Y > graphics.PreferredBackBufferHeight + 1)
            {
                this.Remove = true;
            }

            else if (this.vPos.Y < -20)
            {
                this.Remove = true;
            }
        }
    }

    public void Draw(SpriteBatch spriteBatch, Texture2D myTexture)
    {
        if (!Remove)
        {
            spriteBatch.Draw(myTexture, this.vPos, Color.White);
        }
    }
}

Bullet class

public class Bullet
{
    Vector2 vPos;
    Vector2 vMove;
    Vector2 vPlayer;
    public bool Remove;

    public Bullet(Vector2 Pos, Vector2 Move, Vector2 Player)
    {
        this.Remove = false;
        this.vPos = Pos;
        this.vMove = Move;
        this.vPlayer = Player;
    }

    public void Update(GameTime gameTime, GraphicsDeviceManager graphics, float delta)
    {
            if (!Remove)
            {
                float fMoveTime = (float)gameTime.ElapsedGameTime.TotalSeconds;
                this.vPos.X += this.vMove.X * fMoveTime;
                this.vPos.Y += this.vMove.Y * fMoveTime;

                if (this.vPos.X > graphics.PreferredBackBufferWidth +1)
                {
                    this.Remove = true;
                }

                else if (this.vPos.X < -20)
                {
                    this.Remove = true;
                }

                if (this.vPos.Y > graphics.PreferredBackBufferHeight +1)
                {
                    this.Remove = true;
                }

                else if (this.vPos.Y < -20)
                {
                    this.Remove = true;
                }
            }         
    }

    public void Draw(SpriteBatch spriteBatch, Texture2D myTexture)
    {
        if (!Remove)
        {
            spriteBatch.Draw(myTexture, this.vPos, Color.White);
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T08:24:53+00:00Added an answer on June 13, 2026 at 8:24 am

    You are never clearing the Shot flag. After you have an enemy shoot, just do enemy.Shot = false;.

    Also, a tiny tip, when nextShot is >= shotFrequency, it’s better to do this:

    nextShot -= shotFrequency;
    

    It accounts for the tiny bit of overlap when you go beyond shotFrequency.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want that every enemy shoots independently a bullet. If an enemy’s bullet has
I want that whenever the user types http://localhost:8280/services/Facebook/sea the actual request should be http://localhost:8280/services/Facebook/search?q=jack
I want that screenCheck function show run only after Validation of #contact_email . But
I want that my unit tests to cover my POCO's. How should I test
a want that, he found all select elements with id than begins with 'chbTypes'
I want that the form will not close by doing Alt + F4 but
I want that div in the middle of the page, the div should be
i want that when user scroll down to absolute bottom then a div should
I want that my application (a WPF Window ) is launched on Windows startup.
I want that everytime someone wants to checkout the project from SVN he/she will

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.