【发布时间】:2013-04-11 22:55:24
【问题描述】:
我正在尝试从列表中生成敌人。我已经尝试了多种方法。然而我不能 让它工作。有没有一种简单的方法可以每 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;
}
这个生成但不动我玩过更新和绘制方法但似乎没有任何工作
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);
}
}
【问题讨论】:
-
“我不知道该怎么做”不是问题。你都尝试了些什么?为什么没有成功?
-
我试过这个,当它返回true时,绘制。 @ColeCampbell
spawnTime-(float)gameTime.ElapsedGameTime.TotalSeconds; if (timer < 0) { foreach (Enemy needele in needleList) { spawnN = !spawnN; needele.Update(gameTime); spawnTime = TIMER; } } -
你贴的代码是什么,你试过多种方法?发布您尝试过的内容并给我们一些详细信息!
-
我试过了^^^^。 @Cyral