---
title: "cocos2dx(v2.x)与(v1.x)的一些常用函数区别讲解"
author: "abc20899"
date: "2013-07-29T10:35:20Z"
tags: []
categories: ["移动开发"]
slug: "cocos2dx-v2-x-v1-x"
draft: false
lastmod: "2013-07-29T10:35:20Z"
id: 1914798
url: "https://abc20899.iteye.com/blog/1914798"
---

第一个改动:  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));