Note that there are some explanatory texts on larger screens.

plurals
  1. POWhile loop not working as intended in Pong game
    text
    copied!<p>I am attempting to create a Pong game in C# with Visual C# Express 2010. For the most part, I have the main idea of the game finished, however I have an issue with the ball moving. I have create a for loop, like this:</p> <pre><code>public void ballSet() { if (!Values.isPaused) { while(true) { if (Values.totalTime.Elapsed.Seconds &gt; 1) { Values.totalTime.Restart(); ballMove(50, 50); } } } } public void ballMove(int factorX, int factorY) { Values.ballLastX = ball.Location.X; Values.ballLastY = ball.Location.Y; this.ball.Location = new Point(this.ball.Location.X + factorX, this.ball.Location.Y + factorY); } </code></pre> <p>The "ballMove(50, 50);" is just for testing purposes at the moment. The issue is that when ballSet() is called, the form seems to close with a code of 0, meaning that there was no error. I call ballSet() over here.</p> <pre><code>public Pong() { InitializeComponent(); ballSet(); Values.totalTime.Start(); } </code></pre> <p>I have already checked and the program does somewhat work when I remove the while loop in ballSet(), as well as the if statement checking the stopwatch (Values.totalTime is a Stopwatch). Obviously since the while loop is commented out, ballMove() is only called once, and the the ball moves once and stops. </p> <p>Does anyone know how to fix this? I want the ball to be moving constantly, while still having it possible to perform other tasks such as moving the bat in Pong.</p> <p>This is the output I can give you while running Pong.</p> <p><a href="http://pastebin.com/nj1pdg3U" rel="nofollow">http://pastebin.com/nj1pdg3U</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload