加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

cocos2dx 3.1 倒计时实现

发布时间:2020-12-14 19:21:50 所属栏目:百科 来源:网络整理
导读:cocos2dx 3.1 倒计时实现 分享给大家 直接上代码:头文件 #ifndef _GAME_TIMER_H_#define _GAME_TIMER_H_#include "cocos2d.h"USING_NS_CC;class GameTimer : public cocos2d::Node{public:GameTimer();virtual ~GameTimer();static GameTimer* createTimer(

cocos2dx 3.1 倒计时实现 分享给大家

直接上代码:头文件

#ifndef _GAME_TIMER_H_
#define _GAME_TIMER_H_

#include "cocos2d.h"

USING_NS_CC;

class GameTimer : public cocos2d::Node
{
public:
	GameTimer();

	virtual ~GameTimer();

	static GameTimer* createTimer(float time);

	void update(float delta);

	bool init(float time);

private:
	LabelTTF*				label;
	float					pTime;
};

#endif

cpp文件:
#include "GameTimer.h"

GameTimer::GameTimer()
{

}

GameTimer::~GameTimer()
{

}

bool GameTimer::init(float time)
{
	pTime = time;

	label = LabelTTF::create();
	label->setPosition(0,0);
	
	this->addChild(label);

	schedule(schedule_selector(GameTimer::update));

	return true;
}
void GameTimer::update(float delta)
{
	pTime -= delta;
	char* mtime = new char[10];
	//此处只是显示分钟数和秒数  自己可以定义输出时间格式
	sprintf(mtime,"%d : %d",(int)pTime/60,(int)pTime%60);
	label->setString(mtime);
}

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;
}

使用:

GameTimer * m_timer = GameTimer::createTimer(3000);
	m_timer->setPosition(100,200);
	this->addChild(m_timer);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读