I want to create a two player game on iPad, but I don’t really know how should I do it right. My concept will be just like “fruit ninja” of two player game (one vs other).
I mean that one player will play in viewcontrollerA and the other one will play in viewcontrollerB (that should be upside down from the viewcontrollerA).
How should I do it? Do I need to create two view controllers and display then via controller containment, or there is another and better way to do that?
thanks!
In iOS5 there is a blessed way for controllers to receive orientation events, add/remove callbacks, and they are properly inserted into the responder chain. So if you are going to do it, this is the best way. As long as you don’t do silly things like adding the same view to two UIViewControllers you are fine.
Why do it? I see logical to put the game engine on the master controller, write a client, encapsulate that client on a child view controller, and reuse it for the two players.
You could also use nested controllers to encapsulate complexity if you feel you need such thing. Example: one for the interface talking to the game engine, and another for the screen getting events from the engine.
UIViewController containment is easy. You need to read 6 methods and understand what calls what. You are welcome to read the documentation, but in a nutshell:
addChildViewController:Adds a child view controller. You also need to manually add the view of the child:[self.view addSubView:chieldVC.view];.removeFromParentViewControllerRemoves a child view controller.transitionFromViewController...Replace a view or move a view to the front.willMoveToParentViewController:Automatically called byaddChildViewController:. Call it only if you are going to remove (pass nil to indicate the controller becomes orphan). Override it only if you need custom code to happen.didMoveToParentViewController:You must call this afteraddChildViewControllerwhen the animation completes (or if there is no animation call it right away).automaticallyForward.... Override and return NO only if you manually want to forward rotation and appearance calls.For example, to nest one controller in another, add the child’s view as a regular view, and call addChildViewController: and didMoveToParentViewController: in the order shown below: