cocos2dx绘制时钟

#pragma once
#include "cocos2d.h"
using namespace cocos2d;
class MyScene :public cocos2d::Scene {

public:
	static MyScene * create();
	bool init();
private:
	int width;
	int height;
	void drawClock(DrawNode * drawNode);
	void initSprite();
	void initDrawNode();
	void myUpdate(float f);
};


#include "MyScene.h"
#include "SimpleAudioEngine.h"
#include <iostream>
#include <cmath>
#include <string> 
#include <sstream>
#include <math.h>
#include <windows.h> 
USING_NS_CC;
MyScene * MyScene::create() {
	//创建对象并初始化,然后返回该对象
	MyScene *myScene = new MyScene();
	if (myScene && myScene->init()) {
		//父类函数
		myScene->autorelease();
	}else {
		delete myScene;
		myScene = NULL;
	}
	return myScene;
}
bool MyScene::init() {
	if (!Scene::init())
	{
		return false;
	}
	Size size = Director::getInstance()->getVisibleSize(); //width = 800,heigth = 600
	this->width = size.width;
	this->height = size.height;
	DrawNode *clockNode = DrawNode::create();
	clockNode->setName("clock");
	drawClock(clockNode);
	this->addChild(clockNode);


	
	//每隔一段时间会执行myUpdate函数
	this->schedule(schedule_selector(MyScene::myUpdate), 0.01);//10ms


	//initDrawNode();
	//initSprite();
	return true;
}


void MyScene::initSprite() {
	
	Vec2 origin = Director::getInstance()->getVisibleOrigin();//x = 0, y = 0
	std::stringstream ss;


	ss << width << "," << height;


	auto label = Label::createWithTTF(ss.str(), "fonts/Marker Felt.ttf", 18);


	// position the label on the center of the screen
	label->setPosition(Vec2(origin.x + width / 2,
		origin.y + height - label->getContentSize().height));


	// add the label as a child to this layer
	this->addChild(label, 1);




	Sprite *sprite = Sprite::create("image/myscene.png"); //将图片放到程序所在目录	
	sprite->setName("image");
	cocos2d::Size size = sprite->getContentSize();
	//精灵的锚点为中心点
	sprite->setPosition(Vec2(0+size.width/2,height-size.height/2));//设置位置,坐标原点坐标为左下角
	this->addChild(sprite, 0);
	//每隔一段时间会执行myUpdate函数
	this->schedule(schedule_selector(MyScene::myUpdate),1);//
}


