制作时钟cocos3.0

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


posted @ 2016-06-27 01:13  Anzhongliu  阅读(220)  评论(0编辑  收藏  举报