Cocos2d notes (2) analyze the overall framework and Startup Process of the cocos2d Project

Source: Internet
Author: User

Here we create a project named "test2d". The file structure shown in Group & files in xcode is as follows:

Cocos2d sources: stores cocos2dSource code

Classes: stores the applicationProgramSourceCode

Other sources: main function of the program entry

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

Frameworks: Framework

 

Next step

Enter from the main function:

 

 
# Import <uikit/uikit. h> int main (INT argc, char * argv []) {ngutoreleasepool * Pool = [ngutoreleasepool new]; int retval = uiapplicationmain (argc, argv, nil, @ "test2dappdelegate"); [pool release]; return retval ;}

The 5th line mark passes control of the program to the application proxy objectTest2dappdelegate

 

 

Test2dappdelegate

 
Test2dappdelegateThe header file is as follows:

 

 
# Import <uikit/uikit. h> @ interface test2dappdelegate: nsobject <uiapplicationdelegate> {uiwindow * window;} @ property (nonatomic, retain) uiwindow * window; @ end

The third line showsTest2dappdelegateImplemented the application interface defined by the SystemUiapplicationdelegate

 

Various system events to be handled by the current application:

Give up control: applicationwillresignactive Get control: applicationdidbecomeactive Memory alert: applicationdidreceivememorywarning Application exit prompt: applicationwillterminate System time change: applicationsignificanttimechange

 

// Discard control-(void) applicationwillresignactive :( uiapplication *) Application {[ccdirector shareddirector] pause];} // obtain control-(void) applicationdidbecomeactive :( uiapplication *) application {[[ccdirector shareddire] Resume];} // memory alarm-(void) applicationdidreceivememorywarning :( uiapplication *) Application {[[ccdirector shareddire] purgecacheddata];} //-(void) applicationdidenterbackground :( uiapplication *) Application {[ccdirector shareddirector] stopanimation];} //-(void) applicationwillenterforeground :( uiapplication *) application {[[ccdirector shareddire] startanimation];} // The program exit prompt-(void) applicationwillterminate :( uiapplication *) Application {[[ccdirector shareddire] end];} // system time change-(void) applicationsignificanttimechange :( uiapplication *) Application {[ccdirector shareddire] setnextdeltatimezero: Yes];}

 

The number of shardsApplicationdidfinishlaunchingSetThe control of the program is passed to the Cocos2D-iPhone class library, and the Cocos2D-iPhone begins to prepare for the startup game main screen:

1. The main window object (handle) is saved by the member window.
2. Bind The "director" Object of the Cocos2D-iPhone to it.
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 main window direction (vertical or horizontal) 
// sets Landscape mode
[Director setdeviceorientation: kccdeviceorientationlandscapeleft];
// Whether to display FPS (number of 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 toucheseaglview * view = [Director openglview]; [view setmultipletouchenabled: Yes]; // set the color palette width of the image displayed in the Main Window
// 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]; // scenario where the Director object starts and runs [[ccdirector shareddire] runwithscene: [helloworld scene];}

 

Cocos2d-iPhoneMain image object-HellowworldsceneScenario

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 valueif (Self = [Super init]) {// create and initialize a labelcclabel * label = [cclabel labelwithstring: @ "Hello World" fontname: @ "marker felt" fontsize: 64]; // ask ctor the window sizecgsize size = [[ccdirector shareddire] winsize]; // position the label on the center of the screenlabel. position = CCP (size. width/2, size. height/2); // Add the label as a child to this layer [self addchild: Label];} return self ;}

 

The basic import framework of the Cocos2D-iPhone is:

 1) Make sure that the number of main objects calls the correct application proxy object.

2) In the applicationdidfinishlaunching threshold of the application proxy object

A) create a "layer" object. B) Pass the layer to the newly created "scenario" c) run the newly created "scenario" object through the "director.

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.