Cocos2d-x Study Notes 03-exploring the interface elements of helloworld

Source: Internet
Author: User
Tags addchild


The helloworld project interface is initialized in the helloworld: Init () function.

Part 1: Create the close button in the lower right corner.

// Create a "close" menu item with close icon, it's an auto release object.
Ccmenuitemimage * pcloseitem = ccmenuitemimage: Create (
"Closenormal.png", // icon displayed normally
"Closeselected.png", // The icon displayed when you click
This,
Menu_selector (helloworld: menuclosecallback); // The End message function triggered after a click
Cc_break_if (! Pcloseitem); // determines whether the item is valid

// Place the menu item bottom-Right Conner.
Pcloseitem-> setposition (CCP (ccdirector: shareddire()-> getwinsize (). Width-20, 20 ));

// Create a menu with the "close" menu item, it's an auto release object.
Ccmenu * pmenu = ccmenu: Create (pcloseitem, null );
Pmenu-> setposition (ccpointzero );
Cc_break_if (! Pmenu );

// This button is also a layer. Use the addchild interface to add the layer to the scene.
This-> addchild (pmenu, 1 );


Part 2: Add helloworld to the scene.
// Create a label and initialize with string "Hello World ".
Cclabelttf * plabel = cclabelttf: Create ("cocos2d-x", "Arial", 32 );
Cc_break_if (! Plabel );

// Get window size and place the label upper.
Ccsize size = ccdirector: shareddire()-> getwinsize ();
Plabel-> setposition (CCP (size. width/2, size. Height-30 ));

// Add the label to helloworld layer as a child layer.
This-> addchild (plabel, 1 );

If you are adding a Chinese character, change the file type to UTF-8. (Save As-> set in advanced options)

Part 3: add the cocos2d-x of the logo image as the background.

Ccsprite * psprite = ccsprite: Create ("helloworld.png ");
Cc_break_if (! Psprite );

// Place the Sprite on the center of the screen
Psprite-> setposition (CCP (size. width/2, size. Height/2 ));

// Add the sprite to helloworld layer as a child layer.
This-> addchild (psprite, 0 );

The opaque part of the upper layer overwrites the lower layer. Therefore, the background image is placed on layer 0, and the text and buttons are placed on it.

The frame rate shown in the lower left corner is specified in the appdelegate: applicationdidfinishlaunching interface.

// Turn on display FPS
Pdirector-> setdisplaystats (true );

If it is set to false, the frame rate will not be displayed.

The preceding scenario elements are only added to the memory buffer and are not immediately displayed on the screen.

Drawscene is called in the mainloop () of the message loop, and then swapbuffer () is called. With this function, memory data is exchanged to the screen and the frames are displayed.

Reference: helloworld Project

I am not very familiar with beginners. If you have any mistakes, please correct them.

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.