20.cocos2d-x时钟小项目

项目源码:http://download.csdn.net/download/laixiao_hero/9387974


主要代码:
HelloWorldScene.h文件:


#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"
using namespace cocos2d::ui;

class HelloWorld : public cocos2d::Layer
{
public:
    // there's no 'id' in cpp, so we recommend returning the class instance pointer
    static cocos2d::Scene* createScene();

    // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
    virtual bool init();

    // implement the "static create()" method manually
    CREATE_FUNC(HelloWorld);

private:
    Layout* Panel_1;
    ImageView* shi;
    ImageView* fen;
    ImageView* miao;

    int shiR = 0; //时针旋转角度
    int fenR = 0; //分针旋转角度
    int miaoR = 0; //秒针旋转角度

    struct tm *tm;
    time_t timep;

    void changeClock(float dt);
};

#endif // __HELLOWORLD_SCENE_H__


HelloWorldScene.cpp文件:


#include "HelloWorldScene.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"

USING_NS_CC;

using namespace cocostudio::timeline;
using namespace cocos2d::ui;

Scene* HelloWorld::createScene()
{
    // 'scene' is an autorelease object
    auto scene = Scene::create();

    // 'layer' is an autorelease object
    auto layer = HelloWorld::create();

    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }


    auto rootNode = CSLoader::createNode("MainScene.csb");
    Panel_1 = (cocos2d::ui::Layout*)rootNode->getChildByName("Panel_1");
    shi = (ImageView*)Helper::seekWidgetByName(Panel_1,"shi");
    fen = (ImageView*)Helper::seekWidgetByName(Panel_1, "fen");
    miao = (ImageView*)Helper::seekWidgetByName(Panel_1, "miao");

    Vec2 visiableSize = Director::getInstance()->getVisibleSize();
    Panel_1->setAnchorPoint(Vec2(0.5,0.5));
    Panel_1->setPosition(Vec2(visiableSize.x/2, visiableSize.y/2));

    this->schedule(schedule_selector(HelloWorld::changeClock),1.0f);



    addChild(rootNode);

    return true;
}

void HelloWorld::changeClock(float dt)
{
    if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) {
        time(&timep);
    }
    else {
        struct timeval nowTimeval;
        gettimeofday(&nowTimeval, NULL);
        timep = nowTimeval.tv_sec;
    }
    tm = localtime(&timep);
    int year = tm->tm_year + 1900;
    int month = tm->tm_mon + 1;
    int day = tm->tm_mday;
    int hour = tm->tm_hour;
    int min = tm->tm_min;
    int second = tm->tm_sec;

    miaoR = second * 6;
    fenR = min * 6;
    if (hour>12) {
        shiR = (hour - 12) * 5 * 6 + (fenR / 72) * 6;//转换成12时制,方便计算。
    }
    else
    {
        shiR = hour * 5 * 6 + (fenR / 72) * 6;

    }
    shi->setRotation(shiR);
    fen->setRotation(fenR);
    miao->setRotation(miaoR);

    CCLOG("%d-%d-%d    %d-%d-%d", year, month, day, hour, min, second);//输出时间2015-12-30  14-47-8
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值