GVKun编程网logo

cocos 简单时钟(cocos计时)

15

在这篇文章中,我们将带领您了解cocos简单时钟的全貌,包括cocos计时的相关情况。同时,我们还将为您介绍有关android自定义简单时钟、cocoscodeide导入cocos2d-js自带的实例

在这篇文章中,我们将带领您了解cocos 简单时钟的全貌,包括cocos计时的相关情况。同时,我们还将为您介绍有关android自定义简单时钟、cocos code ide 导入cocos2d-js自带的实例、cocos code ide倒入cocos引擎路径时提示不是完整的cocos引擎、Cocos 商店所有类别,store.cocos.com的知识,以帮助您更好地理解这个主题。

本文目录一览:

cocos 简单时钟(cocos计时)

cocos 简单时钟(cocos计时)

 

原文链接: cocos 简单时钟

上一篇: cocos 粒子同心圆 跟随鼠标

下一篇: cocos 扇形进度条

效果, 每次走的是一个单位, 所以只有在秒针真正转完一圈时, 分针才会走一格

 

 

 

基本思想

秒针: 1秒6度, 60秒转360度

分针: 1秒十分之一度, 60秒转6度, 3600秒转360度

时针: 1秒一百二十分之一度, 3600秒转30度, 12个小时转一圈

 

每次走一格的思想是设置一个世界时间, 每次增加一秒, 然后只有在秒针走完一圈时才进行进位处理

const { ccclass, property } = cc._decorator;

const MAX_DEGREE = 24 * 60 * 60 * 360;
@ccclass
export default class NewClass extends cc.Component {
  @property(cc.Label)
  label: cc.Label = null;
  @property(cc.Node)
  hNode: cc.Node = null;
  @property(cc.Node)
  mNode: cc.Node = null;
  @property(cc.Node)
  sNode: cc.Node = null;
  @property
  text: string = "hello";

  // 时间
  h = 0;
  m = 0;
  s = 0;
  time = 0;

  // 每隔多少时间, 秒针走一格
  speed = 1000;

  @property(cc.Button)
  incBtn: cc.Button = null;
  @property(cc.Button)
  decBtn: cc.Button = null;

  handler = null;
  onLoad() {
    this.incBtn.node.on(cc.Node.EventType.MOUSE_DOWN, () => {
      this.speed *= 2;
    });
    this.decBtn.node.on(cc.Node.EventType.MOUSE_DOWN, () => {
      this.speed /= 2;
    });
    console.error(this.hNode);
  }
  setTime(t) {
    this.time = t % MAX_DEGREE ;
    // 秒针的位置
    this.s = this.time % 360;
    // 分针角度
    this.m = ((this.time - this.s) / 60) % 360;
    // 时针角度
    this.h = ((this.time - this.s - this.m * 60) / 3600) % 360;
    this.sNode.rotation = this.s;
    this.mNode.rotation = this.m;
    this.hNode.rotation = this.h;

    const s = [(this.h / 6) | 0, (this.m / 6) | 0, (this.s / 6) | 0].join(":");
    this.label.string = s;
  }
  update(dt) {
    const dx = (dt * this.speed) | 0;
    this.time += dx;
    this.setTime(this.time);
  }
}

 

 

 

 

连续移动

const { ccclass, property } = cc._decorator;

const MAX_DEGREE = 24 * 60 * 60;
@ccclass
export default class NewClass extends cc.Component {
  @property(cc.Label)
  label: cc.Label = null;
  @property(cc.Node)
  hNode: cc.Node = null;
  @property(cc.Node)
  mNode: cc.Node = null;
  @property(cc.Node)
  sNode: cc.Node = null;
  @property
  text: string = "hello";

  // 时间
  h = 0;
  m = 0;
  s = 0;
  // 一天24小时的秒数
  time = 0;

  // 每隔多少时间, 秒针走一格
  speed = 1;

  @property(cc.Button)
  incBtn: cc.Button = null;
  @property(cc.Button)
  decBtn: cc.Button = null;

