制作如图所示的电子时钟,显示当前的时间。
代码如下
//在初始化函数里创建一个Label,用于显示时间 m_ttpClock = CCLabelTTF::create("","Arial",18); m_ttpClock->setPosition(DZConfig::sharedConfig()->getPoint("config_game","timepiece","pos")); m_ttpClock->setColor(ccc3(0xe2,0xe0,0xdc)); m_ttpClock->setAnchorPoint(ccp(0.5,0.5)); addChild(m_ttpClock,kZOrderClock); m_ttpClock->setVisible(true); schedule(schedule_selector(TableView::onTimerClock)); //时时刷新 void TableView::onTimerClock(float dt) { //获取当前时间 struct tm *tm; time_t timevp ;//= now.tv_sec; time(&timevp); tm = localtime(&timevp); int hour = tm->tm_hour; int minutes = tm->tm_min; int second = tm->tm_sec; CCString *strTime = CCString::createWithFormat("%02d:%02d",hour,minutes,second); m_ttpClock->setString(strTime->getCString()); }原文链接:https://www.f2er.com/cocos2dx/340604.html