silverlight – 如何使用XNA绘制动态网格

我正在尝试使用XNA框架绘制网格,这个网格在XNA执行期间应该有一个固定的维度,但应该给用户提供在启动游戏页面之前自定义它的机会(我正在构建我的应用程序)使用silverlight / xna模板).

有没有人对如何实现这一目标提出建议?

谢谢

最佳答案 设置tileSize,然后在所需网格的大小上绘制纹理.

这是一些重写的​​代码.这就是我开始使用2d数组生成tilemap的方法.

int tileSize = 32;
Vector2 position = Vector2.Zero;
Texture2D gridTexture;

int[,] map = new int[,]
{
    {1, 1, 0,},
    {0, 1, 1,},
    {1, 1, 0,},
};

然后在draw函数中添加这样的东西:

for (int i = 0; i <= map.GetUpperBound(0); i++)
{
    for (int j = 0; j <= map.GetUpperBound(1); j++)
    {
        int textureId = map[i, j];
        if (textureId != 0)
        {
            Vector2 texturePosition = new Vector2(i * tileSize, j * tileSize) + position;

            //Here you would typically index to a Texture based on the textureId.
            spriteBatch.Draw(gridTexture, texturePosition, null, Color.White, 0, Vector2.Zero, 1.0f, SpriteEffects.None, 0f);             
        }
    }
}
点赞