XNA产生敌人的简单方法

时间:2013-04-18 17:30:02

标签: timer xna spawning

我试图从列表中生成敌人。我已经尝试了多种方式。但是我做不到 让它工作。有没有一种简单的方法每隔3秒产生一次烯醇? 修改

我试着像这样产生它:问题:只产生一次

    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;
                }
            }
        }
    }

并且像这样:问题:再次产生一次

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;
        }

并且这个产生了但是没有移动我用updat和draw方法玩了但是似乎什么都没有用

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

更新:

    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;
            }
在平局中

    if (spawnN)
        {
            foreach (Enemy needele in needleList)
            {
                needele.Draw(gameTime, spriteBatch);
            }
        }

2 个答案:

答案 0 :(得分:1)

您需要将此问题分解为以下步骤:

您需要一些全局变量来表示计时器并表示等待的时间,例如

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

接下来在您的更新中,您需要增加自上次更新以来经过的时间。您似乎使用多个计时器并使其比您需要的更笨拙。如果你需要计时多件事,你应该只需要多个计时器。

spawnTimer += gameTime.ElapsedGameTime.TotalSeconds;

接下来,当经过的时间大于等待所需时间的时间量。产生一个敌人,然后将计时器重置为零。

如果你想一次产生多个敌人,添加一个for循环,其条件是循环,而迭代器小于产生的敌人数。

我有点不给你最后两个方面,概述的步骤应该相对简单。完成后,其余代码应该可以正常工作。

如果这不起作用,请调试代码并检查spawnEnemy是否成功调用。

答案 1 :(得分:1)

此外,您应该将新敌人添加到列表中。

因此,在您的更新方法中,您可以执行以下操作:

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();
}