System.ArgumentNullException 经过几个游戏回合后

本文关键字:游戏 几个 ArgumentNullException 经过 System | 更新日期: 2023-09-27 18:30:25

我一直有这种错误的错误"在PresentationFramework中发生了'System.ArgumentNullException'类型的第一次机会异常.dll"

好的,这是我的代码:

    DispatcherTimer timer;
    int count = 1;
    bool ball1visibility = true;
    bool ball2visibility = true;
    bool ball3visibility = true;
    bool ball4visibility = true;
    bool ball5visibility = true;
    bool gameover = false;
    int score1 = 0;
    int score2 = 0;
    int overallscore1 = 0;
    int overallscore2 = 0;
public void militimer_Tick(object sender, EventArgs e)
    {
        TimeSpan result = TimeSpan.FromMilliseconds(count++);
        if (Game != null)
        {
            DetectScoring();
        }
    }
 public void ShowGame(Game game)
    {
        if (Game == null)
        {
            Game = game;
            arenaContainer.Content = game;
            Game.SetGameLoop(gameLoop);
        }
    }
    public void Window_Loaded(object sender, RoutedEventArgs e)
    {            
        Game game = new PlayGame();    
        ShowGame(game);
        gameLoop.Start();
    }
    void DetectScoring()
    {
        if (Game != null && arenaContainer.Content != null && gameover == false)
        {
            PlayGame sg = (PlayGame)Game; //class that calls the objects of the game
            var topball1 = Canvas.GetTop(sg.ellipseBall1);
            var leftball1 = Canvas.GetLeft(sg.ellipseBall1);
            Rect ball1rect = new Rect(leftball1, topball1, 18, 18); //basically the balls that should be inside the pocket in order to get points
            var topball2 = Canvas.GetTop(sg.ellipseBall2);
            var leftball2 = Canvas.GetLeft(sg.ellipseBall2);
            Rect ball2rect = new Rect(leftball2, topball2, 18, 18); 
            var topball3 = Canvas.GetTop(sg.ellipseBall3);
            var leftball3 = Canvas.GetLeft(sg.ellipseBall3);
            Rect ball3rect = new Rect(leftball3, topball3, 18, 18); 
            var topball4 = Canvas.GetTop(sg.ellipseBall4);
            var leftball4 = Canvas.GetLeft(sg.ellipseBall4);
            Rect ball4rect = new Rect(leftball4, topball4, 18, 18);
            var topball5 = Canvas.GetTop(sg.ellipseBall5);
            var leftball5 = Canvas.GetLeft(sg.ellipseBall5);
            Rect ball5rect = new Rect(leftball5, topball5, 18, 18);

            // player 1 goal/ Player 2 gains points here
            topLeft.RadiusX = 25 / 2;
            topLeft.RadiusY = 25 / 2;
            Rect h1rect = topLeft.Bounds; // pocket hole 1
            botLeft.RadiusX = 25 / 2;
            botLeft.RadiusY = 25 / 2;
            Rect h2rect = botLeft.Bounds; // pocket hole 2

            //vice versa
            topRight.RadiusX = 25 / 2;
            topRight.RadiusY = 25 / 2;
            Rect h3rect = topRight.Bounds; // pocket hole 3
            botRight.RadiusX = 25 / 2;
            botRight.RadiusY = 25 / 2;
            Rect h4rect = botRight.Bounds; // pocket hole 4

            //check intersection of pocket with the balls
            if (ball1rect.IntersectsWith(h1rect) || ball1rect.IntersectsWith(h2rect))
            {
                if (ball1visibility == true)
                {
                    //       Player1Score.Content = "Player 2 score:" + score2;
                    Game.RemoveVisual(sg.ellipseBall1);
                    ball1visibility = false;
                    Console.WriteLine("Player 2 scores ball1");
                    score2 += 1;
                }
            }
            else if (ball1rect.IntersectsWith(h3rect) || ball1rect.IntersectsWith(h4rect))
            {
                if (ball1visibility == true)
                {
                    //      Player2Score.Content = "Player 1 scores";
                    Game.RemoveVisual(sg.ellipseBall1);
                    ball1visibility = false;
                    score1++;
                }
            }
            if (ball2rect.IntersectsWith(h1rect) || ball2rect.IntersectsWith(h2rect))
            {
                if (ball2visibility == true)
                {
                    //      Player1Score.Content = "Player 2 scores";
                    Game.RemoveVisual(sg.ellipseBall2);
                    ball2visibility = false;
                    score2++;
                }
            }
            else if (ball2rect.IntersectsWith(h3rect) || ball2rect.IntersectsWith(h4rect))
            {
                if (ball2visibility == true)
                {
                    //   Player2Score.Content = "Player 1 scores";
                    Game.RemoveVisual(sg.ellipseBall2);
                    ball2visibility = false;
                    score1++;
                }
            }
            if (ball3rect.IntersectsWith(h1rect) || ball3rect.IntersectsWith(h2rect))
            {
                if (ball3visibility == true)
                {
                    //    Player1Score.Content = "Player 2 scores";
                    Game.RemoveVisual(sg.ellipseBall3);
                    ball3visibility = false;
                    score2++;
                }
            }
            else if (ball3rect.IntersectsWith(h3rect) || ball3rect.IntersectsWith(h4rect))
            {
                if (ball3visibility == true)
                {
                    //  Player2Score.Content = "Player 1 scores";
                    Game.RemoveVisual(sg.ellipseBall3);
                    ball3visibility = false;
                    score1++;
                }
            }
            if (ball4rect.IntersectsWith(h1rect) || ball4rect.IntersectsWith(h2rect))
            {
                if (ball4visibility == true)
                {
                    //  Player1Score.Content = "Player 2 scores";
                    Game.RemoveVisual(sg.ellipseBall4);
                    ball4visibility = false;
                    score2++;
                }
            }
            else if (ball4rect.IntersectsWith(h3rect) || ball4rect.IntersectsWith(h4rect))
            {
                if (ball4visibility == true)
                {
                    //  Player2Score.Content = "Player 1 scores";
                    Game.RemoveVisual(sg.ellipseBall4);
                    ball4visibility = false;
                    score1++;
                }
            }
            if (ball5rect.IntersectsWith(h1rect) || ball5rect.IntersectsWith(h2rect))
            {
                if (ball5visibility == true)
                {
                    //    Player1Score.Content = "Player 2 scores";
                    Game.RemoveVisual(sg.ellipseBall5);
                    ball5visibility = false;
                    score2++;
                }
            }
            else if (ball5rect.IntersectsWith(h3rect) || ball5rect.IntersectsWith(h4rect))
            {
                if (ball5visibility == true)
                {
                    //       Player2Score.Content = "Player 1 scores";
                    Game.RemoveVisual(sg.ellipseBall5);
                    ball5visibility = false;
                    score1++;
                }
            }                      
        }
        // after the 5 balls are inside the pocket it will count total score
        if (score1 + score2 == 5 && gameover == false)
        {
            Console.WriteLine("Calculating Score");
            Scoring(score1, score2);
        }
        lbScore1a.Content = score1.ToString();
        lbScore2a.Content = score2.ToString();
        lbScore1.Content = overallscore1.ToString() + " - ";
        lbScore2.Content = overallscore2.ToString() + " - ";
    }

  // *************** part where I suspect the problem is ******************
    public void Scoring(int player1score, int player2score)
    {
        player1score = score1;
        player2score = score2;
         Console.WriteLine("Score is " + (score1 + score2));
                if (score1 > score2 && gameover == false)
                {
                    gameover = true;
                    timer.Stop();
                    militimer.Stop();
                    gameLoop.Stop();
                    MessageBoxResult result = MessageBox.Show("Player 1 wins", "Win", MessageBoxButton.OK);
                    if (result == MessageBoxResult.OK)
                    {
                        count = 0;
                        timer.Interval = TimeSpan.FromMilliseconds(Convert.ToDouble(1));
                        timer.Start();
                        militimer.Interval = TimeSpan.FromMilliseconds(Convert.ToDouble(1));
                        militimer.Tick += new EventHandler(militimer_Tick);
                        militimer.Start();
                        gameover = false;                                                      
                        Game = null;
                        arenaContainer.Content = null;
                        Game game = new PlayGame();
                        ShowGame(game);
                        gameLoop.Start();
                        score1 = 0;
                        score2 = 0;
                        ball1visibility = true;
                        ball2visibility = true;
                        ball3visibility = true;
                        ball4visibility = true;
                        ball5visibility = true;
                        Console.WriteLine("Game restarting 1");
                    }
                    overallscore1++;
                }
                else if (score1 < score2 && gameover == false)
                {
                    gameover = true;
                    timer.Stop();
                    militimer.Stop();
                    gameLoop.Stop();
                    MessageBoxResult result = MessageBox.Show("Player 2 wins", "Win", MessageBoxButton.OK);
                    if (result == MessageBoxResult.OK)
                    {
                        count = 0;
                        timer.Interval = TimeSpan.FromMilliseconds(Convert.ToDouble(1));
                        timer.Start();
                        militimer.Interval = TimeSpan.FromMilliseconds(Convert.ToDouble(1));
                        militimer.Tick += new EventHandler(militimer_Tick);
                        militimer.Start();
                        gameover = false;
                        Game = null;
                        arenaContainer.Content = null;
                        Game game = new PlayGame();
                        ShowGame(game);
                        gameLoop.Start();
                        score1 = 0;
                        score2 = 0;
                        ball1visibility = true;
                        ball2visibility = true;
                        ball3visibility = true;
                        ball4visibility = true;
                        ball5visibility = true;
                        Console.WriteLine("Game restarting 2");
                    }
                    overallscore2++;
                }
    }

