上一篇解释了如何编译成功HelloCpp,现在说一下场景切换,如何使用按钮,进行场景切换!!!(代码量没多少,书上有一步也没有说明,害死人啊,一下午就这么没了!!!)笔者想练习下场景切换。现在说下,GameScene.h文件GameScene.cpp文件当中其代码如下:

GameScene.h文件如下:

#ifndef __GAMASCENE__H
#define __GAMASCENE__H

#include "cocos2d.h"
class CGameScene : public cocos2d::CCLayer
{
public:
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();

// there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::CCScene* scene();

// a selector callback
// void menuCloseCallback(CCObject* pSender);

// implement the "static node()" method manually
CREATE_FUNC(CGameScene);
};

#endif // __HELLOWORLD_SCENE_H__

GameScene.cpp文件如下:

#include "GameScene.h"
#include "AppMacros.h"
USING_NS_CC;

CCScene* CGameScene::scene()
{

// 'scene' is an autorelease object
CCScene *scene = CCScene::create();

// 'layer' is an autorelease object
CGameScene *layer = CGameScene::create();

// add layer as a child to scene
scene->addChild(layer);

// return the scene
return scene;
}

bool CGameScene::init()
{
if(!CCLayer::init())
{
return false;
}

CCSize visibleSize=CCDirector::sharedDirector()->getVisibleSize();
CCPoint origin=CCDirector::sharedDirector()->getVisibleOrigin();
CCLabelTTF*pLabel=CCLabelTTF::create("周建业的游戏","Arial",TITLE_FONT_SIZE);
pLabel->setPosition(ccp(origin.x+visibleSize.width/2,origin.y+visibleSize.height-pLabel->getContentSize().height));
this->addChild(pLabel,1);
CCSprite* pSprite=::CCSprite::create("new.jpg");
pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));

// add the sprite as a child to this layer
this->addChild(pSprite, 0);
return true;
}

这个代码经过笔者无数次检测都没有问题,但是一直出现下述错误

D:/Android/android-ndk-r9d-windows-x86/android-ndk-r9d/toolchains/arm-linux-androideabi-4.6/prebuilt/windows/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld.exe: ./obj/local/armeabi/objs/hellocpp_shared/__/__/Classes/HelloWorldScene.o: in function HelloWorld::menuCloseCallback(cocos2d::CCObject*):jni/../../Classes/HelloWorldScene.cpp:100: error: undefined reference to 'CGameScene::scene()'
collect2: ld returned 1 exit status
make.exe: *** [obj/local/armeabi/libhellocpp.so] Error 1

原来到最后还需要修改Android.mk文件,该文件中有了编译选项,在里面的

LOCAL_SRCS中添加我们自行添加的GameScene.cpp文件

LOCAL_SRC_FILES := hellocpp/main.cpp \
                   ../../Classes/AppDelegate.cpp \
                   ../../Classes/HelloWorldScene.cpp \
                   ../../Classes/GameScene.cpp \