我有一个 switch 语句,它接受一个元组,并根据元组的第二个值选择要进入哪一组嵌套 switch 语句。这种方式一切都很好,但是因为每次 switch 语句最终占用的空间超出必要时,我都需要在中心地图周围加载 4 个地图以实现平滑过渡。因此,我正在重构一个方法,该方法现在只是加载和绘制基本精灵。我遇到了一些问题,内容管理器在加载精灵时收到空引用异常并且不知道如何正确地将内容传递给 Load 方法。
这是我在 Draw 中的缩写 Switch 语句:
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend);
Map.FindLocation(Map.LocationXY, ZoneDecider);
switch (Map.TupleHolder.Item1)
{
case 0:
{
switch (Map.TupleHolder.Item2)
{
case 0:
if (LoadNextMap)
{
if (Right)
{
backgroundCenter.LoadContent(this.Content,"BackgroundBottom");
backgroundCenter.position = new Vector2(0, 0);
}
else if (Left)
{
backgroundCenter.LoadContent(this.Content, "BackgroundBottom");
backgroundCenter.position = new Vector2(0, 0);
}
else if (Top)
{
backgroundCenter.LoadContent(this.Content, "BackgroundBottomRight");
backgroundCenter.position = new Vector2(0, 0);
}
else if(Bottom)
{
backgroundCenter.LoadContent(this.Content, "BackgroundRight");
backgroundCenter.position = new Vector2(0, 0);
}
backgroundWest.LoadContent(this.Content, "BackgroundBottom");
backgroundWest.position = new Vector2(westTransition, 0);
backgroundEast.LoadContent(this.Content, "BackgroundBottomRight");
backgroundEast.position = new Vector2(eastTransition, 0);
backgroundNorth.LoadContent(this.Content, "BackgroundMid");
backgroundNorth.position = new Vector2(0, northTransition);
backgroundSouth.LoadContent(this.Content, "BackgroundBottomRight");
backgroundSouth.position = new Vector2(0, southTransition);
LoadNextMap = false;
}
//new SpriteBatch(graphicsDevice)
backgroundCenter.Draw(this.spriteBatch);
//Drawbackground.drawBackground(backgroundWest, backgroundEast,
// backgroundNorth, backgroundSouth);
backgroundWest.Draw(this.spriteBatch);
backgroundEast.Draw(this.spriteBatch);
backgroundNorth.Draw(this.spriteBatch);
backgroundSouth.Draw(this.spriteBatch);
break;
这并不理想,因此要开始重构,我将 if 语句的部分移到新方法中。
新方法:
public class DrawNextBackground
{
SpriteBatch spriteBatch;
ContentManager theContentManager;
public void drawBackground(Background backgroundWest,
Background backgroundEast, Background backgroundNorth, Background backgroundSouth)
{
float eastTransition = 1068;
float westTransition = -1068;
float northTransition = -936;
float southTransition = 936;
backgroundWest.LoadContent(this.theContentManager, "BackgroundBottom");
backgroundWest.position = new Vector2(westTransition, 0);
backgroundEast.LoadContent(this.theContentManager, "BackgroundBottomRight");
backgroundEast.position = new Vector2(eastTransition, 0);
backgroundNorth.LoadContent(this.theContentManager, "BackgroundMid");
backgroundNorth.position = new Vector2(0, northTransition);
backgroundSouth.LoadContent(this.theContentManager, "BackgroundBottomRight");
backgroundSouth.position = new Vector2(0, southTransition);
backgroundWest.Draw(this.spriteBatch);
backgroundEast.Draw(this.spriteBatch);
backgroundNorth.Draw(this.spriteBatch);
backgroundSouth.Draw(this.spriteBatch);
}
}
加载方法:
public void LoadContent(ContentManager theContentManager, string theAssetName)
{
SpriteSize = theContentManager.Load<Texture2D>(theAssetName);
}
我怎样才能让 Load 方法注意到每个对象都有内容?到目前为止我所尝试的一切都没有奏效。