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:
- #import <UIKit/UIKit.h>
-
- int main(int argc, char *argv[]) {
- NSAutoreleasePool *pool = [NSAutoreleasePool new];
- int retVal = UIApplicationMain(argc, argv, nil, @"Test2dAppDelegate");
- [pool release];
- return retVal;
- }
The row 5th mark passes control of the program to the application proxy objects Test2dAppDelegate and Test2dAppDelegate.
- Test2dAppDelegate <SPAN style = "FONT-SIZE: 14px; LINE-HEIGHT: 21px; FONT-FAMILY: verdana, 'courier new'; WHITE-SPACE: normal "> the header file is as follows </SPAN>
- # Import <UIKit/UIKit. h>
- @ Interface Test2dAppDelegate: NSObject <UIApplicationDelegate> {
- UIWindow * window;
- }
- @ Property (nonatomic, retain) UIWindow * window;
- @ 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
- // Give up control
- (Void) applicationWillResignActive :( UIApplication *) application {
- [[CCDirector shareddire] pause];
- // Obtain control
- Void) applicationDidBecomeActive :( UIApplication *) application {
- [[CCDirector shareddire] resume];
- }
- // Memory alarm
- (Void) applicationDidReceiveMemoryWarning :( UIApplication *) application {
- [[CCDirector shareddire] purgeCachedData];
-
- //
- (Void) applicationDidEnterBackground :( UIApplication *) application {
- [[CCDirector shareddire] stopAnimation];
- }
- //
- Void) applicationWillEnterForeground :( UIApplication *) application {
- [[CCDirector shareddire] startAnimation];
- }
- // Program exit prompt
- (Void) applicationWillTerminate :( UIApplication *) application {
- [[CCDirector shareddire] end];
- // System time change
- (Void) applicationSignificantTimeChange :( UIApplication *) application {
- [[CCDirector sharedDirector] setNextDeltaTimeZero: YES];
- }
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.
- (Void) applicationDidFinishLaunching :( UIApplication *) application
- {
- // CC_DIRECTOR_INIT ()
- //
- // 1. Initializes an EAGLView with 0-bit depth format, and RGB565 render buffer
- // 2. EAGLView multiple touches: disabled
- // 3. creates a UIWindow, and assign it to the "window" var (it must already be declared)
- // 4. Parents EAGLView to the newly created window
- // 5. Creates Display Link Director
- // 5a. If it fails, it will use an NSTimer ctor
- // 6. It will try to run at 60 FPS
- // 7. Display FPS: NO
- // 8. Device orientation: Portrait
- // 9. Connects the director to the EAGLView
- //
- CC_DIRECTOR_INIT ();
- // Obtain the shared director in order...
- CCDirector * director = [CCDirector shareddire];
- /*********** Set the basic attributes of the "director" Object ***************/
- // Set the direction of the main window to vertical or horizontal)
- // Sets landscape mode
- [Director setDeviceOrientation: kCCDeviceOrientationLandscapeLeft];
- // Whether to display FPS display frames per second)
- // Turn on display FPS
- [Director setDisplayFPS: YES];
- // Set the relationship between the Director object and the current window to facilitate Director's operation on the Main Window
- // Turn on multiple touches
- EAGLView * view = [director openGLView];
- [View setMultipleTouchEnabled: YES];
- // Set the color palette width of the image displayed in the main window <BR> // Default texture format for PNG/BMP/TIFF/JPEG/GIF images
- // It can be RGBA8888, RGBA4444, RGB5_A1, RGB565
- // You can change anytime.
- [CCTexture2D setDefaultAlphaPixelFormat: kTexture2DPixelFormat_RGBA8888];
- // The scenario where the Director object starts and runs
- [[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.
- (id) init
- {
- // always call "super" init
- // Apple recommends to re-assign "self" with the "super" return value
- if( (self=[super init] )) {
- // create and initialize a Label
- CCLabel* label = [CCLabel labelWithString:@"Hello World" fontName:@"Marker Felt" fontSize:64];
- // ask director the the window size
- CGSize size = [[CCDirector sharedDirector] winSize];
-
- // position the label on the center of the screen
- label.position = ccp( size.width /2 , size.height/2 );
- // add the label as a child to this Layer
- [self addChild: label];
- }
- return self;
- }
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!