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

Ryan Burgess

macrumors 6502
Original poster
Jan 26, 2013
320
48
Hi, so I have a game that I am trying to add a main menu too. But I can't get the main menu to show up...attached is a screenshot of what my storyboard looks like.

However when I click run instead of showing the main menu, it just proceeds to the game.

Any help is appreciated...also I didn't know what parts of my code I would need to post so if you need to see code to help just ask and I'll post it.

TIA

This is the code for my ViewController.m

Code:
//
//  ViewController.m
//  Tap Me
//
//  Created by Ryan on 4/27/13.
//
//


#import "ViewController.h"



@interface ViewController ()

@end



@implementation ViewController
- (AVAudioPlayer *)setupAudioPlayerWithFile:(NSString *)file type:(NSString *)type
{
    // 1
    NSString *path = [[NSBundle mainBundle] pathForResource:file ofType:type];
    NSURL *url = [NSURL fileURLWithPath:path];
    
    // 2
    NSError *error;
    
    // 3
    AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    
    // 4
    if (!audioPlayer) {
        NSLog(@"%@",[error description]);
    }
    
    // 5
    return audioPlayer;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_tile.png" ]];
    scoreLabel.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"field_score.png"]];
    timerLabel.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"field_time.png"]];
    buttonBeep = [self setupAudioPlayerWithFile:@"ButtonTap" type:@"wav"];
    secondBeep = [self setupAudioPlayerWithFile:@"SecondBeep" type:@"wav"];
    backgroundMusic = [self setupAudioPlayerWithFile:@"HallOfTheMountainKing" type:@"mp3"];
    UIAlertView *alerted = [[UIAlertView alloc] initWithTitle:@"Get Ready" message:[NSString stringWithFormat:@"Press Start!"] delegate:self cancelButtonTitle:@"Start" otherButtonTitles:nil];
    [alerted show];
}

- (void)awakeFromNib {
    setHigh = [[NSUserDefaults standardUserDefaults] integerForKey:@"highScore"];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)buttonPressed {
    count++;
    scoreLabel.text = [NSString stringWithFormat:@"Score:\n%i", count];
    [buttonBeep play];
}
- (void)subtractTime {
    // 1
    seconds--;
    timerLabel.text = [NSString stringWithFormat:@"Time: %i",seconds];
    [secondBeep play];
    
    // 2
    if (seconds == 0) {
        if (count < highScore) {
            [timer invalidate];
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Time is up!" message:[NSString stringWithFormat:@"You scored %i points!", count] delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:nil];
            [alert show];
        }
        else if (count > highScore) {
            [timer invalidate];
            (setHigh = count);
            UIAlertView *display = [[UIAlertView alloc] initWithTitle:@"High Score!" message:[NSString stringWithFormat:@"You got a new high score of %i!", count] delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:nil];
            [display show];
            [[NSUserDefaults standardUserDefaults]setInteger:setHigh forKey:@"highScore"];
            
        }
    }
}
- (void)setupGame {
    // 1
    seconds = 30;
    count = 0;
    highScore = setHigh;
    
    
    
    
    // 2
    timerLabel.text = [NSString stringWithFormat:@"Time: %i", seconds];
    scoreLabel.text = [NSString stringWithFormat:@"Score:\n%i", count];
    highLabel.text = [NSString stringWithFormat:@"High Score:\n%i", highScore];
    
    // 3
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0f
                                             target:self
                                           selector:@selector(subtractTime)
                                           userInfo:nil
                                            repeats:YES];
    
    [backgroundMusic setVolume:0.3];
    [backgroundMusic play];
}


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    [self setupGame];
}

- (IBAction)restartPressed{
    [timer invalidate];
    [self viewDidLoad];
}

- (int)getBannerHeight:(UIDeviceOrientation)orientation {
    if (UIInterfaceOrientationIsLandscape(orientation)) {
        return 32;
    } else {
        return 50;
    }
}

- (int)getBannerHeight {
    return [self getBannerHeight:[UIDevice currentDevice].orientation];
}
@end
 

Attachments

  • Sceengrab copy 2.jpg
    Sceengrab copy 2.jpg
    453.7 KB · Views: 134
Last edited:
So the root view controller is of type "ViewController1" and the second one, that's pointed to by the blue root view controller is of type "ViewController"?

Those names are not very helpful I would suggest naming those classes based on what they do, so the name provides information.

If I'm right and your root view controller is a "ViewController1" object, then that's the code we need to see. Post the viewDidLoad, viewWillAppear, and viewDidAppear methods from ViewController1.

Also explain how you are supposed to transition from the blue root view controller to the green one with the giant button on it. I see a segue. What triggers that segue?



