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: 不能修改this中的任何值  modification 2014.11.14
    std::string BaseTest::title() const { return ""; } // 返回subtitle的字符串 // const: 不能修改this中的任何值  modification 2014.11.14
    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!"); }

    add 2014.11.14: 之前对const的解释有错误,星月在此想各位道歉~~~以后会多注意的(小白:你请吃饭么。。。)

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

     

    title : ActionTest

    subTitle : manul Transformation

    外加:三个按钮

    一个BaseTest的实现。。。

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

    不得不说继承的强大。。。感谢:继承。让我们少做好多事情。(小白:嗯嗯~。。。)

    星月最近用lua,用久了,快遗忘了c++了,刚好c++11特性也不是很了解,买了最新的c++ primer学习,一边复习c++同时学习c++11特性。给自己赞一个~~~(小白:赞~~~)

    时间允许的话,星月会把一些重要的知识点记录下来,然后跟大家分享。不要太期望,最近事情太多要做。。。(小白:我不期望。)

    这章到此结束,希望对大家有帮助。。。

    作者使用 cocos2d-x 3.0 示例代码分析,未经作者允许,请勿转载!在此谢谢各位手下留情~~~

    本文没有获得作者本人同意,不得转载,否则必追究相关责任。转载请注明出处!!~~

    原文地址:http://www.cnblogs.com/wodehao0808/p/4087607.html

  • 相关阅读:
    [zz]redhat6.0无法识别ntfs分区的解决方法
    使用ftp搭建yum源问题解决
    [zz]搭建centos6.0本地yum源(32位)
    JAVA传统线程技术
    JAVA判断字符串是否为数字
    java之异常
    随便记两笔Java中的反射
    【转】单例模式完全解析
    java.lang.Enum
    文件搜索
  • 原文地址:https://www.cnblogs.com/wodehao0808/p/4087607.html
Copyright © 2011-2022 走看看