Skip to main content
 首页 » 编程设计

c#之在 XNA 中重构映射加载 switch 语句

2024年02月13日12wuhuacong

我有一个 switch 语句,它接受一个元组,并根据元组的第二个值选择要进入哪一组嵌套的 switch 语句。用这种方式一切都很好,但是因为我需要在中心 map 周围加载 4 个 map 以便每次 switch 语句最终占用比必要更多的空间时平滑过渡。因此,我正在重构一个方法,该方法现在只加载和绘制基本 Sprite 。我遇到了一些问题,内容管理器在加载 sprite 时收到空引用异常,并且不知道如何将内容正确传递给 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 方法注意到每个对象都有内容?到目前为止,我尝试过的一切都没有奏效。

请您参考如下方法:

对于空内容,您必须将原始内容管理器(在 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; 
    }