void MyScene::initDrawNode() {
	//创建DrawNode对象    
	DrawNode *drawNode = DrawNode::create();
	drawNode->setName("drawNode");
	//加入场景就OK    
	this->addChild(drawNode, 20);
	//画实心圆    
	drawNode->drawDot(Vec2(width / 2, height / 2), 50, Color4F(0.6, 0.5, 0, 1));


	//画线段    
	drawNode->drawSegment(Vec2(100, 100), Vec2(150, 220), 0.5, Color4F(0, 1, 0, 1));


	// 画多边形    
	Vec2 points[] = { Vec2(200,200), Vec2(300,300), Vec2(400,150), Vec2(250,100) };
	drawNode->drawPolygon(points, sizeof(points) / sizeof(points[0]), Color4F(1, 0, 0, 0.5), 2, Color4F(0, 0, 1, 1));


	// 画三角形    
	drawNode->drawTriangle(Vec2(140, 70), Vec2(210, 130), Vec2(180, 40), Color4F(CCRANDOM_0_1(), CCRANDOM_0_1(), CCRANDOM_0_1(), 0.5));


	// 画二次贝塞尔曲线    
	drawNode->drawQuadraticBezier(Vec2(width - 150, height - 150), Vec2(width - 70, height - 10), Vec2(width - 10, height - 10),
		10, Color4F(1, 0, 0, 1));


	// 画三次贝塞尔曲线    
	drawNode->drawCubicBezier(Vec2(width - 250, 40), Vec2(width - 70, 100), Vec2(width - 30, 250), Vec2(width - 10, height - 50),
		10, Color4F(0, 1, 0, 1));
}
void MyScene::drawClock(DrawNode * drawNode) {
	//画圆 中心点坐标,半径,角度位置,分段(越大越接近圆),true:中心到角度位置的直线,x缩放,y缩放(画椭圆),颜色
	int centerX = this->width / 2;
	int centerY = this->height / 2;
	int radius = 200;
	drawNode->drawCircle(Vec2(centerX,centerY), radius, 0, 100, false, 1.0, 1.0, Color4F(1.0,1.0, 0, 1));
	//drawNode->drawCircle(Vec2(centerX, centerY), radius /2, 0, 100, false, 1.0, 1.0, Color4F(1.0, 1.0, 0, 1));
	int len = 10;//刻度线长度
	cocos2d::Color4F color = Color4F(0.8, 0.6, 0.2, 1);//刻度线颜色
	//画刻度
	for (int i = 1; i <= 60; i++) {
		int startX = centerX + (radius - len) * sin(i * 6 * 3.14 / 180);
		int startY = centerY + (radius - len) * cos(i * 6 * 3.14 / 180);


		int endX = centerX + radius * sin(i * 6 * 3.14 / 180);
		int endY = centerY + radius * cos(i * 6 * 3.14 / 180);
		if (i % 5 == 0) {
			len = 15;
			color = Color4F(0, 1.0, 0, 1);//刻度线颜色
			
			//画数字
			std::stringstream ss;		
			ss << i/5;
			std::stringstream ss1;
			ss1 << "num" << i;
			std::string name = ss1.str();
			if (this->getChildByName(name) == NULL) {
				auto label = Label::createWithTTF(ss.str(), "fonts/Marker Felt.ttf", 18);
				label->setPosition(Vec2(startX,startY));
				label->setName(name);
				this->addChild(label, 1);
			}
			
		}
		else {
			len = 10;
			color = Color4F(0.8, 0.6, 0.2, 1);//刻度线颜色
		}
		//画刻度(线段:起始坐标,线条粗细,颜色)
		drawNode->drawSegment(Vec2(startX, startY), Vec2(endX, endY), 0.5, color);


		
	}


	//获取时间
	SYSTEMTIME sys;
	GetLocalTime(&sys);
	int hour = sys.wHour;
	int min = sys.wMinute;
	int sec = sys.wSecond;


	//画时间
	std::stringstream ss2;
	ss2 << hour << ":" << min << ":" << sec;
	auto label = this->getChildByName("timeLabel");
	if ( label== NULL) {
		label = LabelTTF::create(ss2.str(), "fonts/Marker Felt.ttf", 18);
		label->setPosition(Vec2(700, 550));
		label->setName("timeLabel");


		this->addChild(label, 2);
	}
	else {
		LabelTTF * labelTTF = reinterpret_cast<LabelTTF *>(label);
		labelTTF->setString(ss2.str());
	}
	




	//画时针,分针,秒针


	int hourLen = 130;//时针长度
	int hourEndX = centerX + hourLen * sin(30 * hour *3.14 / 180);
	int hourEndY = centerY + hourLen * cos(30 * hour * 3.14 / 180);
	drawNode->drawSegment(Vec2(centerX, centerY), Vec2(hourEndX, hourEndY), 0.5, Color4F(1.0, 0.7, 0, 1));


	int minLen = 150;//分针长度
	int minEndX = centerX + minLen * sin(6 * min * 3.14 / 180);
	int minEndY = centerY + minLen * cos(6 * min * 3.14 / 180);
	drawNode->drawSegment(Vec2(centerX, centerY), Vec2(minEndX, minEndY), 0.5, Color4F(0.5, 0.7, 0, 1));


	int secLen = 180;//秒针长度
	int secEndX = centerX + secLen * sin(6 * sec * 3.14 / 180);
	int secEndY = centerY + secLen * cos(6 * sec * 3.14 / 180);
	drawNode->drawSegment(Vec2(centerX, centerY), Vec2(secEndX, secEndY), 0.5, Color4F(1.0, 0, 0, 1));




}


void MyScene::myUpdate(float f) {


	//cocos2d::Node * node = this->getChildByName("image");
	//cocos2d::Size size = node->getContentSize();
	//int width = size.width;
	//int height = size.height;
	//int x = node->getPosition().x;
	//int y = node->getPosition().y;
	//if (x >= this->width - width / 2) {
	//	x = width / 2;
	//}
	//node->setPosition(x+2,y);
	cocos2d::Node * node = this->getChildByName("clock");
	DrawNode* drawNode = reinterpret_cast<DrawNode*>(node);
	//清除该node之前画的
	drawNode->clear();
	//重新画
	drawClock(drawNode);
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值