1

Im trying to spawn enemies from a list. I've tryed multiple ways in doing so. However I can't get it to work. Is there a simple way to spawn enemes every 3 seconds? EDIT

I tried spawning it like this: problem: only spawns once

    protected void AdjustSpawnTimes(GameTime gameTime)
    {
        // If the spawn maximum time is > 500 milliseconds,
        // decrease the spawn time if it's time to do so
        // based on spawn-timer variables
        if (enemySpawnMaxMilliseconds > 500)
        {
            timeSinceLastSpawnTimeChange += gameTime.ElapsedGameTime.Milliseconds;
            if (timeSinceLastSpawnTimeChange > nextSpawnTimeChange)
            {
                timeSinceLastSpawnTimeChange -= nextSpawnTimeChange;
                if (enemySpawnMaxMilliseconds > 1000)
                {
                    enemySpawnMaxMilliseconds -= 100;
                    enemySpawnMinMilliseconds -= 100;
                }
                else
                {
                    enemySpawnMaxMilliseconds -= 10;
                    enemySpawnMinMilliseconds -= 10;
                }
            }
        }
    }

and like this: problem: again spawns once

private void SpawnEnemy()
    {
        Vector2 speed = Vector2.Zero;
        Vector2 position = Vector2.Zero;

        // Default frame size
        Point frameSize = new Point(75, 75);

        int screenWidth = GraphicsDevice.PresentationParameters.BackBufferWidth;
        int screenHeight = GraphicsDevice.PresentationParameters.BackBufferHeight;

        // Randomization:
        // - Randomly choose which side of the screen to place the enemy
        // - Randomly create a position along that side of the screen
        // - Randomly choose a speed for the enemy
        switch (rand.Next(4))
        {
            case 0: // Left to right
                position = new Vector2(-frameSize.X,
                                       (rand.Next(0, screenHeight - frameSize.Y)));
                speed = new Vector2(rand.Next(needleMinV, needleMaxV),
                                    0);
                break;

            case 1: // Right to left
                position = new Vector2(screenWidth,
                                       (rand.Next(0, screenHeight - frameSize.Y)));
                speed = -new Vector2(rand.Next(needleMinV, needleMaxV),
                                    0);
                break;

            case 2: // Bottom to top
                position = new Vector2(rand.Next(0, screenWidth - frameSize.X),
                                       screenHeight);
                speed = -new Vector2(0,
                                    (rand.Next(needleMinV, needleMaxV)));
                break;

            case 3: // Top to bottom
                position = new Vector2(rand.Next(0, screenWidth - frameSize.X),
                                       -frameSize.Y);
                speed = new Vector2(0,
                                   rand.Next(needleMinV, needleMaxV));
                break;
        }

and this one spawns but does not move ive played around with the updat and draw method but nothing seems to work

List<Enemy> needleList = new List<Enemy>();
    Texture2D needle;
    float spawnTime = 10;
    const float TIMER = 10;
    bool spawnN = true;

in update:

    timer = (float)gameTime.TotalGameTime.TotalSeconds;

        spawnTime -= (float)gameTime.ElapsedGameTime.TotalSeconds;
        if (timer < 0)
        {
            foreach (Enemy needele in needleList)
            {
                spawnN = !spawnN;
                needele.Update(gameTime);
                spawnTime = TIMER;
            }

in draw:

    if (spawnN)
        {
            foreach (Enemy needele in needleList)
            {
                needele.Draw(gameTime, spriteBatch);
            }
        }
  • "I don't know how to do this" is not a question. What have you tried so far? Why hasn't it worked? – Cole Campbell Apr 18 '13 at 19:31
  • I tried this, and when it returns true, draw. @ColeCampbell `spawnTime-(float)gameTime.ElapsedGameTime.TotalSeconds; if (timer < 0) { foreach (Enemy needele in needleList) { spawnN = !spawnN; needele.Update(gameTime); spawnTime = TIMER; } } ` – user2296150 Apr 18 '13 at 20:09
  • What is the code you posted, you have tried multiple ways? Post what you have tried and give us some detailed information! – Cyral Apr 18 '13 at 20:28
  • There's ^^^^ what I've tried. @Cyral – user2296150 Apr 18 '13 at 21:04

2 Answers2

1

You need to break this problem into steps:

You need some global variables to represent a timer and to represent an amount of time to wait e.g.

float spawnTimer;
const float TIME_TO_WAIT = 3.0f; // don't make const if you need to change.

Next in your update you need to increment the time that has passed since the last update. You seem to be using multiple timers and making it more awkward than you need to be. You should only need multiple timers if you're needing to time multiple things.

spawnTimer += gameTime.ElapsedGameTime.TotalSeconds;

Next, when the time passed is greater than the amount of time than the time required to wait. Spawn an Enemy and then reset the timer back to zero.

If you want to spawn more than one enemy at a time, add a for loop with it's condition being to loop while the iterator is less than the number of enemies to spawn.

I've delibrately not given you the last two areas, the steps outlined should be relatively simple. The rest of your code should work once this is done.

If this doesn't work, debug your code and check if the spawnEnemy is called successfully or not.

Bushes
  • 1,010
  • 1
  • 18
  • 37
1

Also you should add new enemies to a list.

So in your update method you do something like this:

timeToSpawn += gameTime.ElapsedGameTime.Milliseconds; //adds the time since last update
if (timeToSpawn > 3000)
{
    enemyList.Add new Enemy(); //adds a new enemy to the list
    timeToSpawn -= 3000; subtract 3000ms off the timer.
}
foreach (Enemy e in enemyList)
{
    //Do stuff like updating enemy positions
    e.Update();
}
//In your draw method you do this as well:
foreach (Enemy e in enemyList)
{
    //Do stuff like updating enemy positions
    e.Draw();
}
Madmenyo
  • 8,389
  • 7
  • 52
  • 99