1

我正在 XNA 中开发一款塔防游戏,并且我有一个名为 Tank 的类,它创建一个敌人,该敌人沿着通过 A* 找到的地图的路线前进。现在,我可以创建多个 Tank 实例,但它们都是同时生成的。这意味着他们在沿着路线行驶时处于完全相同的位置,从而看起来只有一个敌人。

这是我加载新浪潮的代码:

if (spawn == true)
{
    maxTanks += 2;
    killsInWave += 2;

    tanks = new Tank[maxTanks];
    for (int i = 0; i < maxTanks; i++)
    {
        tanks[i] = new Tank();
        tanks[i].Initialize(map);
        tanks[i].LoadContent(Content);                                  
    }
    waveNum += 1;
    spawn = false;
}

谁能给我一点帮助,让每个敌人在它和最后一个之间产生延迟?

4

3 回答 3

2

你需要一个基于时间的生成率,类似于这个......

   SpawnUnitsCount = n;
   Units_Per_Second = 5; // 5 Per second

   void Update(float ElapsedSeconds)  // (float) gametime.ElapsedTime.TotalSeconds
   {      
    if (SpawnUnitCount>0)
    {
      CreationRate += Units_per_second * ElapsedSeconds;
      while ( CreationRate >= 1 )
      {
         CreationRate--;
         MakeNewUnit(); 
         SpawnUnitsCount --;
         if (SpawnUnitCount == 0)
         {
            CreationRate = 0; // The wave is finished
         }
      }
    }
于 2012-05-07T11:16:28.897 回答
0

不确定 XNA 的具体情况,但如果您需要间隔执行一些重复操作,您可以使用类似的东西:

        Action addTank = null;
        int interval = 1000; // 1000 milliseconds
        int requiredNumberOfTanks = 10;
        int tanksCreated = 0;

        var timer = new Timer(o => addTank());
        addTank = () =>
                         {
                             CreateTankMethod();  // your method to create tank
                             tanksCreated = tanksCreated + 1;
                             if (tanksCreated < requiredNumberOfTanks)
                                 timer.Change(interval, 0);
                         };
        timer.Change(interval, 0);

此代码将以最有效的方式利用线程。它比Thread.Sleep()

您可能还想让坦克之间的间隔显得更加随机。

于 2012-05-07T11:54:07.707 回答
0

我会这样做:

//need to be defined in your loop
int nextTankToCreate(0);
int iterationCount(0);
int CreateTime(500); //create every 500 iteration

iterationCount++;
if (spawn == true && iterationCount >= createTime)
        {
            iterationCount = 0;
            maxTanks += 2;
            killsInWave += 2;

            tanks = new Tank[maxTanks];
            tanks[nextTankToCreate] = new Tank();
            tanks[nextTankToCreate].Initialize(map);
            tanks[nextTankToCreate].LoadContent(Content);                                  
            nextTankToCreate++;

            if (nextTankToCreate == maxTanks)
            {
                nextTankToCreate = 0;
                waveNum += 1;
                spawn = false;
            }
        }
于 2012-05-07T11:29:42.580 回答