cocos2d-x 3.0 示例代码分析3:BaseTest
发布时间:2020-12-14 16:24:38 所属栏目:百科 来源:网络整理
导读:在上一篇中(链接:http://www.cnblogs.com/wodehao0808/p/4045327.html),有这样一段代码: #include " BaseTest.h " // 基类,提供场景一些公共功能,详情请查看该类的说明文章。(小白:你出了吗?星月:还没...) 这是一个基类,今天我们就来看看这个基
在上一篇中(链接: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,51); font-weight:bold">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 string subtitle() const; 标题2 virtual void restartCallback(Ref* sender); 重新开始按钮回调 void nextCallback(Ref* sender); 下一个按钮回调 void backCallback(Ref* sender); 上一个按钮回调 void onEnter() override; 重载函数:onEnter() void onExit() override; 重载函数:onExit() }; #endif /* defined(__TestCpp__BaseTest__) */ BaseTest.cpp 星月倾心贡献ing... /VisibleRect.h" 辅助类VisibleRect类得到获取视口 testResource.hAppDelegate.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 = addChild(l,0)">); l->setPosition( Point(VisibleRect::center().x,153)">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,) ); 创建:重新开始按钮 auto item2 = MenuItemImage::create(s_pathR1,s_pathR2,CC_CALLBACK_1(BaseTest::restartCallback,0)"> 创建:下一个按钮 auto item3 = MenuItemImage::create(s_pathF1,s_pathF2,CC_CALLBACK_1(BaseTest::nextCallback,0)"> 生成菜单 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/)); item3->setPosition(Point(VisibleRect::center().x + item2->getContentSize().width*2,0)">)); addChild(menu,0)">); } 退出时调用 BaseTest::onExit() { 获得委托 AppDelegate* app = (AppDelegate *)Application::getInstance(); 设置现在正在运行的测试例:空指针 app->setCurrentTest(nullptr); Layer::onExit(); } 返回title的字符串 const: 外界不能修改返回值 std::string BaseTest::title() const { return ""; } 返回subtitle的字符串 string BaseTest::subtitle() 重新开始按钮回调 void BaseTest::restartCallback(Ref* sender) { log(override restart!"); } 下一个按钮回调 void BaseTest::nextCallback(Ref*override next! 上一个按钮回调 void BaseTest::backCallback(Ref*override back!); } 实现是比较简单的,我们看下这些到底是什么。 |
相关内容
- 从Xcode运行UIAutomation脚本
- 使用 SQLiteDatabase 操作 SQLite 数据库
- c# – FileHelpers:非引用CSV中的可选字段
- c# – 当值> 127时关闭BinaryWriter.Write(字节值)
- ruby – rspec stub允许传递[hash_key]
- Cocos2d-x使用CCGLProgram和Shader文件实现精灵置灰
- 在vue项目中引入highcharts图表的方法(详解)
- ruby-on-rails – 为什么必须使用friendly_id显式指定范围?
- ruby – 有没有办法轻松链接RDoc中的文件?
- Cocos2d-x 3.0 主线程