I’m making a game for iOS using XCode however I have run into one problem:
If I use a loop, the graphics do not get updated until the next frame after the loop has finished; so if for example I have something like this:
something.center = CGPointMake(20, something.center.y);
while(something.center.x < 50)
{
something.center = CGPointMake(something.center.x + 1, something.center.y);
// Just a delay:
[NSThread sleepForTimeInterval:0.01];
}
The UIImageView ‘something’ will just go straight from 20 to 50. I know in this occasion I should use an NSTimer but I just made this example to keep things simple.
So is there a function which I can call to update the screen, some kind of VBL?
Since it’s best practice to use the main thread for the UI (like H2CO3 mentioned), I wouldn’t recommend using
sleepForTimeInterval, as it clogs up the thread doing arguably the most important job (at least for the user). Instead, you could use a timer like this:Of course you’ll have to tweak the code and make some modifications, but that’s the gist of what you should do.