  handler = null;
  onLoad() {
    this.incBtn.node.on(cc.Node.EventType.MOUSE_DOWN, () => {
      this.speed = this.speed * 2;
    });
    this.decBtn.node.on(cc.Node.EventType.MOUSE_DOWN, () => {
      this.speed = (this.speed / 2) | 0;
    });
    console.error(this.hNode);

    setInterval(() => {
      this.time += this.speed;
      console.log("===", this.time);
      this.setTime(this.time);
    }, 100);
  }
  setTime(t) {
    this.time = t % MAX_DEGREE;
    // 秒针的位置
    this.s = this.time % 360;
    // 分针角度
    this.m = this.time / 10;
    while (this.m >= 360) this.m -= 360;
    // 时针角度
    this.h = this.time / 120;
    while (this.h >= 360) this.h -= 360;
    this.sNode.angle = -this.s;
    this.mNode.angle = -this.m;
    this.hNode.angle = -this.h;
    const h = ((this.time / 3600) | 0) % 24;
    const m = ((this.time / 60) | 0) % 60;
    const s = (this.time | 0) % 60;
    const str = [h, m, s].join(":");
    this.label.string = str;
  }
}

 

android自定义简单时钟

android自定义简单时钟

本文实例为大家分享了android实现简单时钟的具体代码,供大家参考,具体内容如下

attrs定义如下

<?xml version="1.0" encoding="utf-8"?>
<resources>
 <declare-styleable name="ClockView">
  <attr name="pointer_color" format="color" />
  <attr name="scale_color" format="color" />
  <attr name="one_circle_color" format="color" />
  <attr name="two_circle_color" format="color" />
  <attr name="three_circle_color" format="color" />
  <attr name="four_circle_color" format="color" />
 </declare-styleable>
</resources>

自定义ClockView代码如下

package com.example.helloworld;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import java.util.Calendar;

/**
 * 时钟
 *
 * @since 2021-02-29
 * @author Sar
 */
public class ClockView extends View {
 private static final int DEFAULT_POINTER_COLOR = Color.parseColor("#FFFFFF");
 private static final int DEFAULT_SCALE_COLOR = Color.parseColor("#FFFFFF");
 private static final int DEFAULT_ONE_CIRCLE_COLOR = Color.parseColor("#FFFFFF");
 private static final int DEFAULT_TWO_CIRCLE_COLOR = Color.parseColor("#CCCCCC");
 private static final int DEFAULT_THREE_CIRCLE_COLOR = Color.parseColor("#BBBBBB");
 private static final int DEFAULT_FOUR_CIRCLE_COLOR = Color.parseColor("#AAAAAA");
 private static final int DEFAULT_FOUR_CIRCLE_DIAMETER = 180; // 默认外圈圆直径,也就是该控件默认宽高,单位dp

 private int pointerColor; // 指针颜色
 private int scaleColor; // 刻度颜色
 private int oneCircleColor; // 中心圆颜色
 private int twoCircleColor; // 次中心圆颜色
 private int threeCircleColor; // 次外圈圆颜色
 private int fourCircleColor; // 外圈圆颜色

 private Paint paint;

 private int cx;
 private int cy;
 private int scaleStrokeWidth;
 private int hourPointerWidth;
 private int minutePointerWidth;
 private int oneCircleDiameter;
 private int twoCircleDiameter;
 private int threeCircleDiameter;
 private int fourCircleDiameter;
 private int defaultFourCircleDiameter;

 public ClockView(Context context) {
  super(context);
  init(context, null, 0);
 }

 public ClockView(Context context, @Nullable AttributeSet attrs) {
  super(context, attrs);
  init(context, attrs, 0);
 }

