button, start a new game

Discussion in 'Mac Programming' started by cobolguy, Sep 30, 2010.

  1. macrumors newbie

    Joined:
    Sep 30, 2010
    Location:
    Penna
    #1
    Newbie, just learning xcode, cocoa (25 years Cobol programming).
    Wrote a small game, cannot figure out how, when game is won, to click a button and start a new game?

    Also, I have a 'Quit' button that goes to code that does "exit(0)" - Is this the easiest way to exit (besides red button at top)?

    Thanks, I know the answer must be something very basic.
     
  2. macrumors 603

    Joined:
    Aug 9, 2009
  3. thread starter macrumors newbie

    Joined:
    Sep 30, 2010
    Location:
    Penna
    #3
    code for 'quit' button

    Code:
    #import "stopgame.h"
    
    
    @implementation stopgame
    -(IBAction)stopgame:(id)sender
    {
    	 exit(0);
    	
    }
    @end
     
  4. macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #4
    [NSApp terminate:nil]; is a slightly less forceful way to say quit. As for a new game... that's not really a Cocoa thing. You just need to set all your game's data structures back to their initial state (perhaps by instantiating new ones).
     
  5. macrumors 68000

    Joined:
    Mar 29, 2009
    Location:
    USA
    #5
    What's your game control loop look like?

    It should be something like

    main()
    {
    loadgraphics()
    init_game()
    startgame()
    while (game_over ==0)
    {
    drawgraphics()
    movecharacters()
    getinput()
    etc()
    }
    // game is over now

    // get input and either quit, or start back over at init_game
    }
     
  6. thread starter macrumors newbie

    Joined:
    Sep 30, 2010
    Location:
    Penna
    #6
    'Play Again' code

    Thanks to everyone who replied to this question;

    I found the following website:
    http://13bold.com/tutorials/relaunching-your-application/

    and used the code in it and my 'Play Again' button works.

    Also, I changed the 'Quit' key as recommended.
    __________________________________________

    The 'back and forth' concept, still working on it; will post the code - (question: how does one get the code in the correct format for this site, the rectangle thing?

    Thanks for helping
     

Share This Page