jeudi 13 novembre 2014

System.ArgumentNullException after few game rounds


Vote count:

0




I been having errors of having this kind of error "A first chance exception of type 'System.ArgumentNullException' occurred in PresentationFramework.dll"


Ok this is my code:



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++;
}

}


The output with the current writeline debug is this: 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


What the game should do is that if all the 5 balls are inside. It will immediately start another round and also add the overall score of whoever player wins the previous round. I am not really sure with the error. Sometimes I can go until 7 rounds sometimes, minimum was 2 rounds. If you think the code I gave you is still not enough like you want to see the other classes. Let me know so I can post it. I also put a "****" on which method I think is causing the problem. I know this post is long but Thank you in advance for the help.



asked 20 secs ago







System.ArgumentNullException after few game rounds

Aucun commentaire:

Enregistrer un commentaire