如何使用XNA绘制动态网格

4
我正在尝试使用XNA框架绘制网格,这个网格应该在XNA执行期间具有固定的尺寸,但在启动游戏页面之前应该给用户自定义的机会(我正在使用silverlight/xna模板构建我的应用程序)。
是否有人有关于如何实现这个目标的建议呢?
谢谢。
2个回答

1

设置一个tileSize,然后在你想要的网格大小上绘制纹理。

这里是一些修改过的代码。这是我生成瓦片地图的起点,使用了一个二维数组。

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

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

然后在你的绘制函数中添加类似以下的内容:

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);             
        }
    }
}

似乎 spriteBatch.Draw 不接受 null 作为参数 :( - giulio
展示你的代码。我进行了双重检查,可以编译这个绘制语句。 - jgallant

1
    ContentManager contentManager;
    GameTimer timer;
    SpriteBatch spriteBatch;
    LifeGrid life;


    int tileSize = 32;
    Vector2 position = Vector2.Zero;
    Texture2D gridTexture;
    int[,] map;

    public GamePage()
    {
        InitializeComponent();

        // Get the content manager from the application
        contentManager = (Application.Current as App).Content;

        // Create a timer for this page
        timer = new GameTimer();
        //timer.UpdateInterval = TimeSpan.FromTicks(333333);
        timer.UpdateInterval = TimeSpan.Zero;
        timer.Update += OnUpdate;
        timer.Draw += OnDraw;
        List<Position> p = new List<Position>();
        p.Add(new Position(1,1));
        p.Add(new Position(1,4));
        p.Add(new Position(1,5));
        p.Add(new Position(1,6));
        p.Add(new Position(1,7));
        this.life = new LifeGrid(10, 10, p);


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

        // LayoutUpdated += new EventHandler(GamePage_LayoutUpdated);
    }
    /// <summary>
    /// Allows the page to draw itself.
    /// </summary>
    private void OnDraw(object sender, GameTimerEventArgs e)
    {
        // SharedGraphicsDeviceManager.Current.GraphicsDevice.Clear(Color.CornflowerBlue);
       // SharedGraphicsDeviceManager.Current.GraphicsDevice.Clear(Color.Black);
        // Draw the sprite
        spriteBatch.Begin();

        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);

                }
            }
        }


        spriteBatch.End();
    }

要加载纹理,您可以执行以下操作:gridTexture = Content.Load<Texture2D>("YourAssetNameHere"); 确保在解决方案资源管理器中的Content项目中有相同名称的纹理。此外,如果您不了解XNA的基础知识,可以从以下教程网站学习:http://www.riemers.net/ http://rbwhitaker.wikidot.com/xna-tutorials - annonymously

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接