1

我有一个 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 方法注意到每个对象都有内容?到目前为止我所尝试的一切都没有奏效。

4

2 回答 2

2

对于空内容,您必须将原始内容管理器(在 Game1.cs 中)传递给您的类才能绘制,不要尝试创建新的。

    //add this to the drawNextBackground class
    //where the content manager in game1.cs is "Content"
    //Content is inherited from another class, and an example
    //should be visible in the LoadContent method in game1.cs        
    public void initialize(ContentManager contentManager)
    {
          theContentManager = contentManager;
    }
于 2013-04-30T13:00:37.763 回答
2

好的,如果我理解正确,您的加载始终有 5 个地图(实际地图和 4 个侧面地图)。

什么关于在 Max 上仅加载 3 个地图让我们想想
你知道的:
玩家(位置、速度、移动方向)
地图(大小、离开地图的步行方向)

示例地图

    ---------------------
    | P |   |   |   |   |
    ---------------------
    |   |   |   |   |   |
    ---------------------
    |   |   |   |   |   |
    ---------------------
    |   |   |   |   |   |   P = Player
    ---------------------

所以现在你可以说如果P.Position靠近地图边缘加载这张地图会在顶部,左边
这将降低你的加载成本

现在您可以通过向地图添加更多细节来扩展它,让我们将其命名为 Walkdirections

    ---------------------
    | X | X | X | X | X |
    ---------------------
    | P |   |   |   |   |
    ---------------------
    |   |   |   |   |   |
    ---------------------
    |   |   |   |   |   |   P = Player
    ---------------------   X = Player can't pass this field

因此,作为地图的设计者,您知道他无法在 Map 14 上名列前茅,因此您无需通过检查来加载它map.Walkdirections

只要他在负载范围内就拿着你的旧地图也许他想回去

            Map 1                Map 2
    --------------------- ---------------------
    |   |   |   |   |   | | X | X | X | X | X |
    --------------------- ---------------------
    |   |   |   |   |   | | P |   |   |   |   |
    --------------------- ---------------------
    |   |   |   |   |   | |   |   |   |   |   |
    --------------------- ---------------------
    |   |   |   |   |   | |   |   |   |   |   |   P = Player
    --------------------- ---------------------   X = Player can't pass this field

P.Speed的一些东西你应该随着播放器速度增加加载范围

我希望这会对你有所帮助
,因为我目前对 XNA 一无所知

于 2013-04-30T09:29:36.313 回答