Cocos2D-iPhone的接口形式是objective-c形式的,显然只是针对ios/mac平台。而Cocos2D-X号称是基于Cocos2D-iPhone, 只是api接口形式变成了cpp的形式,以实现跨平台。
也就是说Cocos2D-X这个游戏引擎的架构其实就是Cocos2D-iPhone的,只是对外接口接口形式变了而已,或许cocos2d-x会加入一些补充或修改,让这个架构更好用。对于跨平台的开发者来说,当然是好事。
下面的两段代码,正好说明了cocos2d-x的架构与api更cocos2d-iphone的联系
| |
从cocos2d-iphone转为使用cocos2d-X的用户需要注意下述几点(摘自:http://www.cocos2d-x.org/projects/cocos2d-x/wiki/Chapter_2_-_How_to_Add_a_sprite):
- Don’t use __super in C++ to instead super in objc. The keyworkd __super is only recognized by VC++, but can not compiled by GCC. So you had better to call the name of parent class, CCLayer::init()
- There’s no the concept of property. So the properties in objc, we use get/set methods instead. For example, if you want to fetch the contentSize property of CCSprite , you must call sprite->getContentSize() method. The aleph of cotentSize should be trun to capital “C”, then add “get” prefix to it.
- Use setter to set the value of properties, “player.position = …” , translate to player->setPosition(…)
- But the access to members of structures isn’t following this rule. E.g. there’re no getter/setter wrapper with the “width” & “height” in winSize structure.
- You needn’t to explain the usage of each params in parameter list like objc. For example, [CCSprite spriteWithFile …, rect …]; is translated to CCSprite::spriteWithFile(…, …);
- We have implemented some frequently used functions of CGGeometry, such as CGRectMake, CGPointMake, CGSizeMake, CGPointZero, CGSizeZero, CGRectZero. You can find them in cocos2dx/include/CCGeometry.h. The featrues of them are the same to iOS. Just a little difference, to avoid naming conflicts, classes with CG, NS, UI prefix, is change to CC prefix in cocos2d-x
- All the gaming elements in cocos2d-x, such as sprite, layer, scene, label, action, are allocated in the heap. So we must call their methods by “->”
- Use keyword “this” in cpp to instead “self” in objc.
- The return type of init method is “bool” now. There’s no keyword “id” in cpp, so the methods returning “id” is translated to a object pointer or bool.
- For android, the title bar has ocupied some space, so you should set the player's position to ccp(player.contentSize.width/2 + 40, winSize.height/2).
应该如何学习这个游戏平台呢?理了下思路,学习的内容大概如下:程序架构、内存管理机制、update机制、sprite、场景、菜单、变换、常用对象、与宿主os的交互、物理系统、粒子系统。