API style cocos2d-x

Source: Internet
Author: User

API style cocos2d-x

1. In the Cocos2d-x engine, except for the singleton mode, each cocos2d Class has its own static Class * Class: create (...) method. We strongly recommend this method. Sample Code:

Sprite* monster = Sprite::create("Monster.png");monster->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));this->addChild(monster);

2. doSomething ()
This is the most common function name, which is applied everywhere in the Cocos2d-x/-html5 engine. The first word is a verb, and the second word is a noun. For example, replaceScene (CCScene *) and getTexture ().

3. doWithResource ()
It is a variant of the doSomething () method. In initWithTexture (CCTexture *) and initWithFilename (const std: string &), you can often see this function name.

4. onEventCallback ()
When you see a function name similar to void onEnter (), the onAction type indicates that this is a callback function. When certain conditions are raised, other classes call this method.

5. In the Cocos2d-x engine, if you do not find the create (), only the getInstance () method is found, it belongs to the singleton mode class. For example, TextureCache: getInstance (). The structure of the singleton class is destroyInstance ().

6. The Cocos2d-x engine uses many getter and setters to change the attribute value. This usually affects the behavior of objects. For example, _ sprite-> setPosition (ccp (0, 0) moves the genie to the lower left corner. Based on performance optimization, the CCSize reference is returned here, instead of building another CCSize. When this CCSize & is changed, the internal member variables of the object will be affected, so this CCSize & is a constant.

If the property is "read-only", there will be no setProperty (type) method;
If the attribute is a bool value, the setProperty (bool) and isProperty () methods are available. For example, Sprite: isDirty () and Sprite: setDirty (bool bDirty ).
If the property is not a bool value, there will be setProperty (type) and getProperty () methods. For example, void Sprite: setTexture (Texture2D *) and Texture2D * CCSprite: getTexture ().



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.