上周,cocos2dx 版本3的已经正式发布了。
第三篇,我写到了,利用ecplise进行编译cocos2dx的项目。也列出了我遇到的几个常见问题。
如.so文件没有权限生成、org包没有导入、sdk版本没有对应上等问题。
但是这些都只是根据helloworld这个基本项目来的。我的意思是说,helloworld项目只有一个"场景"。(备注:cocos2dx的概念在后面再补充上。) 但是,我在学习到利用到"导演"进行切换场景时,在ecplise就遇到问题了。
功能需求:
在helloworld项目的基础上,点击"按钮",就出现一段字符串"xiaobei"。
目的: 学习切换场景
我Classes目录新建立了otherScene.cpp 以及otherScene.h作为新场景用。
在vs2012,点击Classes。右键
按照这样的方式建立了otherScene.cpp文件。
otherScene.h代码:
#include "cocos2d.h"
class otherScene:public cocos2d::CCLayerColor
{
public:
virtual bool init();
static cocos2d::CCScene* scene();
CREATE_FUNC(otherScene);
};
otherScene.cpp代码:
#include "otherScene.h"
USING_NS_CC;
//建立场景
CCScene* otherScene::scene()
{
CCScene *scene = CCScene::create();
otherScene *layer = otherScene::create();
scene->addChild(layer);
return scene;
}
//otherScene初始化方法
bool otherScene::init()
{
if ( !CCLayerColor::initWithColor(ccc4(0,0,0,255)) )
{
return false;
}
CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
CCLabelTTF* labelTTF = CCLabelTTF::create("xiaobei","arial",20);
labelTTF->setPosition(ccp(visibleSize.width/2,visibleSize.height/2));
this->addChild(labelTTF);
return true;
}
HelloWorldScene.cpp 关闭方法代码如下:
void HelloWorld::menuCloseCallback(CCObject* pSender)
{
CCScene* otherscene = otherScene::scene();
CCDirector::sharedDirector()->replaceScene(otherscene);
/*#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
#else
CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
#endif*/
}
运行结果:
到这里,切换场景的工作基本上完成了。最后,我把这个项目按照上篇文档的操作,导入ecplise中
但是在run的时候,报了这个错误:
由于这个错误,导致.so无法生成. 而这个问题的产生是由于安卓和c++的"中间组件",没有导入otherScene的信息。
在我们导入到ecplise的项目中,有个jni目录。其中jni叫java native interface(java原生接口),由于它的存在,才使得安卓可以与c++进行交互。
打开jni目录里面的Android.mk文件
补充完,之后,再次run
就可以看到效果了。