Cocos2d-X 1.x 代码转到 Cocos2d-X 2.x 需要替换的东西

时间:2023-02-07 19:54:49

1.先添加/修改几个系统参数:

在系统环境变量中加入变量名COCOS2DX_HOME变量值就是cocos2d-x的目录;

在系统环境变量Path中添加%COCOS2DX_HOME%\Debug.win32;就是把动态链接库的路径告诉系统,这样就可以不用把dll都拷贝到syswow64或是system32系统目录下了。

2.再修改一下cocos2d-x目录下的文件:

\template\msvc\CCAppWiz.win32\Scripts\1033\default.js

找到 // Additional Inlcude Directories

(不要说我的拼写有问题,源文件中就是这样的)

把这句以下的所有 SolutionDir都替换成COCOS2DX_HOME 

COCOS2DX_HOME就是写在系统环境变量里的cocos2d-x根目录

找到 LinkTool.AdditionalLibraryDirectories = '$(OutDir)';

替换为

            if (bDebug)
            LinkTool.AdditionalLibraryDirectories = '$(COCOS2DX_HOME)\\Debug.win32';
            else
            LinkTool.AdditionalLibraryDirectories = '$(COCOS2DX_HOME)\\Release.win32';


======================割======================================

create-android-project.bat

1.生成Android工程的批处理修改_CYGBIN_ANDROIDTOOLS_NDKROOT对应到自己的SDK目录

2.for /f "delims=" %%A in ('%_CYGBIN%\cygpath.exe "%~dpn0.sh"') do set _CYGSCRIPT=%%A前后添加pushd %COCOS2DX_HOME%popd可注掉批处理检测代码(

:: Check if it was run under cocos2d-x root
::if not exist "%cd%\create-android-project.bat" echo Error!!! You should run it under cocos2dx root & pause & exit 2    

echo "%COCOS2DX_HOME%"

rem chaeck create-android-project.sh
:: if not exist "%~dpn0.sh" echo Script "%~dpn0.sh" not found & pause & exit 3

)COCOS2DX_HOME为写在系统环境变量里的cocos2d-x根目录


%COCOS2DX_HOME%\template\android

1.build_native.sh

# paths之后添加变量(分别对应自己的sdk目录):
NDK_ROOT=/cygdrive/F/SDK/android-ndk
COCOS2DX_ROOT=/cygdrive/F/SDK/cocos2d-x

注释掉:#COCOS2DX_ROOT="$DIR/../.."

修改这个的目的是可以在cocos2d-x根目录外进行编译了。

