I am trying to create a game on the iPhone, however I am running in to some (I hope) simple, yet confusing issues.
I am forcing my application to be in UIInterfaceOrientationLandscapeRight mode, this is set in the .plist and each of my UIViewControllers returns this as the only viable orientation for shouldAutorotateToInterfaceOrientation
That works... sometimes. Each of my xibs have been designed to be in landscape mode, and all ViewControllers are set up in a very similar fashion. Here is my problem:
I instantiate all of my UIViewControllers programatically such as:
splashScreen = [[SplashScreenController alloc] initWithNibName:@"SplashScreenView" bundle:[NSBundle mainBundle]];
splashScreen.view.frame = CGRectMake(0, 0, [[GameConsts defaultGameConsts] screenWidth], [[GameConsts defaultGameConsts] screenHeight]);
[[self view] addSubview: [splashScreen view]];
mainMenu = [[MainMenuController alloc] initWithNibName:@"MainMenuView" bundle:[NSBundle mainBundle]]; mainMenu.view.frame = CGRectMake(0, 0, [[GameConsts defaultGameConsts] screenWidth], [[GameConsts defaultGameConsts] screenHeight]); [[self view] addSubview: [mainMenu view]];
This works fine, but when I activate my "game views", i.e., the in-game menus and actual playing field I need to invert the screenWidth and screenHeight parameters to get them to be correctly displayed. I do not understand why this is, and I have not yet been able to figure it out. Here is how they are being instantiated:
- (void) setupGameViews
{
// Why do these need to be reversed to show up properly? (Height and Width!)
gameView = [[GameController alloc] initWithNibName:@"GameView" bundle:[NSBundle mainBundle]];
gameView.view.frame = CGRectMake(0, 0, [[GameConsts defaultGameConsts] screenHeight], [[GameConsts defaultGameConsts] screenWidth]);
[[self view] addSubview: [gameView view]];
gameMenu = [[GameMenuController alloc] initWithNibName:@"GameMenuView" bundle:[NSBundle mainBundle]];
gameMenu.view.frame = CGRectMake(0, 0, [[GameConsts defaultGameConsts] screenHeight], [[GameConsts defaultGameConsts] screenWidth]);
[[self view] addSubview: [gameMenu view]];
}
(Note: This method is being called when a start game button is clicked. This is all inside a "parent" UIViewController that has no associated view with it, but instead contains and sets up all of these subviews. Maybe there is an issue with their architecture? I have seen some similar examples around, so I decided to use it.)
For completeness, here are the definitions in the GameConsts singleton during its init method:
- (id) init
{
screenWidth = [[UIScreen mainScreen] applicationFrame].size.width;
screenHeight = [[UIScreen mainScreen] applicationFrame].size.height;
screenCenter = CGPointMake(screenSizeHeight / 2, screenSizeWidth / 2);
return [super init];
}
screenWidth and screenHeight are floats. screenCenter is a CGPoint. If you note, I have reversed the height/width in the screenCenter CGPoint because otherwise when I try to draw an object at the screen center it shows up in the bottom left corner of the screen.
This is driving me crazy and keeping me from the far more fun parts of this experience. If anyone can provide help, it would be wonderful. If any more information is needed, let me know.