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

制作时钟cocos3.0

发布时间:2020-12-14 17:06:13 所属栏目:百科 来源:网络整理
导读: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 cocos

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

(编辑:李大同)

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

    推荐文章
      热点阅读