Attribute accessors in Cocos2d-x

Source: Internet
Author: User
Class C members only have methods and fields, and there are no attributes and events, which makes it inconvenient for developers. To implement the attribute function provided in Objective-C, we have to use methods to simulate get and set accessors. The Cocos2d-x specifies that the method name for the property accessor is prefixed with get or set followed by the property name. CCNode contains a large number of attributes, for example

Class C members only have methods and fields, and there are no attributes and events, which makes it inconvenient for developers. To implement the attribute function provided in Objective-C, we have to use methods to simulate get and set accessors. The Cocos2d-x specifies that the method name for the property accessor is prefixed with get or set followed by the property name. CCNode contains a large number of attributes, for example

C ++ class members only have methods and fields, and there are no attributes and events, which makes it inconvenient for developers. To implement the attribute function provided in Objective-C, we have to use methods to simulate get and set accessors. The Cocos2d-x specifies that the method name for the property accessor is prefixed with get or set followed by the property name. CCNode contains a large number of attributes, such as the Tag attribute used for marking the node. Its accessors are getTag () and setTag (int aTag). The implementation principle is roughly as follows:

Int tag;

Int getTag () {return tag ;}

Void setTag (int aTag) {tag = aTag ;}

In this example, the attribute type is int, which is easier to process. Because of the memory management, the development of our numerical type, struct type, Cocos2d-x object processing methods are different, this point in the reader to understand the Cocos2d-x Memory Management introduced in Section 2.3, I will have a deeper understanding.

Writing one or two accessors for each attribute is a very boring task, and to avoid repetitive work, the Cocos2d-x provides a series of macros to help us easily create attributes. Table 2-1 lists all attributes related macros, which are defined in "platform/CCPlatformMacros. h" in the Engine Directory.
Table 2-1 properties-related macros in the Cocos2d-x


Macro Description
CC_PROPERTY Defining an attribute and Its accessors is not implemented. It is usually used for simple value types.
CC_PROPERTY_READONLY Defines an attribute that only contains the get accessors and is not implemented.
CC_PROPERTY_PASS_BY_REF Defines an attribute. The accessors use the reference type to pass parameters, which is not implemented. It is usually used for struct type
CC_PROPERTY_READONLY_PASS_BY_REF Defines an attribute that only contains the get accessors and uses the reference type to pass parameters.

(Continued)

Macro Description
CC_PROPERTY_READONLY_PASS_BY_REF Defines an attribute that only contains the get accessors and uses the reference type to pass parameters.
CC_SYNTHESIZE Same as CC_PROPERTY, implements the accesser Method
CC_SYNTHESIZE_READONLY Same as CC_PROPERTY_READONLY, implements the accessors Method
CC_SYNTHESIZE_PASS_BY_REF Same as CC_PROPERTY_PASS_BY_REF, The accessors method is implemented.
CC_SYNTHESIZE_READONLY_PASS_BY_REF Same as CC_PROPERTY_READONLY_PASS_BY_REF, implements the accesser Method
CC_SYNTHESIZE_RETAIN Same as CC_PROPERTY, implements the accesser method. Used to derive the type from CCObject, The accessors take the memory management mechanism of the Cocos2d-x to automatically maintain the reference count of the object

You only need to write these macros in the class definition. Each macro has three parameters:

VarType: attribute type. If the attribute type is an object, it must be written as a pointer;

VarName, private field name of the attribute;

FunName, the name of the accessor of the attribute, that is, the part immediately after the get or set prefix.

Using the macros provided by the Cocos2d-x, the Tag attribute definition above can be replaced with the following statement:

CC_SYNTHESIZE (int, tag, Tag)

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.