memorizing button tap code iOS game

| | August 4, 2015

I’m using a single view app in Xcode 6.1 for my game with Storyboards. Not unity or sprite kit. It’s a small game not even 3MB. Anyways, I created un-lockable characters with a BOOLEAN.

 if(scoreNumber  > 9  || HighScoreNumber >9){


  bird.image=[UIImage imageNamed:@"BLUE.png"];

 if(BlueChar ==YES){       //BlueChar=BOOL

When you click on BlueChar button, the UIImageView is connected to BlueChar character. So when you tap START GAME button game starts and you play as BlueChar. Then when it’s GameOver and you click START GAME button again, the BlueChar button that was clicked isn’t saved and instead you play as Original Character.

How do I make it that if you click BlueChar button ONCE, now you can play as BlueChar forever unless you click on another button that represents another character? How do I make the game save the last button clicked unless user clicks on another button in Objective-C code?

One Response to “memorizing button tap code iOS game”

  1. Don’t rely only on the storyboard to do all the work in your game. You need some properties for which character is currently selected and other game states. Then in viewDidLoad and/or in the method for the Start Game button, set the bird.image to be blue if that’s the status.

    To save off this info between runs of the game utilize the messages for foreground / background state changes:

            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movingToBackground:) name:UIApplicationWillResignActiveNotification object:nil];
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movingToForeground:) name:UIApplicationDidBecomeActiveNotification object:nil];
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateDefaults:) name:UIApplicationWillTerminateNotification object:nil];

    Then create those methods to handle the app moving to the background and save off the game state.

    Like this:

    -(void) updateDefaults: (NSNotification *) notification {
        [defaults setObject:[NSNumber numberWithBool: BlueChar] forKey:@"BlueChar"];
        [[NSUserDefaults standardUserDefaults] synchronize];

    But honestly. If you have more than one character to play you’re not going to want it to be BlueChar as a bool because really you’ll want something like int currentChar where currentChar=BLUE_CHAR and #define BLUE_CHAR 1 so that you can also set currentChar to be YELLOW_CHAR or ORANGE_CHAR. Maybe the BlurChar bool is used to check if it’s unlocked, and the currentChar int is used to store which one you’re playing with. Depends. So instead of the above you’d do:

    [defaults setObject:[NSNumber numberWithInt: currentChar] forKey:@"currentChar"];

    That’s one way to do it anyway.

    Then on the IBAction you’re going to set currentChar to be BLUE_CHAR (if it’s unlocked). Then call a setCharImage method which could set it based on any number of options:

        switch (currentChar) {
            case BLUE_CHAR:
                bird.image=[UIImage imageNamed:@"BLUE.png"];
            case YELLOW_CHAR:
                bird.image=[UIImage imageNamed:@"YELLOW.png"];

    In my games I have a .h file where I stuff all the #defines like that. IMHO it’s a toss up if you use those or enums. I personally prefer #defines. But I’m an old 80’s C coder and I prefer those ways.

Leave a Reply