2.copy_files.sh

    2.0.3版本的这个文件有问题,缺少copy_library_src(){
    cp -rf $COCOSJAVALIB_ROOT/src/* $APP_DIR/proj.android/src/
}

    根据以前版本加上copy_library_src就行了



======================割======================================

3.接下来开始替换代码中的函数名:

先贴一下网上找到的:


AppDelegate::initInstance()
 --> 清除,在main中实现相应功能
CCNotificationCenter::sharedNotifCenter();
 --> CCNotificationCenter::sharedNotificationCenter();
pDirector->setOpenGLView(&CCEGLView::sharedOpenGLView());
 --> pDirector->setOpenGLView(CCEGLView::sharedOpenGLView());
pDirector->setDisplayFPS(true);
 --> pDirector->setDisplayStats(true);
CCDirector::sharedDirector()->pause();
 --> CCDirector::sharedDirector()->stopAnimation();
CCDirector::sharedDirector()->resume();
 --> CCDirector::sharedDirector()->startAnimation();
LAYER_NODE_FUNC(World);  
 --> CREATE_FUNC(World);
CCScene::node();  
 --> CCScene::create();
--Layer相似
CCMutableArray<CCSpriteFrame*> *animFrames = new CCMutableArray<CCSpriteFrame*>(30);
 --> CCArray* animFrames = CCArray::createWithCapacity(30);
const char* fullpath = cocos2d::CCFileUtils::fullPathFromRelativePath(patha.c_str());
 --> const char* fullpath = cocos2d::CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(patha.c_str());
CCSprite *sp = CCSprite::spriteWithFile("himi.png")
 --> CCSprite *sp = CCSprite::create("himi.png");
CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate(this, 0, false);
 --> CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, false);
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));
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)
1.x粒子创建和设置自动释放设置
CCParticleSystem *tempSystem =  CCParticleSystem::particleWithFile("himi.plist");
        tempSystem->setIsAutoRemoveOnFinish(true);
        
2.x粒子创建和设置自动释放设置       
CCParticleSystem *tempSystem =  CCParticleSystemQuad::create("himi.plist");
        tempSystem->setAutoRemoveOnFinish(true);
        
ccTime
--> float

===========================以上是从网上搜的============================

===========================以下是我自己整理的==========================

CGFloat
-->float

setIsEnabled
-->setEnabled

setIsTouchEnabled
-->setTouchEnabled

setIsKeypadEnabled
-->setKeypadEnabled

SCENE_NODE_FUNC(World);  
 --> CREATE_FUNC(World);

CCLayerColor::layerWithColorWidthHeight
-->CCLayerColor::create

CCSprite::spriteWithSpriteFrameName

-->CCSprite::createWithSpriteFrameName

CCRenderTexture::renderTextureWithWidthAndHeight
-->CCRenderTexture::create
 

CCLabelTTF::labelWithString

-->CCLabelTTF::create

CCMenuItemImage::itemFromNormalImage
-->CCMenuItemImage::create

CCMenu::menuWithItem
-->CCMenu::create

CCMenu::menuWithItems
-->CCMenu::create

CCMenuItemLabel::itemWithLabel
-->CCMenuItemLabel::create

CCArray::array()
-->CCArray::create()

CCArray::arrayWithCapacity(n);
-->CCArray::createWithCapacity(n);

CCRect::CCRectContainsPoint(r, p);
-->r.containsPoint(p);

CCPoint touchPoint = touch->locationInView( touch->view() );touchPoint = CCDirector::sharedDirector()->convertToGL( touchPoint );

-->CCPoint touchPoint = touch->getLocation();


CCSequence::actionOneTwo

-->CCSequence::createWithTwoActions


======================OpenGL================================


ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position | kCCVertexAttribFlag_Color );

glBindTexture(GL_TEXTURE_2D, (GLuint)texture);
-->ccGLBindTexture2D(texture->getName());

glBlendFunc
-->ccGLBlendFunc

glVertexPointer(2, GL_FLOAT, 0, vertices);
-->glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, vertices);

glColorPointer(4, GL_UNSIGNED_BYTE, 0, colors);
-->glVertexAttribPointer(kCCVertexAttrib_Color, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, (void*)(colors));

GL_POINT_SMOOTH
-->去掉glEnable(GL_POINT_SMOOTH);

glPointSize(pointSize);
-->ccPointSize(pointSize);

glColor4ub(color.r, color.g, color.b, color.a);

-->ccDrawColor4B(color.r, color.g, color.b, color.a);


CC_INCREMENT_GL_DRAWS(1);


======================割======================================

所有带CC_DEPRECATED_ATTRIBUTE标记的函数,都是废弃函数,看函数声明的注释找对应新函数名,这部分编译时会以警告形式出现。

======================割======================================

setTexParameters

这个函数限制的使用图形的宽高必须是2的n次幂或者wrap参数必须是GL_CLAMP_TO_EDGE(看源代码就知道了)

======================割======================================



create-android-project.bat

1.生成Android工程的批处理修改_CYGBIN_ANDROIDTOOLS_NDKROOT对应到自己的SDK目录

2.for /f "delims=" %%A in ('%_CYGBIN%\cygpath.exe "%~dpn0.sh"') do set _CYGSCRIPT=%%A前后添加pushd %COCOS2DX_HOME%popd可注掉批处理检测代码(

:: Check if it was run under cocos2d-x root
::if not exist "%cd%\create-android-project.bat" echo Error!!! You should run it under cocos2dx root & pause & exit 2    

echo "%COCOS2DX_HOME%"

rem chaeck create-android-project.sh
:: if not exist "%~dpn0.sh" echo Script "%~dpn0.sh" not found & pause & exit 3

)COCOS2DX_HOME为写在系统环境变量里的cocos2d-x根目录

======================割======================================

%COCOS2DX_HOME%\template\android

1.build_native.sh

# paths之后添加变量(分别对应自己的sdk目录):
NDK_ROOT=/cygdrive/F/SDK/android-ndk
COCOS2DX_ROOT=/cygdrive/F/SDK/cocos2d-x

注释掉:#COCOS2DX_ROOT="$DIR/../.."

修改这个的目的是可以在cocos2d-x根目录外进行编译了。

2.copy_files.sh

    2.0.3版本的这个文件有问题,缺少copy_library_src(){
    cp -rf $COCOSJAVALIB_ROOT/src/* $APP_DIR/proj.android/src/
}

    根据以前版本加上copy_library_src就行了


==================================================================

编译

mac下

先用

chmod 755 ./build_native.sh

提升权限

然后执行./build_native.sh