C# 新手,图片框数组中的图片框对象为空?

发布于 2024-12-10 09:05:28 字数 1334 浏览 0 评论 0原文

我刚刚开始使用 C# 并创建我的第一个游戏,但遇到了问题。 我有一个图片框数组

PictureBox[] obstacles = new PictureBox[20];

,我试图在其中声明 20 个新的随机位置图片框。

    private void Game_Load(object sender, EventArgs e)
    {
        player.Size = new Size(20, 20);
        player.Location = new Point(20, 240);
        player.Image = Properties.Resources.playerImg;
        Controls.Add(player);

        for (int i = 0; i < 20; i++)
        {
            obstacles[i] = new PictureBox();
            obstacles[i].Size = new Size(10, 10);
            obstacles[i].Location = new Point(50, 340);
            obstacles[i].BackColor = Color.Red;
            Controls.Add(obstacles[i]);
        }
    }

我知道它还没有将它们生成到随机位置,但在我让它们生成之前我不会这样做。 现在,当我尝试创建一个使用 picturebox 数组的方法时,它的对象 (picturebox[0-19]) 都为空?

private void JustAnOtherTest(PictureBox obj)
{
    for (int y = obj.Top; y < obj.Top + obj.Height - 1; y++)
    {
        MessageBox.Show("Testing out!", "Test");
    }
}

现在调用:JustAnOtherTest(obstacles[1]);在 JustAnOtherTest 方法中添加 obj 的监视只会给我一个 null,为什么呢?

编辑:似乎它永远不会进入for循环,因为它一旦到达player.Image = Properties.Resources.playerImg;就会退出Game_Load;这可能意味着问题出在资源文件中... :S

EDIT2: 是的,我是对的,问题出在资源文件中。我弄乱了一些命名空间,它找不到资源文件,因为它位于不同的命名空间中,但我不太确定为什么它没有给出错误。然而,修复所有名称空间后,工作得很好。 :)

I'm just starting C#, and creating my first game, but just ran into a problem.
I have a picturebox array

PictureBox[] obstacles = new PictureBox[20];

And I'm trying to declare 20 new random location pictureboxes inside it.

    private void Game_Load(object sender, EventArgs e)
    {
        player.Size = new Size(20, 20);
        player.Location = new Point(20, 240);
        player.Image = Properties.Resources.playerImg;
        Controls.Add(player);

        for (int i = 0; i < 20; i++)
        {
            obstacles[i] = new PictureBox();
            obstacles[i].Size = new Size(10, 10);
            obstacles[i].Location = new Point(50, 340);
            obstacles[i].BackColor = Color.Red;
            Controls.Add(obstacles[i]);
        }
    }

I know it's not spawning them into a random location yet, but I won't do that 'till I get them to spawn at all.
Now when I try to create a method to use picturebox array, it's objects (picturebox[0-19]) are all just null?

private void JustAnOtherTest(PictureBox obj)
{
    for (int y = obj.Top; y < obj.Top + obj.Height - 1; y++)
    {
        MessageBox.Show("Testing out!", "Test");
    }
}

Now calling: JustAnOtherTest(obstacles[1]); and adding a watch for obj in JustAnOtherTest method will just give me a null, why is that?

EDIT: Seems like it never goes into the for loop, cause it exits Game_Load once it gets to player.Image = Properties.Resources.playerImg; which probably means that the problem is in the resource files... :S

EDIT2: Yeah, I was right, the problem was within resource files. I had messed up with some namespaces and it couldn't find the resource file cause it was in a different namespace, but I ain't so sure why it didn't give an error on that. How ever, after fixing all the namespaces, works just fine. :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

随梦而飞# 2024-12-17 09:05:29

存在三种可能性

1) 在访问数组之前没有调用 Game_Load()。在其中插入一个断点,看看它是否会触发。

2)你以某种方式创建了两个称为障碍的变量。具体是在哪里定义的呢?

3)在您访问它之前,有东西正在再次清除它。

最基本的检查是在创建循环中放置一个断点,并检查新的在该点是否工作正常。

There are three possibilities

1) Game_Load() is not being called before you access the array. Stick a break point in it and see if it fires.

2) You are somehow creating two variables called obstacles. Where is it defined exactly?

3) Something is clearing it again before you access it.

The most basic check is to put a breakpoint in your create loop, and check that the new is working ok at that point.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文