Question

I'm making a program in C#, and I am trying to simply have a turtle move across the screen. I tried making a while loop:

while (stop <= 60)
{
   turtleRect.X -= 1;
   stop++;
}

What is supposed to happen is the turtle moves slowly across the screen. It works, except for one thing. When I run the program, the while loop finishes before the program opens. It waits until the while loop is done to open. When it opens, the turtle is already 60 pixels to the left. Is there a setting to make the program open, then run? Any help is greatly appreciated.

Was it helpful?

Solution

This is happening because this is a statement and it "stops" the program execution until it finishes its loop.

You can solve this using a timer instead.

If this is XNA, you have the gameloop (Update) to do such things. Just move the code from within your while to the update function.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top