The use of indefinite while loops in program.

Discussion in 'iOS Programming' started by mymac1, Jun 3, 2010.

  1. mymac1 macrumors newbie

    Joined:
    Jul 28, 2009
    #1
    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
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    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).
     
  3. firewood macrumors 604

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #3
    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.)
     

Share This Page