无法在 XNA 中的另一个方法中使用变量
后面我会深入解释。所以这是我的代码,我们有一个 Elevation[] 变量,每个 Elevation 都会获得一个随机数:
public void elevation()
{
for (x = (int)Width - 1; x >= 0; x--)
{
for (y = (int)Width - 1; y >= 0; y--)
{
y = rand.Next((int)MaxElevation); //random number for each y.
Elevation[x] = y; //each Elevation gets a random number.
}
}
}
之后,我尝试在绘制方法中使用这个随机数,如下所示:
public void Draw(SpriteBatch spriteBatch)
{
for (x = (int)Width - 1; x >= 0; x--)
{
spriteBatch.Draw(Pixel, new Rectangle((int)Position.X + x, (int)Position.Y - Elevation[x], 1, (int)Height), Color.White);
//HERE, I try to acces the random number for each Elevation (y value). But I get 0 everywhere.
}
}
如何访问这个随机数?
如果我这样做:
public void Draw(SpriteBatch spriteBatch)
{
for (x = (int)Width - 1; x >= 0; x--)
{
for (y = (int)Width - 1; y >= 0; y--)
{
y = rand.Next((int)MaxElevation);
spriteBatch.Draw(Pixel, new Rectangle((int)Position.X + x, (int)Position.Y - Elevation[y], 1, (int)Height), Color.White);
}
}
}
我将能够访问随机数,但它将更新每一帧并且随机数将会改变。所以我需要计算一次然后使用它们。
这是所有代码:
namespace procedural_2dterrain
{
class Terrain
{
Texture2D Pixel;
Vector2 Position;
Random rand;
int[] Elevation;
float MaxElevation;
float MinElevation;
float Width;
float Height;
int x;
int y;
public void Initialize( ContentManager Content, float maxElevation, float minElevation, float width, float height, Vector2 position)
{
Pixel = Content.Load<Texture2D>("pixel");
rand = new Random();
Elevation = new int[(int)width];
MaxElevation = maxElevation;
MinElevation = minElevation;
Width = width;
Height = height;
Position = position;
elevation();
}
public void Update()
{
}
public void elevation()
{
for (x = (int)Width - 1; x >= 0; x--)
{
for (y = (int)Width - 1; y >= 0; y--)
{
y = rand.Next((int)MaxElevation);
Elevation[x] = y;
}
}
}
public void Draw(SpriteBatch spriteBatch)
{
for (x = (int)Width - 1; x >= 0; x--)
{
spriteBatch.Draw(Pixel, new Rectangle((int)Position.X + x, (int)Position.Y - Elevation[x], 1, (int)Height), Color.White);
}
}
}
}
I will explain in depth after. So here is my code, we have an Elevation[] variable and each Elevation gets a random number:
public void elevation()
{
for (x = (int)Width - 1; x >= 0; x--)
{
for (y = (int)Width - 1; y >= 0; y--)
{
y = rand.Next((int)MaxElevation); //random number for each y.
Elevation[x] = y; //each Elevation gets a random number.
}
}
}
After this I try to use this random number in the draw method like this:
public void Draw(SpriteBatch spriteBatch)
{
for (x = (int)Width - 1; x >= 0; x--)
{
spriteBatch.Draw(Pixel, new Rectangle((int)Position.X + x, (int)Position.Y - Elevation[x], 1, (int)Height), Color.White);
//HERE, I try to acces the random number for each Elevation (y value). But I get 0 everywhere.
}
}
How can I acces this random number?
If I do that:
public void Draw(SpriteBatch spriteBatch)
{
for (x = (int)Width - 1; x >= 0; x--)
{
for (y = (int)Width - 1; y >= 0; y--)
{
y = rand.Next((int)MaxElevation);
spriteBatch.Draw(Pixel, new Rectangle((int)Position.X + x, (int)Position.Y - Elevation[y], 1, (int)Height), Color.White);
}
}
}
I will be able to acces the random numbers, but it will update every frame and the random numbers will change. So I need to calculate them once and then use them.
Here is all the code:
namespace procedural_2dterrain
{
class Terrain
{
Texture2D Pixel;
Vector2 Position;
Random rand;
int[] Elevation;
float MaxElevation;
float MinElevation;
float Width;
float Height;
int x;
int y;
public void Initialize( ContentManager Content, float maxElevation, float minElevation, float width, float height, Vector2 position)
{
Pixel = Content.Load<Texture2D>("pixel");
rand = new Random();
Elevation = new int[(int)width];
MaxElevation = maxElevation;
MinElevation = minElevation;
Width = width;
Height = height;
Position = position;
elevation();
}
public void Update()
{
}
public void elevation()
{
for (x = (int)Width - 1; x >= 0; x--)
{
for (y = (int)Width - 1; y >= 0; y--)
{
y = rand.Next((int)MaxElevation);
Elevation[x] = y;
}
}
}
public void Draw(SpriteBatch spriteBatch)
{
for (x = (int)Width - 1; x >= 0; x--)
{
spriteBatch.Draw(Pixel, new Rectangle((int)Position.X + x, (int)Position.Y - Elevation[x], 1, (int)Height), Color.White);
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题出在您的
elevation()
方法上。您使用 y 作为内部循环索引器,并在循环内为其分配一个值。所以循环开始,y被分配一个随机值。当它继续循环时,它会递减,然后测试是否为>=0
。该循环唯一退出的情况是为y
分配了随机数0
。这就是为什么你所有的Elevation
都是零。我有点困惑为什么你认为你需要一个内循环。尝试:
The problem is with your
elevation()
method. You are usingy
as your inner loop indexer and also assigning a value to it within the loop. So the loop starts, y is assigned a random value. As it continues the loop, it is decremented and then tested for being>=0
. The only time this loop will exit is ify
is assigned the random number of0
. So that is why all yourElevation
are zero.I am a little confused as to why you think you need an inner loop. Try: