首先是C#方法问题

时间:2016-12-14 22:45:31

标签: c# winforms

我每次运行游戏时都会遇到这个奇怪的问题"比赛日#34;在Visual Studio中。所以我基本上都在使用Forms。我有4张PictureBoxes,还有狗照。狗信息存储在一个数组中。每场比赛结束后,我想把这些图片放回起跑线上。所以我做了一个方法:

public void SetToStartingPosition()
{
    for (int j=0; j < GreyhoundArray.Length; j++)
    {
        GreyhoundArray[j].MyPictureBox.Left = 0;
    }
}

每次比赛结束时我都会运行此方法,以及发生的情况: 所有4个PictureBox都按预期返回到起始位置(x = 0),然后弹出MessageBox,当我按下&#34; OK&#34;关闭它,PictureBoxes(Dogs)中1到3的随机数回到正确的位置(x = 600)...我只是不知道为什么关闭这个MessageBox有如此奇怪的影响,我不知道看到这两者之间的任何联系,也没有其他方法与PictureBox.Left有任何关系。对于所发生的事情的任何想法都将非常感激。

public void timer1_Tick(object sender, EventArgs e)
{

    for (int i = 0; i <GreyhoundArray.Length; i++)
    {
        GreyhoundArray[i].Run(); //method making PictureBoxes move to the right

        if (GreyhoundArray[i].Run() == true) //GreyhoundArray[i].Run() is true when the race is finished
        {
            timer1.Stop();
            **SetToStartingPosition();**
            winDog = i;
            MessageBox.Show("Dog number " + (winDog + 1) + " has won!");
            groupBox1.Enabled = true;
            button1.Text = "RESTART!";

            for (int k =0; k < GuysArray.Length; k++)
            {
                if (GuysArray[k].MyBet != null)
                {
                    GuysArray[k].Collect(winDog);
                    GuysArray[k].ClearBet();
                    GuysArray[k].UpdateLabels();
                }
            }
             break;
        }

    }

}

编辑: 哦哇,你刚刚给了一个线索什么是错的,所以实际上在Run()方法中有PictureBox位置改变线,我肯定会在其中一只狗赢得比赛时停止,但我没有打破循环,所以它还在继续,让狗回到他们的位置。我所要做的只是添加“休息”;&#39;在循环结束时。仅为您和其他人提供信息:

  ` public bool Run() 
    {
        if (MyPictureBox.Left > 600 - MyPictureBox.Width)
        {
            return true;

        }
        else
        {
            Location += MyRandom.Next(1, 4); 
            MyPictureBox.Left = StartingPosition + Location;
            return false;
        } `  

感谢您的帮助,这对我的Stack Overflow&#34; carrer&#34;而言是一个糟糕且令人尴尬的开始。 :(

0 个答案:

没有答案