Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

mymac1

macrumors newbie
Original poster
Jul 28, 2009
20
0
Hi,
Regarding the use of while loops on the iphone.

Traditionally my gameloop might look like:

while (not end of game){
do stuff
update screen
check game status, etc..
}

My question is really, does the iphone's operating system have a problem with that sort of operation, specifically would my program receive the touch inputs and could anything else be affected.

Another issue which I am not certain is how an intro/start prompt would fit into a typical gameloop, as this would require its very on while loop, again possibly affecting the iphone's operation.

Would appreciate any ideas/theories.

Thanks in advance.
m1
 
You should not be doing something like that on the main thread: you will freeze all input handling (and potentially drawing). Either push this into a separate thread or look at using the built-in NSRunLoop (you could look at scheduling a NSTimer to fire every 1/60th of a second or so to do the various game processing items).
 
event driven programming

You need to look up event driven programming. It's how Cocoa Touch works.

You don't sit in a loop and call the OS or UI libraries. You ask the OS to call your routines (on timers, for input events, etc.), and exit your setup routine (return). Then the OS calls you when it's good and ready.

Instead of drawing in the while loop, you ask the OS to call your drawRect.

Instead of waiting for a touch in a while loop, you ask the OS to call you when any input events occur.

If you need to do stuff periodically (like asking the OS to call your drawRect), then you set up a timer to call you periodically.

If you have an intro screen, then you ask the OS to call your main game routine after the user does something (hit a Done button, etc.), then exit the intro routine and wait for this event.

It helps to have all the routines needed that respond when the OS wants to call you. You also need to know how to save state somewhere (in Model objects would be the proper MVC, Cocoa and reusable coding paradigm, but globals will also work for a simple enough game.)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.