cocos2d-x 3.0 示例代码分析3:BaseTest
来源:互联网 发布:mysql和oracle的关系 编辑:程序博客网 时间:2024/05/16 09:45
在上一篇中(链接: 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,三个按钮:上一个,重新开始,下一个。但是每个界面的功能又不一样。这样,做一个公共的基类出来,每个界面重载这些函数,就可以达到满足了所有需求,又减少了相同代码,并且维护起来更加容易。
0 0
- cocos2d-x 3.0 示例代码分析3:BaseTest
- BaseTest
- Cocos2d-x示例代码学习(1)
- Cocos2d-x基础示例 精灵移动3
- cocos2d-x box2d 示例
- Cocos2d-x使用Javascript开发js绑定C++<代码示例>
- Quick-Cocos2D-X 并成功运行示例代码
- Quick-Cocos2D-X 并成功运行示例代码
- Cocos2d 3.x 代码示例1(个人理解)
- Cocos2d 3.x 代码示例2(个人理解TMX)
- Quick-Cocos2D-X 并成功运行示例代码
- Quick-Cocos2D-X学习(一)下载Quick-Cocos2D-X 并成功运行示例代码
- cocos2d-x v3.2 FlappyBird 各个类对象具体代码分析(3)
- cocos2d-x之TestCpp界面UI代码分析
- COCOS2D-X示例项目解读
- Cocos2d-X的一个示例
- cocos2d-x lua 示例Demo
- cocos2d-x 开发常用代码(3.0)
- MSP430应用技巧1:COFF ABI与EABI
- CSS学习笔记(2)
- 为什么浏览器采用多进程模型
- JavaScript基础教程
- 原码反码补码
- cocos2d-x 3.0 示例代码分析3:BaseTest
- ios 字节限制
- 合并排序(递归)
- Scala 深入浅出实战经典 第6讲:Map、Tuple、Zip实战解析
- Scala 深入浅出实战经典 第7讲:Scala类的属性和对象私有字段实战详解
- Scala 深入浅出实战经典\第8讲:Scala主构造器、私有构造器、构造器重载实战详解
- Scala 深入浅出实战经典 第9讲:Scala的内部类实战详解
- POJ 1061 青蛙绕地球约会-数论-(解一元一次同余方程+扩展欧几里得算法)
- [C++]学习札记2015-08-14