问题描述
我一直在绘制轴测图用C#/ XNA游戏工作室,而我已经得到了pretty的到目前为止,它看起来不正确的,我想知道是否有人可以提供帮助。
I've been working on drawing an isometric map with C# / XNA Game Studio, and while I've gotten pretty far it doesn't look quite right and I was wondering if anybody can help.
这里的code我绘制地图:
Here's the code I have for drawing the map:
public void Draw(SpriteBatch theBatch, int drawX, int drawY)
{
if ((drawY % 2 == 0))
theBatch.Draw(tileTexture, new Rectangle((drawX * width), (drawY * length / 2), width, length), Color.White);
else
theBatch.Draw(tileTexture, new Rectangle(((drawX * width) + (width / 2)), (drawY * length / 2), width, length), Color.White);
}
此方法中的code的作用就好像它是一个内嵌套循环,拉丝左至右,从上到下。当y值是奇数,该行被转移了,以适应,但它看起来有点假。
The code within this method acts as if it were inside a nested for loop, drawing left to right, top to bottom. When the y-value is odd, the row is shifted over to fit, however it looks a bit off.
这是产生输出的8X5图:
This is the produced output for an 8x5 map:
正如你所看到的,它并不完全看的权利,我不知道它的一个问题,在我的code中的数学,或者如果它有做的顺序一切都被绘制我很新的C#和精灵的工作,所以任何帮助将是很大的AP preciated。
As you can see, it doesn't quite look right, and I'm not sure if its an issue with the math in my code, or if it has to do with the order everything is being drawn in. I'm very new to C# and working with sprites, so any help would be greatly appreciated.
由于它可能会有所帮助,这里是code中的其他相关部分绘制地图。
Because it might be helpful, here is the other relevant parts of code which draw the map.
整个瓷砖类:
public class Tile
{
// Dimension variables
int height;
int width;
int length;
String type;
Texture2D tileTexture;
Vector2 coordinates;
///
/// Tile Constructor
///
public Tile(ContentManager theContent, String theType, Vector2 theCoordinates)
{
width = 68;
length = 46;
type = theType;
coordinates = theCoordinates;
// Sets the right texture to the texture ref
if (theType == "grass")
tileTexture = theContent.Load<Texture2D>(@"Tiles\iso_grass");
}
///
/// Draws the tile at the given location
///
public void Draw(SpriteBatch theBatch, int drawX, int drawY)
{
if ((drawY % 2 == 0))
theBatch.Draw(tileTexture, new Rectangle((drawX * width), (drawY * length / 2), width, length), Color.White);
else
theBatch.Draw(tileTexture, new Rectangle(((drawX * width) + (width / 2)), (drawY * length / 2), width, length), Color.White);
}
}
在TileRow类,其中包含瓷砖的一行。
The TileRow class, which holds one row of tiles.
public class TileRow
{
public List<Tile> Row = new List<Tile>();
public int rowLength;
public TileRow(int theLength, int yIndex, ContentManager theContent)
{
rowLength = theLength;
Tile thisTile;
// Here the tiles are created and added to the row
for (int x = 0; x < rowLength; x++)
{
thisTile = new Tile(theContent, "grass", new Vector2(x, yIndex));
Row.Add(thisTile);
}
}
///
/// Draw -- invokes the draw method of each tile in the row
///
public void DrawRow(SpriteBatch theBatch, int currentY)
{
for (int x = 0; x < rowLength; x++)
{
Row[x].Draw(theBatch, currentY, x);
}
}
}
}
和MapStruct类,其中包含所有行
and the MapStruct class, which holds all the rows
public class MapStruct
{
public List<TileRow> allRows = new List<TileRow>();
int depth;
// Constructor
public MapStruct(ContentManager theContent, int theDepth, int rowLength)
{
depth = theDepth;
TileRow thisRow;
// Here we make a row of tiles for each level of depth
for (int y = 0; y < depth; y++)
{
thisRow = new TileRow(rowLength, depth, theContent);
allRows.Add(thisRow);
}
}
///
/// Draw - this method invokes the draw method in each tile row, which then draws each tile
///
public void DrawMap(SpriteBatch theBatch)
{
for (int y = 0; y < depth; y++)
{
allRows[y].DrawRow(theBatch, y);
}
}
}
这是我怎么能解决这个问题,以及我如何能提高我的code建议任何帮助将是很大的AP preciated!
Any help on how I could fix this issue, as well as advice on how I could improve my code would be greatly appreciated!
推荐答案
我beleive BerggreenDK是正确的,但您的评论使之看起来你误解了他的答案。你的瓷砖需要一个Y轴偏移只包括绿色表面区域屏幕的高度来绘制。这样,绘制完整图块大小,但偏移长度的行 - 。5(其中10估计出的偏移和5是一半,由于各行应只有一半的距离来偏移)
I beleive BerggreenDK is right, but your comment make it seem you misunderstood his answer. Your tiles need to be drawn at an Y offset that only includes the green surface areas "screen height". So, draw the full tile size, but offset the rows with length - 5 (Where 10 is the estimated offset and 5 is half that, as each row should be offset by only half the distance).
theBatch.Draw(tileTexture, new Rectangle((drawX * width), (drawY * (length) / 2), width, length - 5), Color.White);
...如果我得到正确的参数。
...if I got the parameters right.
此外,各行需要被从后向前拉伸,但它们似乎是从左到右绘制?我不知道XNA,所以不能证明$ C $ C,以解决这个问题......当我仔细一看有些砖一排渐行渐远,是由瓷砖过度拉更近。我不知道XNA并没有得到绘制顺序如何确定,因此无法提供修复该...
Also, the rows need to be drawn from back to front, but they seem to be drawn from left to right? I don't know XNA, so can't show code to fix that...when I look closely some tiles in a row "further away" is over-drawn by a tile "closer". I don't know XNA and don't get how the drawing order is determined, so can't offer a fix for that...
这篇关于绘制等距Tilemaps的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!