2013-04-18 30 views
1

我試圖從列表中產生敵人。我已經嘗試過多種方式。但我不能 得到它的工作。有沒有簡單的方法每隔3秒產生一次敵人? 編輯XNA簡單的方法來產生enemys

我試圖產卵這樣的:問題:只產卵一次

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); 
      } 
     } 
+0

「我不知道如何做到這一點」是不是一個問題。你試過什麼了?爲什麼它不起作用? –

+0

我試過了,當它返回true時,繪製。 @ColeCampbell 'spawnTime-(float)gameTime.ElapsedGameTime.TotalSeconds; (定時器<0) foreach(針列表中的敵人) { spawnN =!spawnN; needele。更新(gameTime); spawnTime = TIMER; } }' – user2296150

+0

您發佈的代碼是什麼,您嘗試過多種方式?發佈你已經嘗試過的並給我們一些詳細的信息! – Cyral

回答

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

此外,你應該添加新的敵人到列表中。

因此,在您更新方法你做這樣的事情:

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

是的好點。 – Bushes