无法在 XNA 中的另一个方法中使用变量

发布于 2024-11-27 19:06:59 字数 2689 浏览 0 评论 0原文

后面我会深入解释。所以这是我的代码,我们有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神经大条 2024-12-04 19:06:59

问题出在您的 elevation() 方法上。您使用 y 作为内部循环索引器,并在循环内为其分配一个值。所以循环开始,y被分配一个随机值。当它继续循环时,它会递减,然后测试是否为 >=0。该循环唯一退出的情况是为 y 分配了随机数 0。这就是为什么你所有的Elevation都是零。

我有点困惑为什么你认为你需要一个内循环。尝试:

public void elevation()
{
    for (x = (int)Width - 1; x >= 0; x--)
    {            
        Elevation[x] = rand.Next((int)MaxElevation);            
    }
}

The problem is with your elevation() method. You are using y 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 if y is assigned the random number of 0. So that is why all your Elevation are zero.

I am a little confused as to why you think you need an inner loop. Try:

public void elevation()
{
    for (x = (int)Width - 1; x >= 0; x--)
    {            
        Elevation[x] = rand.Next((int)MaxElevation);            
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文