2011-11-06 7 views
4

我试图使用XNA框架绘制网格,这个网格应该有一个固定的维度,在XNA的执行过程中,但应该给用户提前定制它的机会启动游戏页面(我正在用silverlight/xna模板构建我的应用程序)。如何使用XNA绘制动态网格

有没有人有如何实现这一目标的建议?

谢谢

回答

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(); 
    } 
+0

问题是纹理是空的......我应该加载纹理??? – giulio

+0

要加载一个纹理,你可以这样做:gridTexture = Content.Load (“* YourAssetNameHere *”) ;确保你有纹理在解决方案资源管理器中的内容项目中使用相同的名称。另外,如果您不知道XNA的基本知识,可以从这些教程网站了解:http://www.riemers.net/ http://rbwhitaker.wikidot.com/xna-tutorials – annonymously

1

设置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,}, 
}; 

然后加入这样的事情你绘制函数:

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

似乎spriteBatch.Draw不能接受null作为参数:( – giulio

+0

让我看看你的代码,我仔细检查了,和我可以编译这个绘图语句 – jgallant