我試圖使用XNA框架繪製網格,這個網格應該有一個固定的維度,在XNA的執行過程中,但應該給用戶提前定製它的機會啓動遊戲頁面(我正在用silverlight/xna模板構建我的應用程序)。如何使用XNA繪製動態網格
有沒有人有如何實現這一目標的建議?
謝謝
我試圖使用XNA框架繪製網格,這個網格應該有一個固定的維度,在XNA的執行過程中,但應該給用戶提前定製它的機會啓動遊戲頁面(我正在用silverlight/xna模板構建我的應用程序)。如何使用XNA繪製動態網格
有沒有人有如何實現這一目標的建議?
謝謝
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();
}
設置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);
}
}
}
問題是紋理是空的......我應該加載紋理??? – giulio
要加載一個紋理,你可以這樣做:gridTexture = Content.Load(「* YourAssetNameHere *」) ;確保你有紋理在解決方案資源管理器中的內容項目中使用相同的名稱。另外,如果您不知道XNA的基本知識,可以從這些教程網站了解:http://www.riemers.net/ http://rbwhitaker.wikidot.com/xna-tutorials –
annonymously