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);原文链接:https://www.f2er.com/cocos2dx/346349.html