有一个小问题,使我的电源在屏幕上显示/加载,并且我在VS19中没有错误。蠕虫类继承自与Player和Enemy类相同的父类,并且它们都会加载。它们之间唯一的区别是蠕虫在Update()而不是LoadContent()中加载,所以我只能假设其中的代码有问题。
如果你看到任何奇怪的东西,请让我知道,提前谢谢!
Global声明了一个列表和一个纹理类型:
List<Worm> worms;
Texture2D wormSprite;在load LoadContent()中,我将纹理赋给一个变量:
wormSprite = Content.Load<Texture2D>("worm");然后,我假设Update()中的代码有问题:
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
// TODO: Add your update logic here
/* player */
player.Update(Window);
/* power up */
// worms that spawn in a chance of 200 each loop(60/s)
Random random = new Random();
int newWorm = random.Next(1, 200);
if (newWorm == 1)
{
// cordinates for worms to appear
int rndX = random.Next(0, Window.ClientBounds.Width - wormSprite.Width);
int rndY = random.Next(0, Window.ClientBounds.Height - wormSprite.Height);
// add the worm to the list
worms.Add(new Worm(wormSprite, rndX, rndY, gameTime));
}
// loop thru the list of worms
foreach (Worm w in worms.ToList())
{
if (w.IsAlive) // check if worm is alive
{
// w.Update() check if the worm is to old to stay active
w.Update(gameTime);
// check if the worms collides with the player
if (w.CheckCollision(player))
{
// remove worm if collides
worms.Remove(w);
player.Points++; // and give the player a point
}
}
else // remove the worm if it's too inactive
worms.Remove(w);
}
base.Update(gameTime);
}打印到屏幕方法
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
// TODO: Add your drawing code here
_spriteBatch.Begin();
player.Draw(_spriteBatch);
foreach (Enemy e in enemies)
e.Draw(_spriteBatch);
foreach (Worm w in worms)
w.Draw(_spriteBatch);
printText.Print("Points:" + player.Points, _spriteBatch, 0, 0);
_spriteBatch.End();
base.Draw(gameTime);
}从PhysicalObject()fd继承的PowerUp()类
class Worm : PhysicalObject
{
double timeToDie; // active time of the worm
// Worm(), construct to create a worm
public Worm(Texture2D texture, float X, float Y, GameTime gameTime)
: base(texture, X, Y, 0, 2f)
{
timeToDie = gameTime.TotalGameTime.TotalMilliseconds + 5000;
}
/* Update(), check if the worm should stay alive */
public void Update(GameTime gameTime)
{
// kill worm if it stayed alive too long
if (timeToDie < gameTime.TotalGameTime.TotalMilliseconds)
isAlive = false;
}
}发布于 2021-04-02 17:33:28
忘记将bool变量设置为true
protected bool IsAlive; // changed to: protected bool IsAlive = true;https://stackoverflow.com/questions/66915665
复制相似问题