@import url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);
@import url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);
@import url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);
cocos2dx封装了一些常用的数据结构,我们分为两个部分介绍。一部分是对基本数据类型,int、float、double、bool的装箱。一部分是比较复杂的复合数据结构__String、__Array、__Dictionary、Size、Rect、Point、这些数据结构大多数是用C++方式重写了OC语言中Foundation框架的接口。所以有OC底子的可以调过,大概浏览即可。
第一部分:
int对应的装箱是Integer,在cocos2dx中创建这种整数对象是用create方法,auto pInt = Integer::create(30);
int ---------auto pInt = __Integer::create(2);
double ---------auto pDouble = __Double::create(1.0);
float ---------auto pFloat = __Float::create(2.0);
第二部分:
复合数据结构
__String
auto pStr = String::create("cocos2dx");
auto pStr2 = String::createWithFormat("I love %s", pStr->getCString());
bool isEqualEach = pStr->isEqual(pStr2);
log("%s",isEqualEach == true?"Equal":"not Equal");
#结果是notEqual
__Array
Array对象是对C++中数组的封装。Array中的元素是Ref、可以存不同的数据类型。
创建一个空数组,Array::create()
增:add系列、addObject 删:removeXXX系列,代表removeObjectAtIndext() 改:replaceObjectAtIndex() 查:get系列、getIndexOfObject()
__Dictionary
创建字典对象,auto pDict = Dictionary::create()
添加键值对,pDict->setObject(obj, "key");
删除键对应的值, pDict->removeObjectForKey("someKey");
查 ,pDict->ObjectForKey("someKey");
auto pDict = Dictionary::create();
auto pV1 = String::create("V1");
auto pV2 = String::create("V2");
pDict->setObject(pV1, "key1");
pDict->setObject(pV2, "key2");
String *pStr1 = (String*)pDict->ObjectForKey("key1");
String *pStr2 = (String*)pDict->ObjectForKey("key2");
log("str1 = %s,str2 = %s", pStr1->getCString(), pStr2->getCString());
#使用ObjectForKey获得的是Object对象,要强制类型转换为你确定的那个类型。
Size、Rect、Point
Size于Rect的区别是Size只有长宽的属性,而Rect还有个起始点属性,他们都是表示一块矩形区域。
其中Rect最常使用的是用来判断某个点在不再这个矩形区域内。其实他们都是对象,不像OC中是结构体。
1 //生成两个点
2 Point point1 = Point(10,10);
3 Point point2 = Point(60,60);
4 Point point3;
5 //点1与x轴的夹角
6 log("\n点1(%f,%f)与x轴的夹角为:%f",point1.x,point1.y,point1.getAngle());
7 //两个点的夹角
8 log("\n点1(%f,%f)与点2(%f,%f)的夹角为:%f",point1.x,point1.y,point2.x,point2.y,point1.getAngle(point2));
9 //两个点的距离
10 log("\n点1(%f,%f)与点2(%f,%f)的距离为:%f",
11 point1.x,point1.y,point2.x,point2.y,point1.getDistance(point2));
12 //两个点相加
13 point3 = point1 + point2;
14 log("\n(%f,%f)+(%f,%f)=(%f,%f)",point1.x,point1.y,point2.x,point2.y,point3.x,point3.y);
15 //两个点相减
16 point3 = point1-point2;
17 log("\n(%f,%f)-(%f,%f)=(%f,%f)",point1.x,point1.y,point2.x,point2.y,point3.x,point3.y);
18 //除法
19 point3 = point1/2;
20 log("\n(%f,%f)/2=(%f,%f)",point1.x,point1.y,point3.x,point3.y);
21 //乘法
22 point3 = point1*2;
23 log("\n(%f,%f)+(%f,%f)=(%f,%f)",point1.x,point1.y,point2.x,point2.y,point3.x,point3.y);
24
25 //使用SizeMake创建两个Size
26 Size size1 = Size(10, 20);
27 Size size2 = Size(50, 60);
28 Size size3;
29 //两个Size相加
30 size3 = size1 + size2;
31 log("size(%f,%f)+size(%f,%f)=size(%f,%f)",size1.width,size1.height,size2.width,size2.height,size3.width,size3.height);
32 //两个Size相减
33 size3 = size1 - size2;
34 log("size(%f,%f)-size(%f,%f)=size(%f,%f)",size1.width,size1.height,size2.width,size2.height,size3.width,size3.height);
35 //Size乘法
36 size3 = size1*10;
37 log("size(%f,%f)*10=size(%f,%f)",size1.width,size1.height,size3.width,size3.height);
38 //Size除法
39 size3 = size1/10;
40 log("size(%f,%f)/10=size(%f,%f)",size1.width,size1.height,size3.width,size3.height);
41
42
43 //Rect测试
44 //生成一个坐标为10,20,宽为50,高为30的矩形区域
45 Rect rect = Rect(10, 20, 50, 30);
46 //生成两个点
47 point1 = Point(15,25);
48 point2 = Point(100,100);
49 if (rect.containsPoint(point1)) {
50 log("rect包含点point1\n");
51 }else{
52 log("rect不包含点point1\n");
53 }
54 if (rect.containsPoint(point2)) {
55 log("rect包含点point2\n");
56 }else{
57 log("rect不包含点point2\n");
58 }
59 //获取rect矩形区域最左、右、上、下、中间的坐标点
60 float maxX = rect.getMaxX();
61 float minX = rect.getMinX();
62 float maxY = rect.getMaxY();
63 float minY = rect.getMinY();
64 float midX = rect.getMidX();
65 float midY = rect.getMidY();
66 log("rect的左下角坐标为(%f,%f)\n左上角坐标为(%f,%f)\n右下角坐标为(%f,%f)\n右上角角坐标为(%f,%f)\n中点坐标为(%f,%f)\n"
67 ,minX,minY,minX,maxY,maxX,minY,maxX,maxY,midX,midY);
by sixleaves