More helpful Cocos2d and Gaming macros

More helpful Cocos2d and Gaming macros

Here are w few macros that i wrote to make the code more readable and to compress it in order to make more sense. The most important part of development is to keep the code simple and readable. This has multiple benefits which range from ease of working in a team to manageable code. When you code is clean and organized, you will be able to concentrate more on game logic rather getting lost in the syntactical complexities of the language.

Purpose: gets the x position of the ccnode
#define PX(__NODE) __NODE.position.x

Purpose: gets the y position of the ccnode
#define PY(__NODE) __NODE.position.y

Purpose:gets the CGSize of the ccnode
#define CS(__NODE) __NODE.contentSize

Purpose:gets the width of the ccnode
#define WDT(__NODE) __NODE.contentSize.width

Purpose:gets the height of the ccnode
#define HGT(__NODE) __NODE.contentSize.height

Purpose: gets half the width of the ccnode
#define WDT2(__NODE) __NODE.contentSize.width/2

Purpose:gets half the height of the ccnode
#define HGT2(__NODE) __NODE.contentSize.height/2

Purpose:gets the center point of the ccnode
#define CENTER_POINT(__NODE) ccp(WDT2(__NODE),HGT2(__NODE))

Purpose:gets the screen size (CGSize)
#define viewportSize [[CCDirector sharedDirector] winSize]

Purpose:gets the center point of the screen
#define centerScreenPoint ccp([[CCDirector sharedDirector] winSize].width/2,[[CCDirector sharedDirector] winSize].height/2)

Purpose: calls release on an object and nullifies it
#define RELEASE_OBJECT(__NODE){
if (__NODE) {
[__NODE release];
__NODE = nil;
}
}

Purpose:calls release of each object held at the index of the array, releases the array itself and nullfies the pointer.
#define RELEASE_ARRAY(__NODE){
if (__NODE) {
[__NODE removeAllObjects];
[__NODE release];
__NODE = nil;
}
}

Purpose:removes the sprite from the display list and nullifies its pointer. It does NOT release the sprite. This macro is for objects owned by cocos2d classes/code
#define RELEASE_SPRITE(__NODE) {
if (__NODE) {
[__NODE removeFromParentAndCleanup:YES];
__NODE = nil;
}
}

Purpose:gets a random integer with a specified range
#define GET_RANDOM(min, max)
((rand()%(int)(((max) + 1)-(min)))+ (min))

Purpose:adds child in the mentioned parent and positions it
#define ADDCHILD_POSITION(_object, _parent, _position){
[_parent addChild:_object];
[_object setPosition:_position];
}

Purpose:adds child and positions it while giving it a tag and zindex
#define ADDCHILD_POSITION_TAG_ZINDEX(_object, _parent, _position, _tag, _zIndex){
[_parent addChild:_object z:_zIndex tag:_tag];
[_object setPosition:_position];
}

Purpose:gets the coordinates on the screen where the tap was made. used in ccTouchesBegan, ccTouchesMoved, ccTouchesEnded and ccTouchesCancelled functions
#define GET_TOUCH_POINT(__TOUCHES)
[[CCDirector sharedDirector] convertToGL:[[__TOUCHES anyObject] locationInView:[[__TOUCHES anyObject] view]]]

Purpose:Its a good practice to use such variables. A change in the font family later on will be quite problematic to make. Using this variable, single change will cause the change in the entire application.
#define APPLICATION_FONT_FAMILY @"Arial"

原文地址:https://www.cnblogs.com/Clin/p/3294387.html