Cocos2D-X学习笔记(二)

来源:互联网 发布:js 数字转换为汉字 编辑:程序博客网 时间:2024/06/05 09:18

Cocos2D-iPhone的接口形式是objective-c形式的,显然只是针对ios/mac平台。而Cocos2D-X号称是基于Cocos2D-iPhone, 只是api接口形式变成了cpp的形式,以实现跨平台。

也就是说Cocos2D-X这个游戏引擎的架构其实就是Cocos2D-iPhone的,只是对外接口接口形式变了而已,或许cocos2d-x会加入一些补充或修改,让这个架构更好用。对于跨平台的开发者来说,当然是好事。

 

下面的两段代码,正好说明了cocos2d-x的架构与api更cocos2d-iphone的联系

 

 1// cpp with cocos2d-x 2bool HelloWorld::init() 3{ 4  if ( CCLayer::init() ) 5  { 6    CCSize winSize = CCDirector::sharedDirector()->getWinSize(); 7    CCSprite *player = CCSprite::spriteWithFile("Player.png",  8                                    CCRectMake(0, 0, 27, 40) ); 9    player->setPosition( ccp(player->getContentSize().width/2, 10                              winSize.height/2) );11   this->addChild(player);12  }13  return true;14}
 1// objc with cocos2d-iphone 2-(id) init 3{ 4  if( (self=[super init] ))  5  { 6    CGSize winSize = [[CCDirector sharedDirector] winSize]; 7    CCSprite *player = [CCSprite spriteWithFile:@"Player.png"  8                                  rect:CGRectMake(0, 0, 27, 40) ]; 9    player.position = ccp(player.contentSize.width/2, 10                          winSize.height/2);11    [self addChild:player];12  }13  return self;14} 

 

从cocos2d-iphone转为使用cocos2d-X的用户需要注意下述几点(摘自:http://www.cocos2d-x.org/projects/cocos2d-x/wiki/Chapter_2_-_How_to_Add_a_sprite):

  1. 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()
  2. 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.
  3. Use setter to set the value of properties, “player.position = …”  ,  translate to player->setPosition(…)
  4. 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.
  5. 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(…, …);
  6. 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
  7. 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 “->”
  8. Use keyword “this” in cpp to instead “self” in objc.
  9. 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.
  10. 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的交互、物理系统、粒子系统。