 public ClockView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
  super(context, attrs, defStyleAttr);
  init(context, attrs, defStyleAttr);
 }

 private void init(Context context, AttributeSet attrs, int defStyleAttr) {
  if (attrs != null) {
   TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ClockView, defStyleAttr, 0);
   pointerColor = typedArray.getColor(R.styleable.XTClockView_pointer_color, DEFAULT_POINTER_COLOR);
   scaleColor = typedArray.getColor(R.styleable.XTClockView_scale_color, DEFAULT_SCALE_COLOR);
   oneCircleColor = typedArray.getColor(R.styleable.XTClockView_one_circle_color, DEFAULT_ONE_CIRCLE_COLOR);
   twoCircleColor = typedArray.getColor(R.styleable.XTClockView_two_circle_color, DEFAULT_TWO_CIRCLE_COLOR);
   threeCircleColor = typedArray.getColor(R.styleable.XTClockView_three_circle_color, DEFAULT_THREE_CIRCLE_COLOR);
   fourCircleColor = typedArray.getColor(R.styleable.XTClockView_four_circle_color, DEFAULT_FOUR_CIRCLE_COLOR);
   typedArray.recycle();
  } else {
   pointerColor = DEFAULT_POINTER_COLOR;
   scaleColor = DEFAULT_SCALE_COLOR;
   oneCircleColor = DEFAULT_ONE_CIRCLE_COLOR;
   twoCircleColor = DEFAULT_TWO_CIRCLE_COLOR;
   threeCircleColor = DEFAULT_THREE_CIRCLE_COLOR;
   fourCircleColor = DEFAULT_FOUR_CIRCLE_COLOR;
  }
  paint = new Paint(Paint.ANTI_ALIAS_FLAG);
  paint.setStyle(Paint.Style.FILL);
  defaultFourCircleDiameter = dp2px(context, DEFAULT_FOUR_CIRCLE_DIAMETER);
 }
 
 public void setPointerColor(int pointerColor) {
  this.pointerColor = pointerColor;
 }

 public void setScaleColor(int scaleColor) {
  this.scaleColor = scaleColor;
 }

 public void setOneCircleColor(int oneCircleColor) {
  this.oneCircleColor = oneCircleColor;
 }

 public void setTwoCircleColor(int twoCircleColor) {
  this.twoCircleColor = twoCircleColor;
 }

 public void setThreeCircleColor(int threeCircleColor) {
  this.threeCircleColor = threeCircleColor;
 }

 public void setFourCircleColor(int fourCircleColor) {
  this.fourCircleColor = fourCircleColor;
 }

 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  int width = 0;
  int height = 0;
  int widthMode = MeasureSpec.getMode(widthMeasureSpec);
  int widthSize = MeasureSpec.getSize(widthMeasureSpec);
  switch (widthMode) {
   case MeasureSpec.EXACTLY:
    width = widthSize;
    break;
   case MeasureSpec.AT_MOST:
   case MeasureSpec.UNSPECIFIED:
    width = defaultFourCircleDiameter;
    break;
  }
  int heightMode = MeasureSpec.getMode(heightMeasureSpec);
  int heightSize = MeasureSpec.getSize(heightMeasureSpec);
  switch (heightMode) {
   case MeasureSpec.EXACTLY:
    height = heightSize;
    break;
   case MeasureSpec.AT_MOST:
   case MeasureSpec.UNSPECIFIED:
    height = defaultFourCircleDiameter;
    break;
  }
  setMeasuredDimension(width, height);
  cx = width / 2;
  cy = height / 2;
  fourCircleDiameter = Math.min(width, height);
  threeCircleDiameter = fourCircleDiameter * 7 / 10;
  twoCircleDiameter = fourCircleDiameter * 28 / 50;
  oneCircleDiameter = fourCircleDiameter * 3 / 50;
  scaleStrokeWidth = fourCircleDiameter / 100;
  hourPointerWidth = oneCircleDiameter * 2 / 5;
  minutePointerWidth = oneCircleDiameter / 5;
 }

 private int dp2px(Context context, int dpValue) {
  float scale = context.getResources().getDisplayMetrics().density;
  return (int) (dpValue * scale + 0.5f);
 }

 @Override
 protected void onDraw(Canvas canvas) {
  super.onDraw(canvas);
  drawCircle(canvas, fourCircleColor, fourCircleDiameter);
  drawCircle(canvas, threeCircleColor, threeCircleDiameter);
  drawCircle(canvas, twoCircleColor, twoCircleDiameter);

  drawScale(canvas);
  Calendar calendar = Calendar.getInstance();
  calendar.setTimeInMillis(System.currentTimeMillis());
  int hour = calendar.get(Calendar.HOUR);
  int minute = calendar.get(Calendar.MINUTE);
  int second = calendar.get(Calendar.SECOND);

  paint.setColor(pointerColor);
  drawPointer(canvas, (float) (hour * 30 + minute * 0.5), hourPointerWidth, cy - twoCircleDiameter / 2 * 2 / 3);
  drawPointer(canvas, minute * 6, minutePointerWidth, cy - twoCircleDiameter / 2 * 4 / 5);
  drawPointer(canvas, second * 6, minutePointerWidth, cy - twoCircleDiameter / 2 * 8 / 9);

  paint.setStrokeWidth(0);
  drawCircle(canvas, oneCircleColor, oneCircleDiameter);

  postInvalidateDelayed(1000);
 }

 private void drawCircle(Canvas canvas, int circleColor, int circleDiameter) {
  paint.setColor(circleColor);
  canvas.drawCircle(cx, cy, circleDiameter / 2, paint);
 }

 private void drawScale(Canvas canvas) {
  paint.setColor(scaleColor);
  paint.setStrokeWidth(scaleStrokeWidth);
  for (int i = 0; i < 12; i++) {
   canvas.save();
   canvas.rotate(i * 30, cx, cy);
   canvas.drawLine(cx, (fourCircleDiameter - threeCircleDiameter) / 6, cx, (fourCircleDiameter - threeCircleDiameter) / 3, paint);
   canvas.restore();
  }
 }

 private void drawPointer(Canvas canvas, float rotateDegrees, float strokeWidth, float stopY) {
  paint.setStrokeWidth(strokeWidth);
  canvas.save();
  canvas.rotate(rotateDegrees, cx, cy);
  canvas.drawLine(cx, cy, cx, stopY, paint);
  canvas.restore();
 }
}

