zoukankan      html  css  js  c++  java
  • 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);  //切换场景
    }
    

  • 相关阅读:
    页面内容[置顶] 采用Div+Css布局——牛腩
    区域函数[置顶] linux 3.4.10 内核内存管理源代码分析5:伙伴系统初始化
    安装应用android批量安装APK
    选择版本Win7系统VS2010下搭建qt开发环境
    字体格式The format of Oracle tnsnames.ora file
    程序执行vhdl中延时器的编写
    概率链接nbu 2416 奇怪的散步
    中国主题ASP.Net课堂实验4
    要求终点HDU1010:Tempter of the Bone
    解决方案编程苦B和二B程序员别忘了养生
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/5066921.html
Copyright © 2011-2022 走看看