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):
- 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的交互、物理系统、粒子系统。
- Cocos2d-x学习笔记(二)Cocos2d-x整体框架
- Cocos2D-X学习笔记(二)
- cocos2d-x+cocosbuilder 学习笔记 (二)
- Cocos2d-x学习笔记(二)
- Cocos2d-x学习笔记(二)------HelloWorld
- cocos2d-x Physics 学习笔记(二)
- cocos2d-x学习笔记(二)浅入动画教程。
- cocos2d-x学习笔记(二)--场景切换
- cocos2d-x 菜鸟学习笔记二(自适应屏幕分辨率)
- Cocos2d-x学习笔记(二) HelloCpp代码理解
- Cocos2d-x学习笔记(二)-------->CCDirector导演类
- 《cocos2d-x游戏开发之旅》学习笔记(二)
- 我的Cocos2d-x学习笔记(二十四)CCScrollView
- Cocos2d-x学习笔记(二) 永远的HelloWorld
- Cocos2d-x学习笔记(二) 永远的HelloWorld
- cocos2D-x 3.x学习笔记【二】 事件机制入门
- cocos2d-x 3.x学习笔记<二> 打包apk
- Cocos2d-x学习(二十一):cocos2d-x引擎结构
- Objective-c中Extension(延展)的用法,Objective-c中的私有
- OOAD之设计模式-结构模式
- C风格字符串结束符用作逻辑判断
- 文章6:Nginx中的Epoll事件处理机制
- 文章8:Nginx中与Master-Worker进程通信有关的内容
- Cocos2D-X学习笔记(二)
- 堆内存和栈内存
- 文章9:Nginx accept互斥锁
- scws的学习笔记
- 文本链接与裸链对百度SEO的作用
- Discuz! NT 到 Discuz! X2.5 转换程序
- hdu 1251 字典树
- FindFirstFile Function
- Ubuntu 下 Android NDK 开发环境搭建