效果图如下

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

您可能感兴趣的文章:
  • Android获取设备CPU核数、时钟频率以及内存大小的方法
  • Android多功能时钟开发案例(实战篇)
  • android实现widget时钟示例分享
  • Android 仿日历翻页、仿htc时钟翻页、数字翻页切换效果
  • android高仿小米时钟(使用Camera和Matrix实现3D效果)
  • Android多功能时钟开发案例(基础篇)
  • Android实现简单时钟View的方法
  • Android自定义动态壁纸开发(时钟)
  • Android仿小米时钟效果
  • Android编程基于自定义控件实现时钟功能的方法

cocos code ide 导入cocos2d-js自带的实例

cocos code ide 导入cocos2d-js自带的实例

在上一节,我们看了怎样搭建一个javaScript的开发环境,这一节我们将继续研究如何将cocos2d自带实例如何导入到cocos code ide并且运行出来,为什么这样做,我们先预留这样的问题在这个地方。

首先我们创建一个项目:


这个是我们创建的项目,自动生成的目录,现在我们要导入引擎中自带的一个实例:哪这个实例被放在了那个里面:


我们可以看到的是在自带的项目中里面也是包含src,res,main.js,index.html,还有project.json,是的,细心的肯定也会发现在我们创建项目中也有这些目录,既然是重复了,我们自然要删除,所以我们将创建项目的时候自带的这些文件删除(建议不要去覆盖,经过多次试验,覆盖容易发生错误)


删除之后我们在自带的实例里面的这几个文件夹,复制粘贴到这个项目里面(复制粘贴的时候仅仅复制我们删除的那些文件,对于其他的文件就不要去复制了,如果你想试试,随你)

复制到项目之后,我们打开一project.json这个文件,将里面引擎的路径改一下:

"engineDir":"../../frameworks/cocos2d-html5",

改为:

"engineDir":"frameworks/cocos2d-html5",

更改之后:


然后我们就可以运行了,运行结果:


就这样,我们将引擎中自带的项目就可在看到了,希望对你有所帮助,下一步熟悉自己创建项目中里面的里面语法所代表的含义。

cocos code ide倒入cocos引擎路径时提示不是完整的cocos引擎

cocos code ide倒入cocos引擎路径时提示不是完整的cocos引擎

用这个路径上的就行了。

Cocos 商店所有类别,store.cocos.com

Cocos 商店所有类别,store.cocos.com

Cocos 商店所有类别:

http://store.cocos.com/stuff/category/16/score_count.html

http://store.cocos.com/stuff/category/24/score_count.html
http://store.cocos.com/stuff/category/25/score_count.html
http://store.cocos.com/stuff/category/26/score_count.html
http://store.cocos.com/stuff/category/7/score_count.html
http://store.cocos.com/stuff/category/10/score_count.html
http://store.cocos.com/stuff/category/4/score_count.html
http://store.cocos.com/stuff/category/17/score_count.html
http://store.cocos.com/stuff/category/19/score_count.html
http://store.cocos.com/stuff/category/27/score_count.html
http://store.cocos.com/stuff/category/8/score_count.html
http://store.cocos.com/stuff/category/20/score_count.html
http://store.cocos.com/stuff/category/11/score_count.html
http://store.cocos.com/stuff/category/28/score_count.html

http://store.cocos.com/stuff/category/13/score_count.html


Cocos 商店所有类别:

http://store.cocos.com/stuff/category/14/score_count.html


http://store.cocos.com/contest.html

今天的关于cocos 简单时钟cocos计时的分享已经结束,谢谢您的关注,如果想了解更多关于android自定义简单时钟、cocos code ide 导入cocos2d-js自带的实例、cocos code ide倒入cocos引擎路径时提示不是完整的cocos引擎、Cocos 商店所有类别,store.cocos.com的相关知识,请在本站进行查询。

本文标签: