SetType(type);
for (int x = (Globals.tileSize + (int)position.X); x < ((int)position.X + (Globals.screenWidth - Globals.tileSize)); x += Globals.tileSize)
{
for (int y = (Globals.tileSize + (int)position.Y); y < ((int)position.Y + (Globals.screenHeight - Globals.tileSize)); y += Globals.tileSize)
{
tileType = tileFillTypes[random.Next(1, tileFillTypes.Count())];
Tile tile = new Tile(new Vector2(x, y), tileType, false);
tiles.Add(tile);
}
}
地圖生成調用此類來在一個方框中繪製隨機拼貼。 SetType(type)被調用,它是這樣做的:C#XNA - 隨機房生成
void SetType(int type)
{
if (type == 1) //ROOM TYPE
{
tileWallTypesBottom = new string[] { "", "stonewallBottom1", "stonewallBottom2", "stonewallBottom3" };
tileFillTypes = new String[] { "" };
}
else if (type == 2)
{
tileWallTypesBottom = new string[] { "", "stonewallBottom1", "stonewallBottom2", "stonewallBottom3" };
tileWallTypesLeft = new string[] { "", "stonewallLeft1", "stonewallLeft2", "stonewallLeft3" };
tileWallTypesRight = new string[] { "", "stonewallRight1", "stonewallRight2", "stonewallRight3" };
tileWallTypesTop = new string[] { "", "stonewallTop1", "stonewallTop2", "stonewallTop3" };
tileFillTypes = new String[] { "", "dirt1", "dirt1", "dirt1", "dirt1", "dirt1", "dirt1", "dirt1", "dirt1", "dirt1", "dirt2", "dirtGrassPatch", "dirt4", "dirt4", "dirt4", };
tileWallCornerType = new string[] { "", "stonewallTopLeft", "stonewallTopRight", "stonewallBottomRight", "stonewallBottomLeft" };
}
}
這設置我的數組,我可以隨機選擇哪些貼圖。 我的問題是當我在房間的每個實例中使用此代碼生成多個房間時,房間不會從彼此隨機出來。我試過設置random = new Random();在每種情況下,它們總是具有相同的瓦片輸出。
不要創建一個新的** **隨機每次,因爲它不會特別隨意。 _ [你想知道更多嗎?](http://www.dotnetperls.com/random)_ – MickyD 2014-10-11 05:12:14