无法加载我的 MonoGame 中的 "power-up",编译没有错误
Can't get the "power-up" in my MonoGame to load, no errors from compiling
在屏幕上 appear/load 启动时遇到一点问题,我在 VS19 中的错误为零。
蠕虫 class 继承自与玩家和敌人 Class 相同的父级,并且它们都加载。
它们之间的唯一区别是 Worm 在 Update() 而不是 LoadContent() 中加载,所以我可以
只假设那里的代码有问题。
如果您发现任何奇怪的地方,请告诉我,在此先感谢!
全局声明了一个列表和一个纹理类型:
List<Worm> worms;
Texture2D wormSprite;
在加载 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
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;
}
}
忘记将布尔变量设置为 true
protected bool IsAlive; // changed to: protected bool IsAlive = true;
在屏幕上 appear/load 启动时遇到一点问题,我在 VS19 中的错误为零。 蠕虫 class 继承自与玩家和敌人 Class 相同的父级,并且它们都加载。 它们之间的唯一区别是 Worm 在 Update() 而不是 LoadContent() 中加载,所以我可以 只假设那里的代码有问题。
如果您发现任何奇怪的地方,请告诉我,在此先感谢!
全局声明了一个列表和一个纹理类型:
List<Worm> worms;
Texture2D wormSprite;
在加载 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 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;
}
}
忘记将布尔变量设置为 true
protected bool IsAlive; // changed to: protected bool IsAlive = true;