cocos2d 学习笔记 - 项目启动关键代码摘录
总结出来的调用流程是:
main ---> AppController ---> MyNavigationController ---> IntroLayer --->HelloWorldLayer
<main.m>
1 int main(int argc, char *argv[]) {
2
3 ...
4 int retVal = UIApplicationMain(argc, argv, nil, @"AppController");
5 ...
6 }
<AppDelegate.h>
1 @interface MyNavigationController : UINavigationController <CCDirectorDelegate>
2 @end
3
4 @interface AppController : NSObject <UIApplicationDelegate>
5 {
6 UIWindow *window_;
7 MyNavigationController *navController_;
8
9 CCDirectorIOS *director_; // weak ref
10 }
11
12 @property (nonatomic, retain) UIWindow *window;
13 @property (readonly) MyNavigationController *navController;
14 @property (readonly) CCDirectorIOS *director;
15
16 @end
<AppDelegate.m>
1 @implementation MyNavigationController
2
3 // This is needed for iOS4 and iOS5 in order to ensure
4 // that the 1st scene has the correct dimensions
5 // This is not needed on iOS6 and could be added to the application:didFinish...
6 -(void) directorDidReshapeProjection:(CCDirector*)director
7 {
8 //...
9 if(director.runningScene == nil) {
10 // Add the first scene to the stack. The director will draw it immediately into the framebuffer. (Animation is started automatically when the view is displayed.)
11 // and add the scene to the stack. The director will run it when it automatically when the view is displayed.
12 [director runWithScene: [IntroLayer scene]];
13 }
14 }
15 @end
16
17 @implementation AppController
18 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
19 {
20 //...
21 // Create the main window
22 window_ = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
23 CCGLView *glView = [CCGLView viewWithFrame:[window_ bounds]
24 pixelFormat:kEAGLColorFormatRGB565
25 depthFormat:0
26 preserveBackbuffer:NO
27 sharegroup:nil
28 multiSampling:NO
29 numberOfSamples:0];
30 director_ = (CCDirectorIOS*) [CCDirector sharedDirector];
31 // attach the openglView to the director
32 [director_ setView:glView];
33 // Create a Navigation Controller with the Director
34 navController_ = [[MyNavigationController alloc] initWithRootViewController:director_];
35 // for rotation and other messages
36 [director_ setDelegate:navController_];
37 // set the Navigation Controller as the root view controller
38 [window_ setRootViewController:navController_];
39 //...
40 }
41 //...
42 @end
<IntroLayer.h>
1 @interface IntroLayer : CCLayer
2 {
3 }
<IntroLayer.m>
1 @implementation IntroLayer
2 //...
3 -(void) onEnter
4 {
5 [super onEnter];
6 [[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:1.0 scene:[HelloWorldLayer scene] ]];
7 }
8 @end
<HelloWorldLayer.h>
1 @interface HelloWorldLayer : CCLayer <GKAchievementViewControllerDelegate, GKLeaderboardViewControllerDelegate>
2 {
3 }