完成程序后列表似乎仍然存在
List seems to still exist after finishing program
我在 Visual Studio 2017 使用 MonoGame 编写小行星驱逐舰小游戏时发现了一件很奇怪的事情。在 LoadContent()
方法中,我将我的宇宙飞船的纹理加载到 Sprites 列表中,它是现场创作。然后将此列表输入 Spaceship
构造函数,以便宇宙飞船获得其纹理。代码如下:
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
List<Sprite> spaceship1Sprites = new List<Sprite>
{
new Sprite(Content, "Spaceship1On"),
new Sprite(Content, "Spaceship1Off"),
new Sprite(Content, "Bullet1")
};
spaceship1 = new Spaceship(spaceship1Sprites, new Vector2(graphics.GraphicsDevice.DisplayMode.Width / 2, graphics.GraphicsDevice.DisplayMode.Height / 2))
{
Controls = new Input(Keys.W, Keys.D, Keys.A, Keys.Space)
};
}
奇怪的是,如果在 运行 一次程序后,我删除了定义 Sprite 列表的行,
//List<Sprite> spaceship1Sprites = new List<Sprite>
//{
// new Sprite(Content, "Spaceship1On"),
// new Sprite(Content, "Spaceship1Off"),
// new Sprite(Content, "Bullet1")
//};
程序仍将 运行,即使该列表未在程序的其他任何地方定义,并且 Spaceship
构造函数应该获取一个不存在的列表作为其第一个参数。这怎么还能用?
这不是阻止我的程序运行的原因,但我仍然想知道是什么原因,以便我对 C# 的工作原理有更多了解。
编辑:在 Game1
class 中添加其余代码。 Spaceship
class不包含列表spaceship1Sprites
,它有三个精灵的三个字段,在构造函数中加载。
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;
using System.IO;
namespace Asteroid_Destroyer
{
public class Game1 : Game
{
public static GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
KeyboardState currentKeyboardState, previousKeyboardState;
bool paused = false;
Spaceship spaceship1;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
//Change screen resolution
graphics.PreferredBackBufferWidth = graphics.GraphicsDevice.DisplayMode.Width;
graphics.PreferredBackBufferHeight = graphics.GraphicsDevice.DisplayMode.Height;
graphics.IsFullScreen = true;
graphics.HardwareModeSwitch = false; //fullscreen borderless
graphics.ApplyChanges();
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
List<Sprite> spaceship1Sprites = new List<Sprite>
{
new Sprite(Content, "Spaceship1On"),
new Sprite(Content, "Spaceship1Off"),
new Sprite(Content, "Bullet1")
};
spaceship1 = new Spaceship(spaceship1Sprites, new Vector2(graphics.GraphicsDevice.DisplayMode.Width / 2, graphics.GraphicsDevice.DisplayMode.Height / 2))
{
Controls = new Input(Keys.W, Keys.D, Keys.A, Keys.Space)
};
}
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
protected override void Update(GameTime gameTime)
{
currentKeyboardState = Keyboard.GetState();
if (currentKeyboardState.IsKeyDown(Keys.Escape))
Exit();
if(currentKeyboardState.IsKeyDown(Keys.P) && previousKeyboardState.IsKeyUp(Keys.P))
paused = paused ? false : true;
if (!paused)
spaceship1.Update();
base.Update(gameTime);
previousKeyboardState = currentKeyboardState;
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
spriteBatch.Begin();
spaceship1.Draw(spriteBatch);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
编辑 2:已解决!正如预期的那样,这与代码无关,而是与 Visual Studio 的内容有关 运行ning。如果当前构建中有错误,我的 Visual Studio 配置让以前的工作版本 运行 不知何故......但我已经找到了改变这个的设置,所以谜团解决了!
显然,设置中有一个选项可以在出现错误时启用 Visual Studio 到 运行 项目的先前工作构建,这在我的安装中默认启用(除非我无意中错误地启用了该选项)。
我在尝试调查问题时在另一个问题中找到了解决方案,感谢 this answer:
Try changing this value:
- Tools
- Options
- Projects and Solutions
- Build and Run
- On Run, when build or deployment errors occur: Do not Launch
我在 Visual Studio 2017 使用 MonoGame 编写小行星驱逐舰小游戏时发现了一件很奇怪的事情。在 LoadContent()
方法中,我将我的宇宙飞船的纹理加载到 Sprites 列表中,它是现场创作。然后将此列表输入 Spaceship
构造函数,以便宇宙飞船获得其纹理。代码如下:
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
List<Sprite> spaceship1Sprites = new List<Sprite>
{
new Sprite(Content, "Spaceship1On"),
new Sprite(Content, "Spaceship1Off"),
new Sprite(Content, "Bullet1")
};
spaceship1 = new Spaceship(spaceship1Sprites, new Vector2(graphics.GraphicsDevice.DisplayMode.Width / 2, graphics.GraphicsDevice.DisplayMode.Height / 2))
{
Controls = new Input(Keys.W, Keys.D, Keys.A, Keys.Space)
};
}
奇怪的是,如果在 运行 一次程序后,我删除了定义 Sprite 列表的行,
//List<Sprite> spaceship1Sprites = new List<Sprite>
//{
// new Sprite(Content, "Spaceship1On"),
// new Sprite(Content, "Spaceship1Off"),
// new Sprite(Content, "Bullet1")
//};
程序仍将 运行,即使该列表未在程序的其他任何地方定义,并且 Spaceship
构造函数应该获取一个不存在的列表作为其第一个参数。这怎么还能用?
这不是阻止我的程序运行的原因,但我仍然想知道是什么原因,以便我对 C# 的工作原理有更多了解。
编辑:在 Game1
class 中添加其余代码。 Spaceship
class不包含列表spaceship1Sprites
,它有三个精灵的三个字段,在构造函数中加载。
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;
using System.IO;
namespace Asteroid_Destroyer
{
public class Game1 : Game
{
public static GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
KeyboardState currentKeyboardState, previousKeyboardState;
bool paused = false;
Spaceship spaceship1;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
//Change screen resolution
graphics.PreferredBackBufferWidth = graphics.GraphicsDevice.DisplayMode.Width;
graphics.PreferredBackBufferHeight = graphics.GraphicsDevice.DisplayMode.Height;
graphics.IsFullScreen = true;
graphics.HardwareModeSwitch = false; //fullscreen borderless
graphics.ApplyChanges();
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
List<Sprite> spaceship1Sprites = new List<Sprite>
{
new Sprite(Content, "Spaceship1On"),
new Sprite(Content, "Spaceship1Off"),
new Sprite(Content, "Bullet1")
};
spaceship1 = new Spaceship(spaceship1Sprites, new Vector2(graphics.GraphicsDevice.DisplayMode.Width / 2, graphics.GraphicsDevice.DisplayMode.Height / 2))
{
Controls = new Input(Keys.W, Keys.D, Keys.A, Keys.Space)
};
}
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
protected override void Update(GameTime gameTime)
{
currentKeyboardState = Keyboard.GetState();
if (currentKeyboardState.IsKeyDown(Keys.Escape))
Exit();
if(currentKeyboardState.IsKeyDown(Keys.P) && previousKeyboardState.IsKeyUp(Keys.P))
paused = paused ? false : true;
if (!paused)
spaceship1.Update();
base.Update(gameTime);
previousKeyboardState = currentKeyboardState;
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
spriteBatch.Begin();
spaceship1.Draw(spriteBatch);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
编辑 2:已解决!正如预期的那样,这与代码无关,而是与 Visual Studio 的内容有关 运行ning。如果当前构建中有错误,我的 Visual Studio 配置让以前的工作版本 运行 不知何故......但我已经找到了改变这个的设置,所以谜团解决了!
显然,设置中有一个选项可以在出现错误时启用 Visual Studio 到 运行 项目的先前工作构建,这在我的安装中默认启用(除非我无意中错误地启用了该选项)。
我在尝试调查问题时在另一个问题中找到了解决方案,感谢 this answer:
Try changing this value:
- Tools
- Options
- Projects and Solutions
- Build and Run
- On Run, when build or deployment errors occur: Do not Launch