C# 在XNA中重构映射加载开关语句
我有一个switch语句,它接受一个元组,并根据元组的第二个值选择要进入的嵌套switch语句集。通过这种方式,一切都可以很好地工作,但由于每次switch语句占用的空间比必要的多,我需要在中心映射周围加载4个映射以实现平滑过渡。所以,我正在重构一个方法,现在只加载并绘制基本精灵。在加载精灵时,我遇到了一些content manager接收空引用异常的问题,并且不知道如何正确地将内容传递给Load方法 以下是我在Draw中的简短切换语句:C# 在XNA中重构映射加载开关语句,c#,oop,xna,C#,Oop,Xna,我有一个switch语句,它接受一个元组,并根据元组的第二个值选择要进入的嵌套switch语句集。通过这种方式,一切都可以很好地工作,但由于每次switch语句占用的空间比必要的多,我需要在中心映射周围加载4个映射以实现平滑过渡。所以,我正在重构一个方法,现在只加载并绘制基本精灵。在加载精灵时,我遇到了一些content manager接收空引用异常的问题,并且不知道如何正确地将内容传递给Load方法 以下是我在Draw中的简短切换语句: GraphicsDevice.Clear(Color.C
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);
}
public void LoadContent(ContentManager-theContentManager,string-theAssetName)
{
SpriteSize=contentmanager.Load(assetname);
}
我如何才能让Load方法注意到每个对象都有内容?到目前为止,我所尝试的一切都没有奏效。好的,如果我理解正确,您的加载总是有5个地图(实际地图和4个侧地图) 最多只加载3张地图有什么关系?让我们考虑一下
你知道:
玩家(位置、速度、移动方向),
地图(大小、离开地图的可行走方向) 示例地图
---------------------
| P | | | | |
---------------------
| | | | | |
---------------------
| | | | | |
---------------------
| | | | | | P = Player
---------------------
所以现在你可以说,如果p.Position
靠近地图边缘加载此地图在这里,它将位于顶部,左侧这将降低您的装载成本 现在,您可以通过向地图添加更多细节来扩展此功能,我们将其命名为Walkdirections
---------------------
| X | X | X | X | X |
---------------------
| P | | | | |
---------------------
| | | | | |
---------------------
| | | | | | P = Player
--------------------- X = Player can't pass this field
因此,作为地图的设计者,您知道他无法在地图14上排名靠前,因此您无需通过选中map.Walkdirections
只要他在负载范围内,就拿着你的旧地图,也许他想回去
Map 1 Map 2
--------------------- ---------------------
| | | | | | | X | X | X | X | X |
--------------------- ---------------------
| | | | | | | P | | | | |
--------------------- ---------------------
| | | | | | | | | | | |
--------------------- ---------------------
| | | | | | | | | | | | P = Player
--------------------- --------------------- X = Player can't pass this field
一些关于p速度的东西。你应该增加玩家速度的加载范围
我希望这将对您有所帮助顺便说一句,我目前对XNA一无所知,如果我理解正确,那么你的加载总是有5张地图(实际地图和4张侧地图) 最多只加载3张地图有什么关系?让我们考虑一下
你知道:
玩家(位置、速度、移动方向),
地图(大小、离开地图的可行走方向) 示例地图
---------------------
| P | | | | |
---------------------
| | | | | |
---------------------
| | | | | |
---------------------
| | | | | | P = Player
---------------------
所以现在你可以说,如果p.Position
靠近地图边缘加载此地图在这里,它将位于顶部,左侧这将降低您的装载成本 现在,您可以通过向地图添加更多细节来扩展此功能,我们将其命名为Walkdirections
---------------------
| X | X | X | X | X |
---------------------
| P | | | | |
---------------------
| | | | | |
---------------------
| | | | | | P = Player
--------------------- X = Player can't pass this field
因此,作为地图的设计者,您知道他无法在地图14上排名靠前,因此您无需通过选中map.Walkdirections
只要他在负载范围内,就拿着你的旧地图,也许他想回去
Map 1 Map 2
--------------------- ---------------------
| | | | | | | X | X | X | X | X |
--------------------- ---------------------
| | | | | | | P | | | | |
--------------------- ---------------------
| | | | | | | | | | | |
--------------------- ---------------------
| | | | | | | | | | | | P = Player
--------------------- --------------------- X = Player can't pass this field
一些关于p速度的东西。你应该增加玩家速度的加载范围
我希望这将对您有所帮助顺便说一下,我目前对XNA一无所知。对于空内容,您必须将原始内容管理器(在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;
}
对于空内容,必须将原始内容管理器(在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;
}
你的平稳过渡会是什么样子?你的精灵是一直在你窗口的中心,你的地图在移动,还是相反,如果你走出它,地图在切换?当我走出地图时,下一张地图朝着玩家的方向移动。当下一张地图经过玩家时,玩家开始移动地图,直到他到达对方。转换看起来类似于Zelda中的映射开关。它现在可以工作了吗?还是空引用仍然保留?原始类的类类型是什么,它继承了什么原始类是background,它有绘制和加载精灵的方法。因此,原始类中的
这个是typeOfbackground,不继承任何内容,对吗??那么,您的初始化部分在原始代码中的this.Content
的哪里?因为您需要在重构解决方案中对此进行调整,平滑过渡的效果如何?你的精灵是一直在你窗口的中心,你的地图在移动,还是相反,如果你走出它,地图在切换?当我走出地图时,下一张地图朝着玩家的方向移动。当下一张地图经过玩家时,玩家开始移动地图,直到他到达对方。转换看起来类似于Zelda中的映射开关。它现在可以工作了吗?还是空引用仍然保留?原始类的类类型是什么,它继承了什么原始类是background,它有绘制和加载精灵的方法。因此,原始类中的这个是typeOfbackground,不继承任何内容,对吗??那么,您的初始化部分在原始代码中的this.Content
的哪里?因为您需要在重构的solutionOk中对此进行调整,所以只有当我接近触发贴图转换时,才会加载下一个贴图。这肯定很有用,但它不能解决重构中的空引用。@ZeroPhase您的LoadContent()也重构了吗?如果没有,您应该检查您的contentmanager
值,因为该值为空,您可能无法填充。在contentmanager.Load(assetname)中发布您的Load()
,它不应该是空的吗代码>@ZeroPhase oh i s您从未初始化它ContentManager内容管理器代码>应该类似于ContentManager-ContentManager=newcontentmanager()代码>因为e