制作时钟cocos3.0
2016-06-27 01:13
330 查看
clockBackgroundScene.h头文件:
#ifndef __Clock_1__clockBackgroundScene__
#define __Clock_1__clockBackgroundScene__
#include <iostream>
#include "cocos2d.h"
USING_NS_CC;
class clockBackground: public Layer
{
public:
virtual bool init();
static cocos2d::Scene * scene();
CREATE_FUNC(clockBackground);
void MutUpdate(float dt);
public:
Sprite * m_hour;
Sprite * m_minute;
Sprite * m_second;
int mRotation;
int sRotation;
int hRotation;
private:
int nHour;
int nMinute;
int nSecond;
};
#endif
clockbackgroundScene.cpp文件:
#include "clockBackgroundScene.h"
Scene * clockBackground::scene(){
Scene * scene = Scene::create();
clockBackground * layer = clockBackground::create();
scene->addChild(layer);
return scene;
}
bool clockBackground::init(){
Size winSize = Director::getInstance()->getVisibleSize();
Sprite * clockBk = Sprite::create("background.jpg");
clockBk->setPosition(ccp(winSize.width / 2, winSize.height / 2));
clockBk->setScale(0.5f);
this->addChild(clockBk);
m_minute = Sprite::create("fen.png");
m_minute->setAnchorPoint(ccp(0, 0.5));
m_minute->setScale(0.2f);
m_minute->setRotation(-90);
m_minute->setPosition(ccp(winSize.width / 2, winSize.height / 2));
this->addChild(m_minute);
m_second = Sprite::create("miao.png");
m_second->setAnchorPoint(ccp(0, 0.5));
m_second->setScale(0.2f);
m_second->setRotation(-90);
m_second->setPosition(ccp(winSize.width / 2, winSize.height / 2));
this->addChild(m_second);
m_hour = Sprite::create("shi.png");
m_hour->setAnchorPoint(ccp(0, 0.5));
m_hour->setScale(0.2f);
m_hour->setRotation(-90);
m_hour->setPosition(ccp(winSize.width / 2, winSize.height / 2));
this->addChild(m_hour);
///////////////获取系统当前时间
struct tm *tm;
time_t timep;
time(&timep);
tm = localtime(&timep);
//int year = tm->tm_year + 1900;
//int month = tm->tm_mon + 1;
//int day = tm->tm_mday;
nHour = tm->tm_hour;
nMinute = tm->tm_min;
nSecond = tm->tm_sec;
log("%d-%d-%d", nHour, nMinute, nSecond);
/////////////
mRotation = nMinute * 5 - 90;//nMinute;
sRotation = nSecond * 5 - 90;// nSecond;
hRotation = -nHour - 90;// nHour + 80;
this->schedule(schedule_selector(clockBackground::MutUpdate), 1);
return true;
}
void clockBackground::MutUpdate(float dt){
//static int mRotation = nMinute;//*6*/
//static int sRotation = nSecond;//static 静态变量
//static int hRotation=nHour;
if (nHour > 12){
//hRotatio = (nHour - 12) * 5 * 6 + (mRotation / 72) * 6;
}
else{
//hRotatio = (nHour)* 5 * 6 + (mRotation / 72) * 6;
}
m_second->setRotation(sRotation);
//m_second->runAction(RotateBy::create(sRotation,360));
m_minute->setRotation(mRotation);
m_hour->setRotation(hRotation);
//log("%d", sRotation);
if (sRotation >= 360){
sRotation = 0;
mRotation += 6;
m_minute->setRotation(mRotation);
if (mRotation % 72 == 0){//mRotation % 72 == 0
hRotation += 6;//6
m_hour->setRotation(hRotation);
if (mRotation >= 360){
mRotation = 0;
}
}
}
sRotation += 6;
//hRotation += 6;
}
#ifndef __Clock_1__clockBackgroundScene__
#define __Clock_1__clockBackgroundScene__
#include <iostream>
#include "cocos2d.h"
USING_NS_CC;
class clockBackground: public Layer
{
public:
virtual bool init();
static cocos2d::Scene * scene();
CREATE_FUNC(clockBackground);
void MutUpdate(float dt);
public:
Sprite * m_hour;
Sprite * m_minute;
Sprite * m_second;
int mRotation;
int sRotation;
int hRotation;
private:
int nHour;
int nMinute;
int nSecond;
};
#endif
clockbackgroundScene.cpp文件:
#include "clockBackgroundScene.h"
Scene * clockBackground::scene(){
Scene * scene = Scene::create();
clockBackground * layer = clockBackground::create();
scene->addChild(layer);
return scene;
}
bool clockBackground::init(){
Size winSize = Director::getInstance()->getVisibleSize();
Sprite * clockBk = Sprite::create("background.jpg");
clockBk->setPosition(ccp(winSize.width / 2, winSize.height / 2));
clockBk->setScale(0.5f);
this->addChild(clockBk);
m_minute = Sprite::create("fen.png");
m_minute->setAnchorPoint(ccp(0, 0.5));
m_minute->setScale(0.2f);
m_minute->setRotation(-90);
m_minute->setPosition(ccp(winSize.width / 2, winSize.height / 2));
this->addChild(m_minute);
m_second = Sprite::create("miao.png");
m_second->setAnchorPoint(ccp(0, 0.5));
m_second->setScale(0.2f);
m_second->setRotation(-90);
m_second->setPosition(ccp(winSize.width / 2, winSize.height / 2));
this->addChild(m_second);
m_hour = Sprite::create("shi.png");
m_hour->setAnchorPoint(ccp(0, 0.5));
m_hour->setScale(0.2f);
m_hour->setRotation(-90);
m_hour->setPosition(ccp(winSize.width / 2, winSize.height / 2));
this->addChild(m_hour);
///////////////获取系统当前时间
struct tm *tm;
time_t timep;
time(&timep);
tm = localtime(&timep);
//int year = tm->tm_year + 1900;
//int month = tm->tm_mon + 1;
//int day = tm->tm_mday;
nHour = tm->tm_hour;
nMinute = tm->tm_min;
nSecond = tm->tm_sec;
log("%d-%d-%d", nHour, nMinute, nSecond);
/////////////
mRotation = nMinute * 5 - 90;//nMinute;
sRotation = nSecond * 5 - 90;// nSecond;
hRotation = -nHour - 90;// nHour + 80;
this->schedule(schedule_selector(clockBackground::MutUpdate), 1);
return true;
}
void clockBackground::MutUpdate(float dt){
//static int mRotation = nMinute;//*6*/
//static int sRotation = nSecond;//static 静态变量
//static int hRotation=nHour;
if (nHour > 12){
//hRotatio = (nHour - 12) * 5 * 6 + (mRotation / 72) * 6;
}
else{
//hRotatio = (nHour)* 5 * 6 + (mRotation / 72) * 6;
}
m_second->setRotation(sRotation);
//m_second->runAction(RotateBy::create(sRotation,360));
m_minute->setRotation(mRotation);
m_hour->setRotation(hRotation);
//log("%d", sRotation);
if (sRotation >= 360){
sRotation = 0;
mRotation += 6;
m_minute->setRotation(mRotation);
if (mRotation % 72 == 0){//mRotation % 72 == 0
hRotation += 6;//6
m_hour->setRotation(hRotation);
if (mRotation >= 360){
mRotation = 0;
}
}
}
sRotation += 6;
//hRotation += 6;
}
相关文章推荐
- cocos2dx int to string的几种方法
- cocos2dx-详细剖析lua(如何与lua集成,如何导出lua api,如何与lua交互)
- cocos2d-x位图字体生成工具bmfont使用图文教程 美术字使用
- 如何在Cocos2d-x中集成安卓版微信SDK
- VS2012运行最新版本的Cocos3.10出现的Bug
- cocos lua -字符串匹配规则。
- cocos2dx-内存管理剖析(智能指针的局限与引用计数的选择)
- CVP认证学习笔记--李天宇010节点的计划任务
- CVP认证学习笔记--李天宇009节点的缩放和旋转
- CVP认证学习笔记--李天宇007节点的锚点和坐标
- CVP认证学习笔记--李天宇006场景切换特效
- CVP认证学习笔记--李天宇005实现多场景切换
- CVP认证学习笔记--李天宇004实现自定义场景
- CVP认证学习笔记--李天宇003实现文本菜单层
- CVP认证学习笔记--李天宇002添加图片层
- CVP认证学习笔记--李天宇001添加文字层
- 7------cocos2dx 3.1.1 在线热更新 自动更新(AssetsManager)
- 6------cocos2dx-------3.3<LUA>官网英文版
- 5------Cocos2dx-- 资源热更新(lua)
- cocos2dx-3.10 开发初探(一)