iPhone Game Development - Chris Craft [102]
CALayer *topPaddleLayer;
CALayer *puckLayer;
StateData bottomPaddleData;
StateData topPaddleData;
UIImage *goalImage;
UIImage *blueWinsImage;
UIImage *redWinsImage;
int topScore;
int bottomScore;
double messageRotation;
NSTimer *animationTimer;
UILabel *scoreLabel;
UIImageView *messageView;
}
@property (nonatomic,assign) id /* @property (nonatomic) StateData bottomPaddleData; @property (nonatomic) StateData topPaddleData; @property (nonatomic) ConnectionType connectionType; - (void)startAnimation; - (void)stopAnimation; - (void)updatePuckData:(StateData)puckData; - (void)displayMessage:(MessageCode)messageCode forPlayer:(PlayerCode)playerCode; @end At the beginning of the implementation of HockeyTableView you will see a short list of private methods. It is helpful to separate fields and attributes of the interface this way, especially when a class becomes as large as this one: // HockeyTableView.m #import “HockeyTableView.h” // private methods @interface HockeyTableView () @property (nonatomic, assign) NSTimer *animationTimer; - (void)initializeContents; - (void)drawView; - (void)updateView; - (void)updatePuck; - (void)didScore:(PlayerCode)playerCode; @end In addition to the private section, we have included a handful of helper methods. These methods do not need to access any class members and could be moved out to another global library. The helper methods have been implemented in C instead of Objective-C to illustrate how you can seamlessly blend the two syntaxes in a single project in Xcode: // helper methods CGPoint normalVector(double x1, double x2, double y1, double y2); double distance(double x1, double x2, double y1, double y2); double paddleSpeed(StateData stateData); void correctTouchPoint(CGPoint *touchPoint); What is needed to communicate and control the hockey table HockeyViewController manages and hosts the HockeyTableView. All peer-to-peer communication that occurs between the two devices is managed right here in this class. This has been extremely beneficial in this project because we can come to one place for all of our communication maintenance: // HockeyViewController.h #import #import “FlipsideViewController.h” #import “MainView.h” #import “HockeyData.h” #import “MenuViewController.h” #import “RootViewController.h” @interface HockeyViewController : UIViewController RootViewController *rootViewController; MainView *mainView; GKPeerPickerController *picker; GKSession *session; ConnectionType connectionType; } @property (nonatomic, retain) GKSession *session; @property (nonatomic, retain) GKPeerPickerController *picker; @property (nonatomic, assign) RootViewController *rootViewController; - (IBAction)showInfo; - (IBAction)showMenu; - (void)sendData; - (void)showPeerPicker; - (void)startNearby; - (void)startHeadToHead; @end Focusing on the details Hopefully, the bird's-eye-view tour you just took of the class headers has helped you to see the big picture more clearly. For the rest of the code analysis, we will take you to ground level and review the details of the code. Please realize that not every method that makes HockeyTableView and HockeyViewController has been listed here. Several methods in these classes are the same run-of-the-mill methods you deal with every day in iPhone development. Remember, you can always download the entire project from http://appsamuckcom/airhockey to see all the mundane details. First we are going to look at method implementations for the class HockeyTableView, then we'll look at the ones for the class HockeyViewController. Setting up a hockey table The method initializeComponents of HockeyTableView is called during the construction of the class. This method is responsible for the following: