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 7839939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:36:17+00:00 2026-06-02T15:36:17+00:00

I am creating a 2d side scrolling(kinda) game for the windows phone in C#

  • 0

I am creating a 2d side scrolling(kinda) game for the windows phone in C# and XNA.

I have a character animation that makes it look like the sprite is moving, however the background and enemies are the only things moving.
I have loaded the texture for the enemy and the position on the screen is static so that’s fine.
But my problem is, I want to spawn these enemies at random intervals.
I have set up a random number generator, an int and a couple TimeSpan variables.

Here is the code I have placed in the UpdateEnemies method.

Note: in Update, I am calling UpdateEnemies(gameTime);

random = new Random();
spawnSeconds = random.Next(1, 6);
enemySpawnTime = TimeSpan.FromSeconds(spawnSeconds); 
if (gameTime.TotalGameTime - previousSpawnTime > enemySpawnTime)
{
    previousSpawnTime = gameTime.TotalGameTime;
    // Add an Enemy
    AddEnemy();
}

I am having an issue here as the enemies spawning is not random. They spawn at the same interval for that particular run.

I have used Breakpoints and the spawnSeconds and enemySpawnTime update fine everytime but still this issue.

Any help would be greatly appreciated.

  • 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-02T15:36:21+00:00Added an answer on June 2, 2026 at 3:36 pm

    Assuming UpdateEnemies is called on every Update(), you are changing enemySpawnTime on every Update(), which doesn’t make sense. You should only change it when an enemy spawns, i.e.:

    if (gameTime.TotalGameTime - previousSpawnTime > enemySpawnTime)
    {
        // Add an Enemy
        AddEnemy();
    
        // Update the time left next enemy spawn
        previousSpawnTime = gameTime.TotalGameTime;
        var spawnSeconds = random.Next(1, 6); // random should be a member of the class
        enemySpawnTime = TimeSpan.FromSeconds(spawnSeconds); 
    }
    

    And as noted previously, only create the Random() object once, for example in Initialize(), and make it a member of the class.

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

Sidebar

Related Questions

I'm creating a 2d side scrolling game and have a question regarding the collision.
I am creating a client side swing app that will have data provided by/from
I am creating a side scrolling game in Flash (as3) and trying to keep
I'm creating a web site that I think must have a client side database.
I'm creating a demo that must be in html code only (no server side
I am creating an object server-side which is to have all of the data
I have a slight problem with creating a button on the server side code.
I am creating an app which uses Core Data. I have a flip-side in
I'm designing a website that uses side-scrolling strips of cells containing images with description
I'm new to server side. I'm creating a database app for my company that

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.