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