Parse the overall framework and Startup Process of the Cocos2d Project

Source: Internet
Author: User

Cocos2dThe overall framework and startup process of the project are described in this article. Here we create a project named "Test2d ".XcodeThe file structure shown in Group & Files in is as follows:

Cocos2dSources: storesCocos2dSource code

Classes: stores the source code of this application.

Other Sources: main function of the program entry

Resources: stores images, icons, and sound files of the project.

Frameworks: Framework, followed by the START process

Enter from the main function:

 
 
  1. #import <UIKit/UIKit.h>    
  2.      
  3.  int main(int argc, char *argv[]) {    
  4.      NSAutoreleasePool *pool = [NSAutoreleasePool new];    
  5.     int retVal = UIApplicationMain(argc, argv, nil, @"Test2dAppDelegate");    
  6.      [pool release];    
  7.      return retVal;    
  8.  }  

The row 5th mark passes control of the program to the application proxy objects Test2dAppDelegate and Test2dAppDelegate.

 
 
  1. Test2dAppDelegate <SPAN style = "FONT-SIZE: 14px; LINE-HEIGHT: 21px; FONT-FAMILY: verdana, 'courier new'; WHITE-SPACE: normal "> the header file is as follows </SPAN>
  2. # Import <UIKit/UIKit. h>
  3. @ Interface Test2dAppDelegate: NSObject <UIApplicationDelegate> {
  4. UIWindow * window;
  5. }
  6. @ Property (nonatomic, retain) UIWindow * window;
  7. @ End

The third line shows that Test2dAppDelegate implements the system-defined application interface UIApplicationDelegate.

Various system events to be handled by the current application:

Give up control: applicationWillResignActive quit

Get control: applicationDidBecomeActive role

Memory alert: applicationDidReceiveMemoryWarning alert

Application exit prompt: applicationWillTerminate quit

System time change: applicationSignificantTimeChange

 
 
  1. // Give up control
  2. (Void) applicationWillResignActive :( UIApplication *) application {
  3. [[CCDirector shareddire] pause];
  4. // Obtain control
  5. Void) applicationDidBecomeActive :( UIApplication *) application {
  6. [[CCDirector shareddire] resume];
  7. }
  8. // Memory alarm
  9. (Void) applicationDidReceiveMemoryWarning :( UIApplication *) application {
  10. [[CCDirector shareddire] purgeCachedData];
  11.  
  12. //
  13. (Void) applicationDidEnterBackground :( UIApplication *) application {
  14. [[CCDirector shareddire] stopAnimation];
  15. }
  16. //
  17. Void) applicationWillEnterForeground :( UIApplication *) application {
  18. [[CCDirector shareddire] startAnimation];
  19. }
  20. // Program exit prompt
  21. (Void) applicationWillTerminate :( UIApplication *) application {
  22. [[CCDirector shareddire] end];
  23. // System time change
  24. (Void) applicationSignificantTimeChange :( UIApplication *) application {
  25. [[CCDirector sharedDirector] setNextDeltaTimeZero: YES];
  26. }

After the restore process is completed, the control of the program is passed toCocos2D-IPhoneClass Library,Cocos2D-IPhoneNext we will prepare for the main screen of the startup game:

1. The main window object (handle) is saved by the member window.

2. SetCocos2D-IPhoneIs bound to the "director" object.

3. Set the basic attributes of the "director" object.

 
 
  1. (Void) applicationDidFinishLaunching :( UIApplication *) application
  2. {
  3. // CC_DIRECTOR_INIT ()
  4. //
  5. // 1. Initializes an EAGLView with 0-bit depth format, and RGB565 render buffer
  6. // 2. EAGLView multiple touches: disabled
  7. // 3. creates a UIWindow, and assign it to the "window" var (it must already be declared)
  8. // 4. Parents EAGLView to the newly created window
  9. // 5. Creates Display Link Director
  10. // 5a. If it fails, it will use an NSTimer ctor
  11. // 6. It will try to run at 60 FPS
  12. // 7. Display FPS: NO
  13. // 8. Device orientation: Portrait
  14. // 9. Connects the director to the EAGLView
  15. //
  16. CC_DIRECTOR_INIT ();
  17. // Obtain the shared director in order...
  18. CCDirector * director = [CCDirector shareddire];
  19. /*********** Set the basic attributes of the "director" Object ***************/
  20. // Set the direction of the main window to vertical or horizontal)
  21. // Sets landscape mode
  22. [Director setDeviceOrientation: kCCDeviceOrientationLandscapeLeft];
 
 
  1. // Whether to display FPS display frames per second)
  2. // Turn on display FPS
  3. [Director setDisplayFPS: YES];
  4. // Set the relationship between the Director object and the current window to facilitate Director's operation on the Main Window
  5. // Turn on multiple touches
  6. EAGLView * view = [director openGLView];
  7. [View setMultipleTouchEnabled: YES];
  8. // Set the color palette width of the image displayed in the main window <BR> // Default texture format for PNG/BMP/TIFF/JPEG/GIF images
  9. // It can be RGBA8888, RGBA4444, RGB5_A1, RGB565
  10. // You can change anytime.
  11. [CCTexture2D setDefaultAlphaPixelFormat: kTexture2DPixelFormat_RGBA8888];
  12. // The scenario where the Director object starts and runs
  13. [[CCDirector shareddire] runWithScene: [HelloWorld scene];

Main image object for Cocos2d-iPhone-HellowWorldScene scenario

After the scene object HellowWorldScence obtains control, it initializes the Worker Number init to directly create a Lable with "Hello world" content in the main screen. Place the tag in the center of the screen.

 
 
  1. (id) init    
  2.  {    
  3.     // always call "super" init    
  4.     // Apple recommends to re-assign "self" with the "super" return value    
  5.      if( (self=[super init] )) {                
  6.          // create and initialize a Label    
  7.         CCLabel* label = [CCLabel labelWithString:@"Hello World" fontName:@"Marker Felt" fontSize:64];    
  8.          // ask director the the window size    
  9.          CGSize size = [[CCDirector sharedDirector] winSize];    
  10.           
  11.         // position the label on the center of the screen    
  12.          label.position =  ccp( size.width /2 , size.height/2 );                
  13.          // add the label as a child to this Layer    
  14.          [self addChild: label];    
  15.      }    
  16.      return self;    
  17.  }  

Cocos2D-The basic iPhone import framework is to ensure that the number of main items calls the correct application proxy object.

In the applicationDidFinishLaunching threshold of the application proxy object:

Create "layer" Object

Pass the layer to the newly created "scenario"

Use the scenario object created by the director "object running.

Summary: AnalysisCocos2dThe overall framework and startup process of the project have been introduced. I hope this article will help you!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.