标签:blog http io ar os sp for on div
http://blog.csdn.net/u012945598/article/details/16946283
总结:一个SCENE,三个LAYER代表三个指针,每个LAYER加载一个精灵(实际图片)
大多数情况都是background申明成一个layer,在background里面建立一个新的SCENE返回给主函数。
本人通过将background设置成scene,背景图片用一个精灵来表示,然后将精灵直接加到这个scene中。addchild的参数都是node,而layer和scene都是node的子类,因此加载到背景和场景都一回事。
同时,三个指针时、分、秒也可以作为精灵直接加载到scene中。
但是个人觉得几乎所有情况还是把精灵放在布景里面处理,布景多了一个触屏等动作处理函数。
auto* scene = clockBackgroundScene::create();
// run
director->runWithScene(scene);
create函数会自动调用init,因此只需要重写子类clockBackgroundScene的init即可
//
// clockBackgroundScene.h
// Clock_1
//
// Created by mzy1992 on 13-11-25.
//
//
#ifndef __Clock_1__clockBackgroundScene__
#define __Clock_1__clockBackgroundScene__
#include <iostream>
#include "cocos2d.h"
#include "hourHand.h"
#include "secondHand.h"
#include "minuteHand.h"
USING_NS_CC;
class clockBackgroundScene : public CCLayer
{
public:
virtual bool init();
static CCScene * scene(); //创建场景对象的方法
CREATE_FUNC(clockBackgroundScene);
void MutUpdate(float aa);//自定义的update函数
public:
hourHand * m_hour; //创建hourHand类的对象
minuteHand * m_minute;
secondHand * m_second;
public:
int nHour; //当前的系统时间
int nMinute;
int nSecond;
};
#endif /* defined(__Clock_1__clockBackgroundScene__) */
//
// clockBackgroundScene.cpp
// Clock_1
//
// Created by mzy1992 on 13-11-25.
//
//
#include "HelloWorldScene.h"
CCScene * clockBackgroundScene::scene(){
clockBackgroundScene * layer = clockBackgroundScene::create();
CCScene * scene = CCScene::create();
scene->addChild(layer);
return scene;
}
bool clockBackgroundScene::init(){
CCSize winSize = CCDirector::sharedDirector()->getWinSize(); //获取屏幕大小
CCSprite * clockBk = CCSprite::create("background.jpg"); //创建一个精灵
clockBk->setPosition(ccp(winSize.width / 2, winSize.height / 2));//设置背景精灵的坐标
clockBk->setScale(0.5f); //将精灵缩小为原来的0.5倍
this->addChild(clockBk); //将时钟背景添加到屏幕上
m_minute = minuteHand::create(); //创建分针对象
// m_minute->bindSprite(CCSprite::create("fen.png"));//初始化也可以放在m_minute的init方法内
// m_minute->setPosition(ccp(winSize.width / 2, winSize.height / 2));
this->addChild(m_minute);
m_second = secondHand::create();
m_second->bindSprite(CCSprite::create("miao.png"));
m_second->setPosition(ccp(winSize.width / 2, winSize.height / 2));
this->addChild(m_second);
m_hour = hourHand::create(); //创建时针对象
m_hour->bindSprite(CCSprite::create("shi.png")); //调用hourHand类中的bindSprite方法绑定精灵对象
m_hour->setPosition(ccp(winSize.width / 2, winSize.height / 2));
this->addChild(m_hour);
//获取当前的系统时间
struct tm *tm;
time_t timep;
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
time(&timep);
#else
struct timeval tv;
gettimeofday(&tv, NULL);
timep = tv.tv_sec;
#endif
tm = localtime(&timep);
nHour = tm->tm_hour;
nMinute = tm->tm_min;
nSecond = tm->tm_sec;
// CCLog("%d-%d-%d", nHour, nMinute, nSecond); //打印出来对比一下获取的时间是否有误
// nHour = 12; //当前时
// nMinute = 5; //当前分
// nSecond = 5; //当前秒
this->schedule(schedule_selector(clockBackgroundScene::MutUpdate), 1.0f); //每隔一秒钟调用一次MutUpdate方法
return true;
}
void clockBackgroundScene::MutUpdate(float aa){
static int mRotation = nMinute * 6; //分针初始角度
static int sRotation = nSecond * 6; //秒针初始角度
static int hRotatio; /*小时的计数不同于分和秒,分和秒一个周期都是60,而小时数在一天之中是从1-24,我们需要将获取到的时间先转换
成12时制,再将其按照比例变成60为周期的数,即给当前小时*5 */
if (nHour>12){ //当 当前时间大于上午12点时
hRotatio = (nHour - 12) * 5 * 6 + (mRotation / 72) * 6; /*将其转换成12时制 并*5变成以60为周期的数,因为60对应360度,所以应换算成度数应再*6,
换算完成后还应该加上分针走过的偏转角,由于分针走360度,时针走30度,所以分针每走72度时针走6度*,故用分针旋转角除以72后再乘以6 */
}
else{ //当前时间小于或等于12点
hRotatio = (nHour)* 5 * 6 + (mRotation / 72) * 6;
}
m_second->setRotation(sRotation); //重新设置秒针的位置,另其角度顺时针旋转6度,即走一秒钟
m_minute->setRotation(mRotation); //重置分针位置
m_hour->setRotation(hRotatio); //重置时针位置
if (sRotation >= 360){ //当秒针走了一圈(360度)时
sRotation = 0; //秒针旋转角归零
mRotation += 6; //分针转6度,即走了一分钟
m_minute->setRotation(mRotation); //重置分针指针位置
if (mRotation % 72 == 0){ //每当分针转到72的倍数时
hRotatio += 6; //时针转六度
m_hour->setRotation(hRotatio); //重置时针位置
if (mRotation >= 360){ //当分针转了一圈时
mRotation = 0; //分针旋转角归零
}
}
}
sRotation += 6;
}
hourhand.h:
#ifndef __Clock_1__hourHand__
#define __Clock_1__hourHand__
#include <iostream>
#include "cocos2d.h"
USING_NS_CC;
class hourHand :public CCLayer{
public:
virtual bool init();
CREATE_FUNC(hourHand);
CCSprite * getSprite(); //获取精灵对象
void bindSprite(CCSprite * sprite); //绑定精灵对象
private:
CCSprite * m_sprite;
};
#endif /* defined(__Clock_1__hourHand__) */
//
// hourHand.cpp
// Clock_1
//
// Created by mzy1992 on 13-11-25.
//
//
#include "hourHand.h"
CCSprite * hourHand::getSprite(){
return this->m_sprite;
}
void hourHand::bindSprite(CCSprite * sprite){
m_sprite = sprite;
m_sprite->setAnchorPoint(ccp(0, 0.5)); /*设置精灵锚点为(0,0.5),因为默认锚点为(0.5,0.5)是指针的中心,但是指针旋转时并不是沿着
中心旋转的,而是以它的尾部重点旋转,所以需要更改他的锚点为x是0,y是0.5,即宽的一半*/
m_sprite->setScale(0.2f); //将精灵大小扩大0.2倍
m_sprite->setRotation(-90); //将精灵逆时针旋转90度 因为时针初始方向是指向3的,旋转后让他指向12
this->addChild(m_sprite); //将精灵添加到当前层
}
bool hourHand::init(){
return true;
}
标签:blog http io ar os sp for on div
原文地址:http://www.cnblogs.com/notlate/p/4112783.html