《cocos2d-x手机游戏开发:跨iOS、Android和沃Phone平台》讲了一个"魔塔"游戏,它是我看的第一本关于cocos2d-x的书,“魔塔”我小时候有玩过,没有想到若干年后能看到实现方式。但是比较遗憾的是这本书使用的cocos2d-x类库比较老,是cocos2d-0.99.5-x-0.8.5。我花了一点时间把整个代码升级到cocos2d-x v2.2。想要下载的童鞋直接拉到最底部进行下载。下面是部分新老API的变化。这种大量抛弃老API的方式还是第一次见到,不过我想这是移动开发的必然吧,新老API都存在,会影响类库文件大小,效率等。
//old GameLayer *gamelayer = GameLayer::node(); //new GameLayer *gamelayer = GameLayer::create(); //old LAYER_NODE_FUNC(GameLayer); //new CREATE_FUNC(GameLayer); //old CCLayerColor* fadeLayer = CCLayerColor::layerWithColor(ccc4(0, 0, 0, 0)); //new CCLayerColor* fadeLayer = CCLayerColor::create(ccc4(0, 0, 0, 0)); //old CCAction* action = CCSequence::actions( CCFadeIn::actionWithDuration(0.5f), CCCallFunc::actionWithTarget(this, callfunc_selector(GameScene::resetGameLayer)), NULL); //new CCAction* action = CCSequence::create( CCFadeIn::create(0.5f), CCCallFunc::create(this, callfunc_selector(GameScene::resetGameLayer)), NULL); //old heroSprite = CCSprite::spriteWithSpriteFrame(defaultFrame) //new heroSprite = CCSprite::createWithSpriteFrame(defaultFrame); //old CCAnimation* animation = new CCAnimation(); animation->initWithFrames(animFrames, 0.1f); //new CCAnimation* animation = CCAnimation::createWithSpriteFrames(animFrames, 0.1f); //old return cocos2d::CCAnimate::actionWithAnimation(anim); //new return cocos2d::CCAnimate::create(anim); //old npcSprite = CCSprite::spriteWithFile(imagePath->m_sString.c_str(), rect); //new npcSprite = CCSprite::create(imagePath->m_sString.c_str(), rect); //old frame0 = CCSpriteFrame::frameWithTexture(heroTexture, cocos2d::CCRectMake(32*0, 32*direction, 32, 32)); //new frame0 = CCSpriteFrame::createWithTexture(heroTexture, cocos2d::CCRectMake(32*0, 32*direction, 32, 32)); //old CCMenuItemImage::itemFromNormalImage CCMenu* pMenu = CCMenu::menuWithItems(pCloseItem, NULL); CCMenuItem *down = CCMenuItemFont::itemFromString("down", this, menu_selector(ControlLayer::menuCallBackMove)); //new CCMenuItemImage *pCloseItem = CCMenuItemImage::create CCMenu* pMenu = CCMenu::create(pCloseItem, NULL); CCMenuItem *down = CCMenuItemFont::create("down", this, menu_selector(ControlLayer::menuCallBackMove)); //old map = CCTMXTiledMap::tiledMapWithTMXFile("0.tmx"); //new map = CCTMXTiledMap::create("0.tmx");
CCMutableArray使用有点像stl 中的vector,需要定义一个类型,CCArray全是CCObject,需要用什么类型都要转换。遍历数组方式也变了,之前的像stl中的iterator,升级后使用CCARRAY_FOREACH宏。
//old enemyArray = new CCMutableArray<Enemy*>(); CCMutableArray<Enemy*>::CCMutableArrayIterator iter; for (iter = enemyArray->begin(); iter != enemyArray->end(); ++iter) { enemy = *iter; } //new enemyArray = new CCArray(); CCObject *arrayItem; CCARRAY_FOREACH(enemyArray,arrayItem){ enemy = (Enemy *)arrayItem; }
这个也变化较大,感觉cocos2d-x慢慢去掉了C++的stl风格。
//old teleportDict = new CCMutableDictionary<int, Teleport*>(); //获取对象层内的所有对象 CCMutableArray<CCStringToStringDictionary*> * objects = group->getObjects(); CCStringToStringDictionary* dict; CCMutableArray<CCStringToStringDictionary*>::CCMutableArrayIterator it; //遍历所有对象 for( it = objects->begin(); it != objects->end(); it++) { dict = (*it); int x = dict->objectForKey(key)->toInt(); CCString *type = dict->objectForKey(key); } //new teleportDict = CCDictionary::create(); teleportDict->retain(); //获取对象层内的所有对象 CCArray *objects = group->getObjects(); CCObject* objectItem = NULL; //遍历所有对象 CCARRAY_FOREACH(objects,objectItem) { CCDictionary *dict = (CCDictionary*)objectItem; int x = ((CCString*)dict->objectForKey(key))->floatValue(); CCString *type =(CCString*) dict->objectForKey(key); }
//old void Hero::updateOpenDoorAnimation(ccTime dt) //new void Hero::updateOpenDoorAnimation(float dt)
//old fightSprite->setIsVisible(false); //new fightSprite->setVisible(false);
http://www.waitingfy.com/?attachment_id=715
http://www.waitingfy.com/?p=711