Hi, so I have a game that I am trying to add a main menu too. But I can't get the main menu to show up...attached is a screenshot of what my storyboard looks like.

However when I click run instead of showing the main menu, it just proceeds to the game.

Any help is appreciated...also I didn't know what parts of my code I would need to post so if you need to see code to help just ask and I'll post it.

TIA

This is the code for my ViewController.m

Code:
//
//  ViewController.m
//  Tap Me
//
//  Created by Ryan on 4/27/13.
//
//


#import "ViewController.h"



@interface ViewController ()

@end



@implementation ViewController
- (AVAudioPlayer *)setupAudioPlayerWithFile:(NSString *)file type:(NSString *)type
{
    // 1
    NSString *path = [[NSBundle mainBundle] pathForResource:file ofType:type];
    NSURL *url = [NSURL fileURLWithPath:path];
    
    // 2
    NSError *error;
    
    // 3
    AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    
    // 4
    if (!audioPlayer) {
        NSLog(@"%@",[error description]);
    }
    
    // 5
    return audioPlayer;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_tile.png" ]];
    scoreLabel.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"field_score.png"]];
    timerLabel.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"field_time.png"]];
    buttonBeep = [self setupAudioPlayerWithFile:@"ButtonTap" type:@"wav"];
    secondBeep = [self setupAudioPlayerWithFile:@"SecondBeep" type:@"wav"];
    backgroundMusic = [self setupAudioPlayerWithFile:@"HallOfTheMountainKing" type:@"mp3"];
    UIAlertView *alerted = [[UIAlertView alloc] initWithTitle:@"Get Ready" message:[NSString stringWithFormat:@"Press Start!"] delegate:self cancelButtonTitle:@"Start" otherButtonTitles:nil];
    [alerted show];
}

- (void)awakeFromNib {
    setHigh = [[NSUserDefaults standardUserDefaults] integerForKey:@"highScore"];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)buttonPressed {
    count++;
    scoreLabel.text = [NSString stringWithFormat:@"Score:\n%i", count];
    [buttonBeep play];
}
- (void)subtractTime {
    // 1
    seconds--;
    timerLabel.text = [NSString stringWithFormat:@"Time: %i",seconds];
    [secondBeep play];
    
    // 2
    if (seconds == 0) {
        if (count < highScore) {
            [timer invalidate];
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Time is up!" message:[NSString stringWithFormat:@"You scored %i points!", count] delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:nil];
            [alert show];
        }
        else if (count > highScore) {
            [timer invalidate];
            (setHigh = count);
            UIAlertView *display = [[UIAlertView alloc] initWithTitle:@"High Score!" message:[NSString stringWithFormat:@"You got a new high score of %i!", count] delegate:self cancelButtonTitle:@"Play Again" otherButtonTitles:nil];
            [display show];
            [[NSUserDefaults standardUserDefaults]setInteger:setHigh forKey:@"highScore"];
            
        }
    }
}
- (void)setupGame {
    // 1
    seconds = 30;
    count = 0;
    highScore = setHigh;
    
    
    
    
    // 2
    timerLabel.text = [NSString stringWithFormat:@"Time: %i", seconds];
    scoreLabel.text = [NSString stringWithFormat:@"Score:\n%i", count];
    highLabel.text = [NSString stringWithFormat:@"High Score:\n%i", highScore];
    
    // 3
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0f
                                             target:self
                                           selector:@selector(subtractTime)
                                           userInfo:nil
                                            repeats:YES];
    
    [backgroundMusic setVolume:0.3];
    [backgroundMusic play];
}


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    [self setupGame];
}

- (IBAction)restartPressed{
    [timer invalidate];
    [self viewDidLoad];
}

- (int)getBannerHeight:(UIDeviceOrientation)orientation {
    if (UIInterfaceOrientationIsLandscape(orientation)) {
        return 32;
    } else {
        return 50;
    }
}

- (int)getBannerHeight {
    return [self getBannerHeight:[UIDevice currentDevice].orientation];
}
@end
 
So the root view controller is of type "ViewController1" and the second one, that's pointed to by the blue root view controller is of type "ViewController"?

Those names are not very helpful I would suggest naming those classes based on what they do, so the name provides information.

If I'm right and your root view controller is a "ViewController1" object, then that's the code we need to see. Post the viewDidLoad, viewWillAppear, and viewDidAppear methods from ViewController1.

Also explain how you are supposed to transition from the blue root view controller to the green one with the giant button on it. I see a segue. What triggers that segue?

Very strange... I just re-opened the project in Xcode and all is well now. I've been trying to get this to work for weeks to no avail, and now all of a sudden it just works! Go figure. Thanks though!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.