(Original) cocos2d notes -- Analysis of helloworldscene

Source: Internet
Author: User

Helloworldscene class is pure coocs2dCodeDisplay the hello World tag. Before getting started, you need to understand that cocos2d uses a ccnode object level to determine where to display something. The base class of all nodes is the ccnode class, And the ccnode class is the parent class of all other node classes, including two basic elements: ccscene and cclayer.

Ccscene is an abstract concept. Only one thing is required. The correct position of the object in the scenario is given based on the pixel coordinates of the object. Ccscene node is always used as the parent object at the cocos2d scenario level.

The cclayer class has nothing to do except provide touch and accelerator input. You usually add it as the first class to ccscene, just because most games require at least simple touch input. if you open helloworldscene. h header file, you will find that the helloworld class comes from cclayer.

Because ccscene is only an abstract concept, the default method for setting a scenario is to use the static initiator + (ID) scene in your class. This method creates a common ccscene object and adds an instance of your class to the scenario. In most cases, it is the only place where ccscene is created and used. Next is a common example of the + (ID) scene method.

+(ID) scene {
Ccscene*Scene=[Ccscene node];
Id Node=[Helloworld node];
[Scene addchild: node];
ReturnScene;
}

In the following code,-(ID) Init breaks the law, and you will notice some strange things: Self is specified as the return value, the returned value is obtained through the init message sent to the super object through self = [Super init]. If you have a C ++ background, it will be painful to see this. Don't be upset. It's okay. It only means that in object-C, we must manually call the init method of the parent class. There is no automatic call to the parent class. We must specify self as the return value of [Super init] because it may return nil.

 
-(ID) Init {
If (Self = [Super init]) {
// Create and initialize a label
Cclabel * label = [cclabel labelwithstring: @ "Hello World" fontname: @ "marker felt" fontsize: 64];
 
// Get the window (screen) size from ccdirector
Cgsize size = [[ccdirector shareddire] winsize];
// Position the label at the center of the screen
Label. Position = cgpointmake (size. width/2, size. Height/2 );
// Add the label as a child to this layer
[Self addchild: Label];} return self ;}

Now let me explain how tags are added to the scenario. If you look at the init method of the code above, you will see that a static Initialization Method of init creates a cclabel object, which returns a new cclabel class object, this is an automatically released object. To make the init method no longer occupy memory after it ends, you must add the tag to self as a child. This is done by calling the [self addchild: Label] Message. In the method body shown in the code above, the tag is specified in the center of the screen. Note: it does not matter whether you specify a location before or after calling addchild.

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.