我在使用MonoStudio和Visual Studio 2017编写Asteroid Destroyer迷你游戏时发现了一件很奇怪的事情。在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
类中添加其余代码。 Spaceship
类不包含列表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的运行有关。如果当前版本中存在错误,我的Visual Studio配置会以某种方式让先前的工作版本运行...但是我已经找到了更改此设置的方法,所以谜团得以解决!
答案 0 :(得分:1)
显然,设置中有一个选项,如果出现错误,Visual Studio可以运行该项目的以前的工作版本,该错误在我的安装中默认启用(除非我无意中错误地启用了该选项)。
在尝试调查问题时,我在另一个问题中感谢this answer找到了解决方案:
尝试更改此值:
- 工具
- 选项
- 项目与解决方案
- 构建并运行
- 运行时,如果发生构建或部署错误:请勿启动