【问题标题】:How to draw a dynamic Grid with XNA如何使用 XNA 绘制动态网格
【发布时间】:2011-11-06 15:24:55
【问题描述】:

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

有人对如何实现这一目标有建议吗?

谢谢

【问题讨论】:

    标签: silverlight windows-phone-7 xna grid


    【解决方案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 作为参数 :(
    • 显示你的代码。我仔细检查了一下,我可以编译这个 draw 语句。
    【解决方案2】:
        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("YourAssetNameHere");确保您的解决方案资源管理器的内容项目中有同名纹理。另外,如果您不了解 XNA 的基础知识,您可以从以下教程网站学习:riemers.netrbwhitaker.wikidot.com/xna-tutorials
    猜你喜欢
    • 1970-01-01
    • 2021-12-29
    • 1970-01-01
    • 2015-03-17
    • 1970-01-01
    • 2011-02-17
    • 1970-01-01
    • 2010-11-01
    • 1970-01-01
    相关资源
    最近更新 更多