C# 用于随机生成的标高的XNA小地图
我正在做一个自上而下的类似流氓的游戏,关卡是随机生成的,但我遇到了一个问题:我似乎无法启动一个小地图 代码如下:C# 用于随机生成的标高的XNA小地图,c#,xna,C#,Xna,我正在做一个自上而下的类似流氓的游戏,关卡是随机生成的,但我遇到了一个问题:我似乎无法启动一个小地图 代码如下: for (y = 0; y <= 19; y++) { for (x = 0; x <= 19; x++) { if (level[x, y] != null) { Draw(spriteB
for (y = 0; y <= 19; y++)
{
for (x = 0; x <= 19; x++)
{
if (level[x, y] != null)
{
Draw(spriteBatch);
}
}
x = 0;
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(Resources.room_minimap, new Rectangle(30, 20, 30, 20), null, Color.White, 0f, new Vector2(0, 0), SpriteEffects.None, 0f);
}
for(y=0;y您的LevelGen
类不应处理小地图的渲染,可能是您的游戏或关卡类
将您的级别
变量移出方法并移到类中。确保它是公共的,以便其他类可以访问它
public class LevelGen
{
int x = 20;
int y = 20;
public Room[,] level = new Room[x, y];
//...
在Game
类中的protectedoverride void Draw(GameTime GameTime)
方法中,将渲染代码移到那里。您需要每帧绘制一次小地图,而不是在世界生成过程中绘制一次
移动小地图代码后,Game1.cs(或类似)绘图代码应如下所示:
protected override void Draw(GameTime gameTime)
{
for (y = 0; y <= 19; y++)
for (x = 0; x <= 19; x++)
if (Map.level[x, y] != null)
spriteBatch.Draw(Resources.room_minimap, new Rectangle(30, 20, 30, 20), null, Color.White, 0f, new Vector2(0, 0), SpriteEffects.None, 0f);
}
protected override void Draw(游戏时间游戏时间)
{
对于(y=0;yIm我的游戏,我在更新方法中使用以下代码:
Game1.graphicsDeviceManager.GraphicsDevice.SetRenderTarget(miniMapDrawRenderTarget2D);
Game1.graphicsDeviceManager.GraphicsDevice.Clear(Color.Transparent);
Game1.spriteBatch.Begin(SpriteSortMode.Deferred, new BlendState
{
ColorSourceBlend = Blend.One,
ColorDestinationBlend = Blend.SourceAlpha,
AlphaSourceBlend = Blend.One,
AlphaDestinationBlend = Blend.SourceAlpha
});
if (miniMapTexture2d != null)
{
sourceRectangle.X = (int)(((Game1.mission.player.position.X - Config.chunkSize * playerRegion.X) / Config.tileSize) + (Config.tilesPerChunk * 0.5f));
sourceRectangle.Y = (int)((-(Game1.mission.player.position.Y - Config.chunkSize * playerRegion.Y) / Config.tileSize) + (Config.tilesPerChunk * 1.5f));
Game1.spriteBatch.Draw(miniMapTexture2d, new Rectangle(4, 4, 192, 192), sourceRectangle, Color.White);
}
Game1.mission.textures2D["miniMapMask"].drawOnScreen();
Game1.spriteBatch.End();
Game1.graphicsDeviceManager.GraphicsDevice.SetRenderTarget(null);
Game1.graphicsDeviceManager.GraphicsDevice.ScissorRectangle = Game1.display.centerViewport.Bounds;
Game1.graphicsDeviceManager.GraphicsDevice.Viewport = Game1.display.centerViewport;
然后在绘制方法上:
Game1.spriteBatch.Draw((Texture2D)miniMapDrawRenderTarget2D, new Vector2(800, 0), Color.White);
没有绘制任何东西,下面是更新的代码:小错误:您的矩形对于每个对象都是相同的,您的意思是:新矩形(30*x,20*y,30,20)
?另外,在spriteBatch.Draw()行上放置一个断点(如果您不知道如何操作,请用谷歌搜索),以确保它被命中(可能级别[x,y]
真的是空的吗?)编辑:看起来你也在调用Map()
每次更新,是这样吗?>你的意思是:新建矩形(30*x,20*y,30,20)?是的,我忘记了>看起来你也在调用Map()每次更新,都是这样吗?我想确保每次创建关卡时它都会创建一个小地图,我会在完成小地图后更改它。是的,这样他们就不会在一个地方绘制,尽管我想如果是这样的话,你至少会看到一个瓷砖。我按enter键进入下一行,然后我发布了评论,该死!