02-cocos2dx-基本内功第一部-常用宏定义
这篇主要总结下cocos2dx中常用到的3大宏定义(数学类宏定义、数据结构相关宏定义、对象相关宏定义),由于在win下的vs编译太慢,所以这些测试代码都是在mac下编译的,有如下
之所以分为三大类是为了方便记忆、归纳。分类不在于分细、而在于简约、容易记忆。
1.数学相关的宏
CCRANDOM_MINUS1_1()、CCRANDOM_0_1()、CC_DEGREES_TO_RADIANS、CC_RADIANS_TO_DEGREES()
#1.2断言宏
1.数学相关的宏
CCRANDOM_MINUS1_1()、CCRANDOM_0_1()、CC_DEGREES_TO_RADIANS、CC_RADIANS_TO_DEGREES()
1 log("CCRANDOM_MINUS1_1=%f", CCRANDOM_MINUS1_1());
2 log("CCRANDOM_0_1=%f", CCRANDOM_0_1());
3 log("CC_DEGREES_TO_RADIANS(30)=%f", CC_DEGREES_TO_RADIANS());
4 log("CC_RADIANS_TO_DEGREES(180)=%f",CC_RADIANS_TO_DEGREES());
5 int x = 10;
6 int y = 20;
7 CC_SWAP(x, y, int);
8 log("交换后x=%d,y=%d",x,y);
输出:
2 log("CCRANDOM_0_1=%f", CCRANDOM_0_1());
3 log("CC_DEGREES_TO_RADIANS(30)=%f", CC_DEGREES_TO_RADIANS());
4 log("CC_RADIANS_TO_DEGREES(180)=%f",CC_RADIANS_TO_DEGREES());
5 int x = 10;
6 int y = 20;
7 CC_SWAP(x, y, int);
8 log("交换后x=%d,y=%d",x,y);
1 cocos2d: CCRANDOM_MINUS1_1()=-0.999984
2 cocos2d: CCRANDOM_0_1()=0.131538
3 cocos2d: CC_DEGREES_TO_RADIANS()=0.523599
4 cocos2d: CC_RADIANS_TO_DEGREES()=20626.480469
5 cocos2d: x=20,y=10
2 cocos2d: CCRANDOM_0_1()=0.131538
3 cocos2d: CC_DEGREES_TO_RADIANS()=0.523599
4 cocos2d: CC_RADIANS_TO_DEGREES()=20626.480469
5 cocos2d: x=20,y=10
把这个归位数学类,是因为我认为,断言本身就是属于数学的一种抽象名词
输出:
CCDICT_FOREACH
需要注意的是dictionary中得元素都是DictElement类型,其封装了每个元素的Object和对应的key。
输出: cocos2d: monster HP = 100
ASSERT(cone, msg);
第一个参数是条件,为false则提示msg消息。
1 Point *point = NULL;
2 // CCAssert(point != NULL,"something wrong");
3 CCASSERT(point != NULL, "somthing wrong");
2 // CCAssert(point != NULL,"something wrong");
3 CCASSERT(point != NULL, "somthing wrong");
1 cocos2d: Assert failed: somthing wrong
2 Assertion failed: (point != __null), function init, file /Users/mac/Desktop/gameDevelopment/1410/testMacro/Classes/HelloWorldScene.cpp, line 39.
2 Assertion failed: (point != __null), function init, file /Users/mac/Desktop/gameDevelopment/1410/testMacro/Classes/HelloWorldScene.cpp, line 39.
2.与特定数据结构相关的宏(主要用来遍历、如同迭代器)
CCARRAY_FOREACH、CCDICT_FOREACH
CCARRAY_FOREACH
1 __Array * arrayMe = __Array::create();
2 arrayMe->addObject(__Integer::create(1));
3 arrayMe->addObject(__Integer::create(2));
4 arrayMe->addObject(__Integer::create(3));
5 Ref * ref = NULL;
6 CCARRAY_FOREACH(arrayMe, ref) {
7 Integer *pInt = (Integer *)ref;
8 log("CCARRAY_FOREACH:%d", pInt->getValue());
9 }
2 arrayMe->addObject(__Integer::create(1));
3 arrayMe->addObject(__Integer::create(2));
4 arrayMe->addObject(__Integer::create(3));
5 Ref * ref = NULL;
6 CCARRAY_FOREACH(arrayMe, ref) {
7 Integer *pInt = (Integer *)ref;
8 log("CCARRAY_FOREACH:%d", pInt->getValue());
9 }
输出:
cocos2d: CCARRAY_FOREACH:1
cocos2d: CCARRAY_FOREACH:2
cocos2d: CCARRAY_FOREACH:3
1 __Dictionary * dict = __Dictionary::create();
2 dict->setObject(__Integer::create(1), "one");
3 dict->setObject(__Integer::create(2), "two");
4 dict->setObject(__Integer::create(3), "three");
5 DictElement *el = NULL;
6 CCDICT_FOREACH(dict, el) {
7 __Integer *pVlaue = (__Integer*)el->getObject();
8 log("KEY=%s,CCDICT_FOREACH %d",el->getStrKey(),pVlaue->getValue());
9 }
2 dict->setObject(__Integer::create(1), "one");
3 dict->setObject(__Integer::create(2), "two");
4 dict->setObject(__Integer::create(3), "three");
5 DictElement *el = NULL;
6 CCDICT_FOREACH(dict, el) {
7 __Integer *pVlaue = (__Integer*)el->getObject();
8 log("KEY=%s,CCDICT_FOREACH %d",el->getStrKey(),pVlaue->getValue());
9 }
输出:
cocos2d: KEY=one,CCDICT_FOREACH 1
cocos2d: KEY=two,CCDICT_FOREACH 2
cocos2d: KEY=three,CCDICT_FOREACH 3
3.对象相关宏定义
#3.1对象创建方法宏CREATE_FUNC
这里直接贴出这个的宏定义,其主要意思
就是先new、new完后是通过init初始化,而不是构造函数。如果
init返回false,也就是失败。则释放对象返回NULL。否则,把其加入
自动管理的内存池,然后返回该对象的引用(本质是指针)。
1 #define CREATE_FUNC(__TYPE__) \
2 static __TYPE__* create() \
3 { \
4 __TYPE__ *pRet = new __TYPE__(); \
5 if (pRet && pRet->init()) \
6 { \
7 pRet->autorelease(); \
8 return pRet; \
9 } \
10 else \
11 { \
12 delete pRet; \
13 pRet = NULL; \
14 return NULL; \
15 } \
16 }
17
2 static __TYPE__* create() \
3 { \
4 __TYPE__ *pRet = new __TYPE__(); \
5 if (pRet && pRet->init()) \
6 { \
7 pRet->autorelease(); \
8 return pRet; \
9 } \
10 else \
11 { \
12 delete pRet; \
13 pRet = NULL; \
14 return NULL; \
15 } \
16 }
17
#3.2属性定义宏
CC_PROPERTY(tpye, varName, funName);
这个功能其就是用C++得方式,实现了C#中的属性,通过这个宏定义,
可以自动生产protected的成员变量,和public的虚setter、getter方法
具体的setter、getter实现需要,自己实现。如下
//Monster.h
#ifndef __Monster_H__
#define __Monster_H__
#include "cocos2d.h"
USING_NS_CC;
class Monster:public Sprite {
CC_PROPERTY(int, _monsterHp, MonsterHp);
public:
virtual bool init();
CREATE_FUNC(Monster);
};
#endif
//Monster.cpp
//
// Monster.cpp
// testMacro
//
// Created by sixleaves on 14-10-9.
//
//
#include "Monster.h"
void Monster::setMonsterHp(int var) {
_monsterHp = var;
}
int Monster::getMonsterHp() {
return _monsterHp;
}
bool Monster::init() {
return true;
}
//HelloWorldScene.cpp
auto monster = Monster::create();
monster->setMonsterHp(100);
log("monster HP = %d", monster->getMonsterHp());
#ifndef __Monster_H__
#define __Monster_H__
#include "cocos2d.h"
USING_NS_CC;
class Monster:public Sprite {
CC_PROPERTY(int, _monsterHp, MonsterHp);
public:
virtual bool init();
CREATE_FUNC(Monster);
};
#endif
//Monster.cpp
//
// Monster.cpp
// testMacro
//
// Created by sixleaves on 14-10-9.
//
//
#include "Monster.h"
void Monster::setMonsterHp(int var) {
_monsterHp = var;
}
int Monster::getMonsterHp() {
return _monsterHp;
}
bool Monster::init() {
return true;
}
//HelloWorldScene.cpp
auto monster = Monster::create();
monster->setMonsterHp(100);
log("monster HP = %d", monster->getMonsterHp());
提示:还有CC_RROPERTY_XXXX系列的其他宏定义,这里只介绍这个,因为比较常使用,其他自己了解。
2014.10.09 by sixleaves