匹配游戏揭示瓷砖和无意中获胜

时间:2015-03-10 18:23:04

标签: c# random picturebox

我很抱歉不得不再次请求你的帮助,但这是我课堂作业的最后一块。

基本上在这个测验的这一部分中,我设计了它,以便玩家将两个图片框匹配在一起,每当玩家点击一个框时它会显示第一个图像,每当玩家点击第二个框时,如果标签匹配,两个框将保持可见,然后播放器将继续,直到所有对都匹配。

我的问题是,在输入表格后,所有的图片框都是可见的,这根本不可能。而第二个,也许是最令人沮丧的是,每当我点击任何其他图片框上的任何图片框时,游戏结束,我得到一个未处理的NullReferenceException,引用TimerClick_Tick事件。

如果有人可以帮助我,或者至少引导我正确指导我修正错误,我会非常感激。

感谢您的时间。

private void pbox_Click(object sender, EventArgs e)
    {
        if (TimerClick.Enabled == true)
            return;

        PictureBox clickedPBox = sender as PictureBox;

        if (clickedPBox != null)
        {
            if (clickedPBox.ForeColor == Color.Transparent)
                return;

            if (FirstClickedBox == null)
            {
                FirstClickedBox = clickedPBox;
                FirstClickedBox.ForeColor = Color.Transparent;
                return;
            }

            SecondClickedBox = clickedPBox;
            SecondClickedBox.ForeColor = Color.Transparent;

            CheckingForWinner();

            if (FirstClickedBox.Tag == SecondClickedBox.Tag)
            {
                FirstClickedBox = null;
                SecondClickedBox = null;
                return;
            }

            TimerClick.Start();
        }
    }

    private void TimerClick_Tick(object sender, EventArgs e)
    {
        FirstClickedBox.ForeColor = Color.Transparent;
        SecondClickedBox.ForeColor = Color.Transparent;

        FirstClickedBox = null;
        SecondClickedBox = null;
    }

    private void CheckingForWinner()
    {

        foreach (Control control in tableLayoutPanel1.Controls)
        {
            PictureBox iconBox = control as PictureBox;

            if (iconBox != null)
            {
                if (iconBox.ForeColor == iconBox.BackColor)
                    return;

            }



        }

        HasGameBeenWon = true;
        MessageBox.Show("You have successfully matched all of the above icons!", "We congratulate you!");
        CurrentPlayer.CurrentPlayerScore += 10;
        CurrentPlayer.CheckHardMode = true;
        CurrentPlayer.HardModeHasBeenChecked = false;
        Close();

    }

    int counter = 60;
    private void frmQuizThreeHardMode_Load(object sender, EventArgs e)
    {
        int counter = 60;
        TimerEnd = new System.Windows.Forms.Timer();
        TimerEnd.Tick += new EventHandler(TimerEnd_Tick);
        TimerEnd.Interval = 1000;
        TimerEnd.Start();
        textBox1.Text = counter.ToString() + " seconds";
    }

    private void TimerEnd_Tick(object sender, EventArgs e)
    {
        counter--;
        if ((counter <= 60) && (counter != 0))
        {
            textBox1.Text = counter.ToString() + " seconds";

            if ((counter < 1) && (HasGameBeenWon == false))
            {
                TimerEnd.Stop();
                textBox1.Text = "Time's up!";
                MessageBox.Show("You didn't finish in time. 10 points will be deducted from your total.", "You're Too Slow!", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                this.Close();
            }
        }


    }
}

}

1 个答案:

答案 0 :(得分:2)

引用TimerClick_Tick事件的 NullReferenceException

可能意味着 - 例外,来自此代码:

private void TimerClick_Tick(object sender, EventArgs e)
{
    FirstClickedBox.ForeColor = Color.Transparent;
    SecondClickedBox.ForeColor = Color.Transparent;

    FirstClickedBox = null;
    SecondClickedBox = null;
}

这似乎与点击某些内容有关,但这可能是巧合......

您的代码将FirstClickedBox和SecondClickedBox设置为NULL,也许是第一次触发事件,此代码运行正常,但因为这些引用已被删除;当代码第二次运行时 FirstClickedBox.ForeColor = Color.Transparent 无法运行,因为 FirstClickedBox 已设置为null?

您可以通过在Tick事件中放置断点或在未使用具有交互式调试的IDE时输出调试消息来验证这一点。