Box2D Rigid Bodies Distorted When Loading Another View
I have a Box2D world which works great. I'm using it in a simple iOS game where the user tilts the device to push bodies around the screen racking up points when a certain body collides with another.
It was working fine when it was buried a couple layers deep in my app in a test controller. I invoked it from a button on my menu screen and a button on the game screen would return to the menu just fine via popToViewController. But once I saw how great it worked I realized that I wanted the game to be the first screen a user sees (after the default loading image). So now the app loads the world and the game is on right off the bat without any problems.
Until the user taps the menu button to get to the menu screen - another view controller. The menu controller loads fine and everything works there as expected. BUT when one taps the back button to return to the game/world the images/bodies are distorted. The distortion varies depending on how they happened to be rotated when the menu controller got pushed onto the navigation stack. Sometimes they are enlarged and sometimes smooshed.
I did try pausing the world (by not calling Step) when loading the menu controller, but that did not help with the image distortion problem.
Here is the code where the menu controller gets loaded:
//unhide the navbar for the next screen
[self.navigationController setNavigationBarHidden:NO];
MenuViewController *mvc = [[MenuViewController alloc]init];
[self.navigationController pushViewController:mvc animated:YES];
[mvc release];
And here is where the game's view controller gets loaded by the app delegate in application didFinishLaunchingWithOptions:
B2DViewController *b2dvc = [[B2DViewController alloc]init];
UINavigationController *navcon = [[UINavigationController alloc] init];
[navcon setNavigationBarHidden:YES];
[navcon pushViewController:b2dvc animated:NO];
[window addSubview:navcon.view];
[b2dvc release];
[window makeKeyAndVisible];
The images come in via a xib. One is button and one is an imageView. Both are added to dynamic bodies which are configured thusly:
b2PolygonShape dynamicBox;
dynamicBox.SetAsBox(boxDimensions.x, boxDimensions.y);
...
b2FixtureDef fixtureDef;
fixtureDef.shape = &dynamicBox;
fixtureDef.density = 3.0f;
fixtureDef.friction = 0.3f;
fixtureDef.restitution = 0.5f;
...
I tried hiding all the subviews but, as you might suspect, that only hid them from view, not from the forces of distortion. I also turned off all the stretching and resizing stuff in IB, to no avail (I was just guessing).
I'd be grateful for any assistance or ideas t开发者_如何学Pythonhat anyone might have.
Instead of pushing the menu controller onto the navigation controller and then adding the navigation controller to the view I instead used presentModalViewController: animated. This didn't fix the problem, but once I commented out [self.navigationController setNavigationBarHidden:NO]; the problem was resolved.
//unhide the navbar for the next screen
//[self.navigationController setNavigationBarHidden:NO];
MenuViewController *mvc = [[MenuViewController alloc]init];
[self presentModalViewController:(UIViewController *)mvc animated:YES];
[mvc release];
It seems that the NavigationBar was smashing the images? I'd like to look into this further as the navigation bar can be a handy thing, but I don't have time now, which probably means I'll never get around to it.
精彩评论