Cocos2d-x游戏开发之计时器
首先写一个计时器的头文件GameTimer.h:
#ifndef _GAME_TIMER_H_ #define _GAME_TIMER_H_
#include "cocos2d.h"
class GameTimer : public cocos2d::Node { public: GameTimer();
virtual ~GameTimer();
static GameTimer* createTimer(float time);
void update(float delta);
bool init(float time);
void stop();
float getTime();
private: cocos2d::LabelTTF* label; float pTime; };
#endif
GameTimer.cpp如下:
#include "GameTimer.h"
USING_NS_CC;
GameTimer::GameTimer() {
}
GameTimer::~GameTimer() {
}
bool GameTimer::init(float time) {
pTime = time;
label = LabelTTF::create();
label->setPosition(ccp(130, 610));
this->addChild(label);
schedule(schedule_selector(GameTimer::update));
return true; }
void GameTimer::stop() {
this->unschedule(schedule_selector(GameTimer::update));
}
float GameTimer::getTime() {
return pTime;
}
void GameTimer::update(float delta) {
pTime -= delta;
char* mtime = new char[10];
//此处只是显示分钟数和秒数 自己可以定义输出时间格式
sprintf(mtime, "%02d : %02d", (int)pTime / 60, (int)pTime % 60);
label->setString(mtime);
label->setFontSize(30);
label->setColor(Color3B(0, 0, 0));
}
GameTimer* GameTimer::createTimer(float time) {
GameTimer* gametimer = new GameTimer;
if (gametimer && gametimer->init(time)) {
gametimer->autorelease(); return gametimer;
}
else {
delete gametimer; gametimer = NULL;
}
return NULL;
}
在MyGame.h中声明:
头文件#include "GameTimer.h"
函数void Updatetime(float t);//该函数用于让计时器停止计时
MyGame.cpp:
#include "MyGame.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"
#include "cocos2d.h"
#include "GameTimer.h"
#include<iostream>
#include<stdio.h>
using namespace std;
USING_NS_CC;
using namespace cocos2d;
using namespace cocostudio::timeline;
Scene* MyGame::createScene() {
// 'scene' is an autorelease object
auto scene = Scene::create();
// 'layer' is an autorelease object
auto layer = MyGame::create();
// add layer as a child to scene scene->addChild(layer);
// return the scene return scene;
}
bool MyGame::init()
{
//////////////////////////////
// 1. super init first
if (!Layer::init())
{
return false;
}
//创建计时器
m_timeCounter = GameTimer::createTimer(61); this->addChild(m_timeCounter);
//开始计时
m_timeCounter->init(61);
this->schedule(schedule_selector(MyGame::Updatetime));
// 创建背景图片
auto dirt = Sprite::create("scene.png");
dirt->setScaleX(0.75);
dirt->setScaleY(0.803);
dirt->setAnchorPoint(Vec2::ZERO);
dirt->setPosition(0, 0);
this->addChild(dirt, -2);
.....................
return true;
}
//Updatetime函数
void MyGame::Updatetime(float t)//超过60秒,让时间暂停
{
if (m_timeCounter->getTime() <= 0) m_timeCounter->stop();
}
运行结果如下:
Cocos2d-x游戏开发之计时器的更多相关文章
- iOS cocos2d 2游戏开发实战(第3版)书评
2013是游戏爆发的一年,手游用户也是飞速暴增.虽然自己不做游戏,但也是时刻了解手机应用开发的新动向.看到CSDN的"写书评得技术图书赢下载分"活动,就申请了一本<iOS c ...
- (转载)如何学好iphone游戏开发
转自:http://www.cnblogs.com/zilongshanren/archive/2011/09/19/2181558.html 自从发布<如何学习iphone游戏开发>到 ...
- cocos2d 游戏开发实战
文章转自:http://uliweb.clkg.org/tutorial/read/40 6 cocos2d 游戏开发实战 6.1 创建cocos2d项目 6.2 cocos2d v3 & ...
- 【Cocos2D研究院之游戏开发】
http://www.xuanyusong.com/archives/category/ios/cocos2d_game 分类目录归档:[Cocos2D研究院之游戏开发] 201211-19 Co ...
- 2、Cocos2dx 3.0游戏开发找小三之引擎简单介绍
尊重开发人员的劳动成果,转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/27094663 引擎简单介绍 Cocos2d-x 的 ...
- HTML5游戏开发进阶指南(亚马逊5星畅销书,教你用HTML5和JavaScript构建游戏!)
HTML5游戏开发进阶指南(亚马逊星畅销书,教你用HTML5和JavaScript构建游戏!) [印]香卡(Shankar,A.R.)著 谢光磊译 ISBN 978-7-121-21226-0 201 ...
- MVC模式在游戏开发的应用
原地址: http://www.cocoachina.com/gamedev/2012/1129/5212.html MVC是三个单词的缩写,分别为:模型(Model).视图(View)和控制Cont ...
- 从一点儿不会开始——Unity3D游戏开发学习(一)
一些废话 我是一个windows phone.windows 8的忠实粉丝,也是一个开发者,开发数个windows phone应用和两个windows 8应用.对开发游戏一直抱有强烈兴趣和愿望,但奈何 ...
- 学习手机游戏开发的两个方向 Cocos2d-x 和 Unity 3D/2D,哪个前景更好?
如题! 首先说一说学习手机游戏(移动游戏)这件事. 眼下移动互联网行业的在以井喷状态发展.全球几十亿人都持有智能终端设备(ios android),造就了非常多移动互联网创业机会: 一.移动社交 微信 ...
随机推荐
- nsenter into docker. selinux(semanage,restorecon)
Docker容器运行后,如何进入容器进行操作呢?起初我是用SSH.如果只启动一个容器,用SSH还能应付,只需要将容器的22端口映射到本机的一个端口即可.当我启动了五个容器后,每个容器默认是没有配置SS ...
- 【转】android程序编译过程
现在很多人想对Android工程的编译和打包进行自动化,比如建立每日构建系统.自动生成发布文件等等.这些都需要我们对Android工程的编译和打包有一个深入的理解,至少要知道它的每一步都做了什么,需要 ...
- 在Windows 环境下编译Qt静态库(QT5.32)
参考链接 Qt5.3 Tools and Versions MinGW ICU ActivePerl Qt 安装MinGW工具链环境 这里在Win32环境下要安装一个MinGW工具链,这里最好是先安装 ...
- Solr分页与高亮(使用SolrNet实现)
Solr分页与高亮(使用SolrNet实现) 本节我们使用Asp.net MVC实现Solr客户端查询,建议使用SolrNet这个客户端,开源地址在:https://github.com/mausch ...
- 用CSS为表格添加边框
格式: <style type="text/css"> table tr td,th {border:1px solid #000;} </style>
- Android笔记:Socket通讯常见问题
经验证的socket通讯问题 1.如果是模拟器和本机PC直接通讯,需要使用本机IP地址 而不是 10.0.2.2 如本机的静态地址为192.168.1.2 则直接使用该地址 2.接收和连接代码不能在 ...
- leap motion
体感控制器: 识别:手,手指和工具,获取位置,手势,动作 范围:倒金字塔,塔尖在设备中心,2.5cm~0.6米 坐标系统:采用右手笛卡尔积坐标系,返回的数值:毫米 摆放:绿灯朝向自己,z轴距离屏幕越来 ...
- JS之iframe中的窗口
1.window.self 对当前窗口自身的引用;self,window.self,window三者是等价的 2.window.top 对顶层窗口的引用,如果本身就是顶层窗口,则返回本身 3.wind ...
- MFC之常用控件(四)
常用控件主要包括:静态文本框.编辑框.单选按钮.复选框.分组框.列表框.组合框.图片控件.列表控件.树形控件和进度条控件等等.本节教程先来讲解静态文本框的使用. 控件的通知消息 在将静态文本框的使用之 ...
- Proxy settings in TortoiseSVN and command line svn client
The server file is created when you install TortoiseSVN, Eclipse or command-line Subversion. Use the ...