当前写行调试的输出如下:

Calculating Score
Score is 5
Game restarting 2
Calculating Score
Score is 5
A first chance exception of type 'System.ArgumentNullException' occurred in PresentationFramework.dll
Game restarting 1

游戏应该做的是,如果所有 5 个球都在里面。它将立即开始另一轮,并添加上一轮获胜的玩家的总分。我不太确定这个错误。有时我可以直到 7 轮,最少是 2 轮。如果你认为我给你的代码仍然不够,就像你想看到其他类一样。让我知道,以便我可以发布它。我还在我认为导致问题的方法上加上了"****"。我知道这篇文章很长,但提前感谢您的帮助。

我认为问题是:

 ** // *************** part where I suspect the problem is ******************
public void Scoring(int player1score, int player2score)
{
    player1score = score1;
    player2score = score2;
     Console.WriteLine("Score is " + (score1 + score2));
            if (score1 > score2 && gameover == false)
            {
                gameover = true;
                timer.Stop();
                militimer.Stop();
                gameLoop.Stop();
                MessageBoxResult result = MessageBox.Show("Player 1 wins", "Win", MessageBoxButton.OK);
                if (result == MessageBoxResult.OK)
                {
                    count = 0;
                    timer.Interval = TimeSpan.FromMilliseconds(Convert.ToDouble(1));
                    timer.Start();
                    militimer.Interval = TimeSpan.FromMilliseconds(Convert.ToDouble(1));
                    militimer.Tick += new EventHandler(militimer_Tick);
                    militimer.Start();
                    gameover = false;                                                      
                    Game = null;
                    arenaContainer.Content = null;
                    Game game = new PlayGame();
                    ShowGame(game);
                    gameLoop.Start();
                    score1 = 0;
                    score2 = 0;
                    ball1visibility = true;
                    ball2visibility = true;
                    ball3visibility = true;
                    ball4visibility = true;
                    ball5visibility = true;
                    Console.WriteLine("Game restarting 1");
                }
                overallscore1++;
            }
            else if (score1 < score2 && gameover == false)
            {
                gameover = true;
                timer.Stop();
                militimer.Stop();
                gameLoop.Stop();
                MessageBoxResult result = MessageBox.Show("Player 2 wins", "Win", MessageBoxButton.OK);
                if (result == MessageBoxResult.OK)
                {
                    count = 0;
                    timer.Interval = TimeSpan.FromMilliseconds(Convert.ToDouble(1));
                    timer.Start();
                    militimer.Interval = TimeSpan.FromMilliseconds(Convert.ToDouble(1));
                    militimer.Tick += new EventHandler(militimer_Tick);
                    militimer.Start();
                    gameover = false;
                    Game = null;
                    arenaContainer.Content = null;
                    Game game = new PlayGame();
                    ShowGame(game);
                    gameLoop.Start();
                    score1 = 0;
                    score2 = 0;
                    ball1visibility = true;
                    ball2visibility = true;
                    ball3visibility = true;
                    ball4visibility = true;
                    ball5visibility = true;
                    Console.WriteLine("Game restarting 2");
                }
                overallscore2++;
            }
}**

System.ArgumentNullException 经过几个游戏回合后

我看到的是每次开始新一轮时,您都会向毫秒计时器添加一个事件。这样,在几个回合之后,你会同时发射多个事件,而不是一个。

如果发生滴答并且事件处理程序跳入您那里DetectScore,这也是一个问题

if (Game != null && arenaContainer.Content != null && gameover == false)

如果在

gameover = false;                                                      
Game = null;

您应该使用

militimer.Tick -= new EventHandler(militimer_Tick);

重置所有游戏状态,如游戏结束和其他内容并开始之后的时间

militimer.Tick += new EventHandler(militimer_Tick);
militimer.Start();

这应该可以解决问题。