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
1
2
3
4
5
6
<code>.h中:</code>
<code>1.x版本Layer函數</code>
<code>LAYER_NODE_FUNC(World);</code>
<code>2.x版本Layer函數</code>
<code>LAYER_CREATE_FUNC(World);</code>
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<code>.cpp中:</code>
<code>1.x版本的重寫函數:</code>
<code>CCScene* World::scene()</code>
<code>{</code>
<code> </code><code>CCScene *scene = CCScene::node();</code>
<code> </code><code>World *layer = World::node();</code>
<code> </code><code>scene->addChild(layer);</code>
<code> </code><code>return</code> <code>scene;</code>
<code>}</code>
<code>2.x版本的重寫函數:</code>
<code> </code><code>CCScene *scene = CCScene::create();</code>
<code> </code><code>World *layer = World::create();</code>
然後我們看第二個常用的CCArray的初始化:
<code>1.</code><code>x版本的CCArray建立:</code>
<code>CCArray</code><code>*</code><code>array</code><code>=</code> <code>CCArray</code><code>:</code><code>:</code><code>array</code><code>(</code><code>)</code><code>;</code>
<code>2.</code><code>x版本的CCArray建立: </code>
<code>CCArray</code><code>*</code><code>array</code><code>=</code> <code>CCArray</code><code>:</code><code>:</code><code>create</code><code>(</code><code>)</code><code>;</code>
第三個我們看檔案路徑相關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);<span style="color:#ff0000;">
</span>
第七個: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還發現比較重點區分的地方也一定會博文分享出來的。
本文轉蓬萊仙羽51CTO部落格,原文連結:http://blog.51cto.com/dingxiaowei/1366263,如需轉載請自行聯系原作者