zoukankan      html  css  js  c++  java
  • cocos2d-x 3.0 示例代码分析3:BaseTest

    在上一篇中(链接: http://www.cnblogs.com/wodehao0808/p/4045327.html),有这样一段代码:

    #include "BaseTest.h"    // 基类,提供场景一些公共功能,详情请查看该类的说明文章。(小白:你出了吗?星月:还没...)

    这是一个基类,今天我们就来看看这个基类的实现了。大家鸡冻了吗。。。(小白:好鸡冻。。。)

    // BaseTest.h
    // 星月倾心贡献~~~
    
    /****************************************************************************
     Copyright (c) 2013-2014 Chukong Technologies Inc.
    
     http://www.cocos2d-x.org
    
     Permission is hereby granted, free of charge, to any person obtaining a copy
     of this software and associated documentation files (the "Software"), to deal
     in the Software without restriction, including without limitation the rights
     to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     copies of the Software, and to permit persons to whom the Software is
     furnished to do so, subject to the following conditions:
    
     The above copyright notice and this permission notice shall be included in
     all copies or substantial portions of the Software.
    
     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
     THE SOFTWARE.
     ****************************************************************************/
    
    // 基类,提供场景一些公共功能
    
    #ifndef __TestCpp__BaseTest__
    #define __TestCpp__BaseTest__
    
    #include "cocos2d.h"
    
    class BaseTest : public cocos2d::Layer  // 继承layer,所以BaseTest也是一个layer
    {
    public:
        // 下面这些函数全是虚函数
    
        virtual std::string title() const;     // 标题1
        virtual std::string subtitle() const;  // 标题2
    
        virtual void restartCallback(Ref* sender);  // 重新开始按钮回调
        virtual void nextCallback(Ref* sender);     // 下一个按钮回调
        virtual void backCallback(Ref* sender);     // 上一个按钮回调
    
        virtual void onEnter() override;  // 重载函数:onEnter()
        virtual void onExit() override;   // 重载函数:onExit()
    };
    
    
    #endif /* defined(__TestCpp__BaseTest__) */
    // BaseTest.cpp 
    // 星月倾心贡献ing...
    
    /****************************************************************************
     Copyright (c) 2013-2014 Chukong Technologies Inc.
    
     http://www.cocos2d-x.org
    
     Permission is hereby granted, free of charge, to any person obtaining a copy
     of this software and associated documentation files (the "Software"), to deal
     in the Software without restriction, including without limitation the rights
     to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     copies of the Software, and to permit persons to whom the Software is
     furnished to do so, subject to the following conditions:
    
     The above copyright notice and this permission notice shall be included in
     all copies or substantial portions of the Software.
    
     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
     THE SOFTWARE.
     ****************************************************************************/
    
    #include "BaseTest.h"
    #include "VisibleRect.h"   // 辅助类VisibleRect类得到获取视口
    #include "testResource.h"
    #include "AppDelegate.h"
    
    USING_NS_CC;
    
    // 进入时调用
    void BaseTest::onEnter()  
    {
      // 调用父类的onEnter
      Layer::onEnter(); 
      // 获得委托
      AppDelegate* app = (AppDelegate *)Application::getInstance();
    
      // this:设置为现在正在运行的测试例
      app->setCurrentTest(this);
    
      // add title and subtitle
      std::string str = title();
      const char * pTitle = str.c_str();
    
      // 设置字体和大小
      TTFConfig ttfConfig("fonts/arial.ttf", 32); 
    
      auto label = Label::createWithTTF(ttfConfig,pTitle);
      addChild(label, 9999);
      label->setPosition( Point(VisibleRect::center().x, VisibleRect::top().y - 30) );
    
      std::string strSubtitle = subtitle();
      if( ! strSubtitle.empty() )
      {
        // 设置字体和大小
        ttfConfig.fontFilePath = "fonts/Thonburi.ttf";
        ttfConfig.fontSize = 16;
        auto l = Label::createWithTTF(ttfConfig,strSubtitle.c_str());
        addChild(l, 9999);
        l->setPosition( Point(VisibleRect::center().x, VisibleRect::top().y - 60) );
      }
    
      // add menu
      // CC_CALLBACK_1 == std::bind( function_ptr, instance, std::placeholders::_1, ...)
      // 创建:上一个按钮
      auto item1 = MenuItemImage::create(s_pathB1, s_pathB2, CC_CALLBACK_1(BaseTest::backCallback, this) );
      // 创建:重新开始按钮
      auto item2 = MenuItemImage::create(s_pathR1, s_pathR2, CC_CALLBACK_1(BaseTest::restartCallback, this) );
      // 创建:下一个按钮
      auto item3 = MenuItemImage::create(s_pathF1, s_pathF2, CC_CALLBACK_1(BaseTest::nextCallback, this) );
    
      // 生成菜单
      auto menu = Menu::create(item1, item2, item3, NULL);
    
      menu->setPosition(Point::ZERO);
      // 设置每个按钮位置
      // VisibleRect::center().x : 屏幕中心x坐标
      // VisibleRect::bottom().y : 屏幕底部y坐标
      item1->setPosition(Point(VisibleRect::center().x - item2->getContentSize().width*2, VisibleRect::bottom().y+item2->getContentSize().height/2));
      item2->setPosition(Point(VisibleRect::center().x, VisibleRect::bottom().y+item2->getContentSize().height/2));
      item3->setPosition(Point(VisibleRect::center().x + item2->getContentSize().width*2, VisibleRect::bottom().y+item2->getContentSize().height/2));
    
      addChild(menu, 9999);
    }
    
    // 退出时调用
    void BaseTest::onExit()
    {
      // 获得委托
      AppDelegate* app = (AppDelegate *)Application::getInstance();
      // 设置现在正在运行的测试例:空指针
      app->setCurrentTest(nullptr);
      Layer::onExit();
    }
    
    // 返回title的字符串
    // const: 外界不能修改返回值
    std::string BaseTest::title() const
    {
      return "";
    }
    
    // 返回subtitle的字符串
    // const: 外界不能修改返回值
    std::string BaseTest::subtitle() const
    {
      return "";
    }
    
    // 重新开始按钮回调
    void BaseTest::restartCallback(Ref* sender)
    {
      log("override restart!");
    }
    
    // 下一个按钮回调
    void BaseTest::nextCallback(Ref* sender)
    {
      log("override next!");
    }
    
    // 上一个按钮回调
    void BaseTest::backCallback(Ref* sender)
    {
      log("override back!");
    }

    实现是比较简单的,我们看下这些到底是什么。

    title : ActionTest

    subTitle : manul Transformation

    外加:三个按钮

    一个BaseTest的实现。。。

    在测试代码中,基本每个界面都有,标签lbl,三个按钮:上一个,重新开始,下一个。但是每个界面的功能又不一样。这样,做一个公共的基类出来,每个界面重载这些函数,就可以达到满足了所有需求,又减少了相同代码,并且维护起来更加容易。

  • 相关阅读:
    Hibernate在自由状态和持久的状态转变
    JS日期时间选择器
    LevelDB初体验
    java多线程样例
    PHP 的解压缩ZipArchive中的extractTo()方法 LINUX+nginx环境中解压zip时文件丢失的问题
    JavaScript--基于对象的脚本语言学习笔记(三)
    什么是消息队列?
    android 环境使用smack 必须注冊的组件
    AssetManager asset的使用
    键盘过滤驱动
  • 原文地址:https://www.cnblogs.com/Anzhongliu/p/6091969.html
Copyright © 2011-2022 走看看