cocos2dx(v2.x)與(v1.x)的一些常用函數區別講解

初學,各種問題,發現時版本不一樣啦

轉自:http://xiaominghimi.blog.51cto.com/2614927/969984

cocos2dx v2.0版本發佈一段時間了,現在最新版本是 cocos2d-2.0-rc2-x-2.0.1 ;這段時間Himi對2.x的更新版也有關注,也嘗試使用過,發現不少地方都有改動,對於Himi最新項目快到尾聲的考慮,所以也沒有更新引擎到最新。那麼今天開始Himi將陸續使用最新v2.x版本的一些東東,同步更新一些經常使用的改動以及值得注意的地方發博文出來與大家共享;

在之前我們使用cocos2dx 1.x版本中,我們都知道,創建一個CCObject類,都是類名然後::類名去除CC這個規律來創建和初始化,但是這一條在Cocos2dx 2.x版本就不行了,在cocos2dx  2.x版本中初始化和創建類基本都是 create 關鍵字開頭創建。

首先我們來看第一個改動:  CCLayer初始化

自定義Layer,類名:World

.h中:
1.x版本Layer函數
LAYER_NODE_FUNC(World);

2.x版本Layer函數
LAYER_CREATE_FUNC(World);
.cpp中:
1.x版本的重寫函數:

CCScene* World::scene()
{
	CCScene *scene = CCScene::node(); 
	World *layer = World::node(); 
	scene->addChild(layer); 
	return scene;
}

2.x版本的重寫函數:

CCScene* World::scene()
{
	CCScene *scene = CCScene::create(); 
	World *layer = World::create(); 
	scene->addChild(layer); 
	return scene;
}

 

然後我們看第二個常用的CCArray的初始化:

1.x版本的CCArray創建: 
CCArray*array = CCArray::array();

2.x版本的CCArray創建:  
CCArray*array = CCArray::create();

 

 

第三個我們看文件路徑相關CCFileUtils函數使用:

1.x版本的使用: 
const char* fullpath = cocos2d::CCFileUtils::fullPathFromRelativePath(patha.c_str());

2.x版本的使用:
const char* fullpath = cocos2d::CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(patha.c_str());

 

 

第四個精靈的創建:  

1.x中精靈的創建:
 CCSprite *sp = CCSprite::spriteWithFile("himi.png");
2.x中精靈的創建:
 CCSprite *sp = CCSprite::create("himi.png");

 

 

第五個註冊觸屏事件監聽

1.x中註冊:
CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate(this, 0, false);

2.x中註冊:
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, false);

 

 

第六個粒子相關

1.x粒子創建和設置自動釋放設置
CCParticleSystem *tempSystem =  CCParticleSystem::particleWithFile("himi.plist");
        tempSystem->setIsAutoRemoveOnFinish(true);
        
2.x粒子創建和設置自動釋放設置        
CCParticleSystem *tempSystem =  CCParticleSystemQuad::create("himi.plist");
        tempSystem->setAutoRemoveOnFinish(true);

 

 

第七個:CCFileData 類去除了:

 

1.x的CCFileData的使用:
     
cocos2d::CCFileData fileDataClip(const char *pszFileName, const char *pszMode);
 
2.x中CCFileData被刪除,直接使用如下函數即可代替:
CCFileUtils::sharedFileUtils()->getFileData(const char *pszFileName, const char *pszMode, unsigned long *pSize)

 

 

第八個 Action 動作使用與創建:

1.x動作的創建與使用:
this->runAction(CCSequence::actions(
                                            CCMoveTo::actionWithDuration(ccpDistance(this->getPosition(), target) / velocity,
                                                                         target),
                                            CCCallFunc::actionWithTarget(this, callfunc_selector(Player::removeTarget))
                                            ,NULL));
 
2.x的動作創建和使用:      
this->runAction(CCSequence::create(
                                           CCMoveTo::create(ccpDistance(this->getPosition(), target) / velocity,
                                                            target),
                                           CCCallFunc::create(this, callfunc_selector(Player::removeTarget))
                                           ,NULL));

 

其實以上這幾個例子比較有代表性了,其他的一些區分我想大家也能找到不一定的規律。那麼本篇對於cocos2dx v2.0版本的差異就講述到這,後續如果Himi還發現比較重點區分的地方也一定會博文分享出來的。


發佈了114 篇原創文章 · 獲贊 35 · 訪問量 91萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章