Cocos2d-x学习笔记(19)(TestCpp源代码分析-3)

本章主要介绍testBasic.h/cpp,这两个文件主要用于返回主场景界面。

//testBasic.h
#ifndef _TEST_BASIC_H_
#define _TEST_BASIC_H_

#include "cocos2d.h"
#include "VisibleRect.h"

USING_NS_CC;
using namespace std;

class TestScene : public CCScene  //继承自CCScene
{
public: 
    TestScene(bool bPortrait = false);  //构造函数
    virtual void onEnter();  //重写onEnter函数

    virtual void runThisTest() = 0;  //重写runThisTest函数

    // The CallBack for back to the main menu scene
    virtual void MainMenuCallback(CCObject* pSender);  //返回主菜单函数
};

typedef CCLayer* (*NEWTESTFUNC)();  //NEWTESTFUNC表示一个返回类型为CCLayer*。没有參数的函数指针
#define TESTLAYER_CREATE_FUNC(className) \  //TESTLAYER_CREATE_FUNC表示一个创建CCLayer的宏函数
static CCLayer* create##className() \  //用两个##来在宏中替换className为宏定义的參数
{ return new className(); }

#define CF(className) create##className  //CF表示 createXXX;比方CF(Scene)表示:createScene

#endif
下面是testBasic.cpp文件介绍

//testBasic.cpp
#include "testBasic.h"
#include "controller.h"

TestScene::TestScene(bool bPortrait)  //构造函数
{
    
    CCScene::init();  //调用基类的初始化函数
}

void TestScene::onEnter()
{
    CCScene::onEnter();  //调用基类的onEnter函数

    //add the menu item for back to main menu
//#if (CC_TARGET_PLATFORM == CC_PLATFORM_MARMALADE)
//    CCLabelBMFont* label = CCLabelBMFont::create("MainMenu",  "fonts/arial16.fnt");
//#else
    CCLabelTTF* label = CCLabelTTF::create("MainMenu", "Arial", 20);
//#endif  //将label与返回函数MainMenuCallback相关联
    CCMenuItemLabel* pMenuItem = CCMenuItemLabel::create(label, this, menu_selector(TestScene::MainMenuCallback));

    CCMenu* pMenu =CCMenu::create(pMenuItem, NULL);  ///用pMenuItem初始化CCMenu对象

    pMenu->setPosition( CCPointZero );  //设置pMenu位置
    pMenuItem->setPosition( ccp( VisibleRect::right().x - 50, VisibleRect::bottom().y + 25) ); //设置返回按键位置

    addChild(pMenu, 1);
}

void TestScene::MainMenuCallback(CCObject* pSender)  //返回主菜单函数
{
    CCScene* pScene = CCScene::create();  //创建新场景
    CCLayer* pLayer = new TestController();  //创建TestController层
    pLayer->autorelease();

    pScene->addChild(pLayer);  //将新层增加到新场景
    CCDirector::sharedDirector()->replaceScene(pScene);  //切换场景
}

posted @ 2015-12-22 15:52  phlsheji  阅读(243)  评论(0编辑  收藏  举报