Note that these are not complete equivalents from Cocos2D and most of the porting is done from Cocoa to C++ through Classes and Macros provided by the Cocos2D framework.
Instead NSString use string/const char* and do the formatting with CCString where needed.
1 2 3 4 |
NSString *str; NSString *start = [NSString stringWithFormat:@"%@",str]; |
1 2 3 4 5 6 7 8 |
string str; // or const char* str CCString *start = CCString::createWithFormat("%s",str); //get string from CCString const char* tmp = start->getCString(); //or string tmp(start->getCString()); |
This code example finds a substring between two given string patterns.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
NSString *start; NSString *end; NSString *haystack; NSRange startRange = [haystack rangeOfString:start]; NSRange endRange = [haystack rangeOfString:end]; if(startRange.location != NSNotFound && endRange.location != NSNotFound) { NSRange datarange = NSMakeRange(startRange.location + startRange.length, endRange.location(startRange.location + startRange.length)); NSString *sub = [haystack substringWithRange:datarange]; return deckdata; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
string str1; string str2; string haystack; //no real need for CCString here CCString *start = CCString::createWithFormat("%s",str1); CCString *end = CCString::createWithFormat("%s",str2); unsigned startRange = haystack.find(start->getCString()); unsigned endRange = haystack.find(end->getCString()); if(startRange != -1 && endRange != -1) { return haystack.substr( startRange + start->length(), endRange - (startRange + start->length())); } |
Use CCArray instead of NSMutableArray iterate with CCARRAY_FOREACH.
Important:
If you need an array for CGPoint objects you have to use CCPointArray with CCPoint objects instead.
Reason for this is, CCArray works with objects that inherit from CCNode class, which CCPoint doesen’t.(see CGPoint example)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
NSMutableArray *menus = [NSMutableArray arrayWithCapacity:100]; CCMenu *men; [menus addObject:men]; for(id element in menus) //iterate menus array { CCMenu *m = (CCMenu*) element; for (CCMenuItem* item in [m children]) //iterate children of each menu { [item setIsEnabled:NO]; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
CCArray *menus = CCArray::createWithCapacity(100); CCObject *m_item;//menu CCObject *mm_item;//menu item CCMenu *item; menus->addObject(item); CCARRAY_FOREACH(menus, m_item) { CCMenu *menu = dynamic_cast(m_item); // use dynamic cast sparingly, its expensive CCARRAY_FOREACH(menu->getChildren(), mm_item) { CCMenuItem *child = dynamic_cast(mm_item); child->setEnabled(false); } } |
Use CCPoint instead of CGPoint with CCPointArray.
1 2 3 4 5 6 7 8 9 10 11 12 |
NSMutableArray *arrAllPoints; NSMutableArray *points = [NSMutableArray arrayWithCapacity:nCurrNumOfPoints]; for(int i=0;i < nCurrNumOfPoints;i++) { CGPoint pt=arrAllPoints[i]; [points addObject:[NSValue valueWithCGPoint:ccp(pt.x,pt.y)]]; } [self setPoints:points]; |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
CCPointArray *arrAllPoints; CCPointArray *points = CCPointArray::create(nCurrNumOfPoints); for(int i=0;i < nCurrNumOfPoints;i++) { CCPoint pt = arrAllPoints->getControlPointAtIndex(i); points->addControlPoint(pt); } this->setPoints(points); |
1 2 3 4 5 6 7 |
[NSTimer scheduledTimerWithTimeInterval:timer target:self selector:@selector(myTimerMethod:) userInfo:nil repeats:NO]; |
1 2 3 4 5 6 |
CCDirector* pDirector = CCDirector::sharedDirector(); CCScheduler *scheduler = pDirector->getScheduler(); scheduler->scheduleSelector(schedule_selector(MyCCObject::myTimerMethod),this,timer,1,0,false); |
This is a Macro to create setter and getter methods for class members in one line.
Use it in your header file when defining members.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
class HelloWorld : { CC_SYNTHESIZE(CCPoint*,point,Point); //parameters - Type, member name, method name /* is equivalent with protected : CCPoint *point; public : virtual CCPoint* getPoint(void){ return point; } public : virtual void setPoint(CCPoint * ppoint){ point = ppoint; } */ } |
Hello World example for scene and layer.
Important:
Don’t release “autorelease” objects like CCScene and most of the objects instanciated with create.
Like the description implies they will be automatically released so there is no need to release them again, but if you do your programm will crash.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
class HelloWorld : public cocos2d::CCLayer { public: // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone virtual bool init(); // there's no 'id' in cpp, so we recommend returning the class instance pointer static cocos2d::CCScene* scene(); // implement the "static node()" method manually CREATE_FUNC(HelloWorld); }; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
CCScene* HelloWorld::scene() { // 'scene' is an autorelease object CCScene *scene = CCScene::create(); // 'layer' is an autorelease object HelloWorld *layer = HelloWorld::create(); // add layer as a child to scene scene->addChild(layer); // return the scene return scene; } // on "init" you need to initialize your instance bool HelloWorld::init() { ////////////////////////////// // 1. super init first if ( !CCLayer::init() ) { return false; } //add stuff to layer here menus, sprites etc. } |
Usage
1 2 3 4 5 6 7 8 |
CCDirector* pDirector = CCDirector::sharedDirector(); CCScene *pScene = HelloWorld::scene(); // run pDirector->runWithScene(pScene); |