CocosCreator 工具类(持续更新)

import {
    _decorator, Component, Node, Vec2, Vec3, Quat, Camera, UITransform, view, log, Constructor, BoxCollider, geometry, SphereCollider, Mat3, director } from 'cc';
import {
    ColliderEx } from './ColliderEx';

const {
    ccclass, property } = _decorator;

export default class Util {
   

    static physicsMoveFactor: number = 70;

    /**
     * 返回 min ~ max 的随机数
     * @param min 最小值(包含)
     * @param max 最大值(包含)
     */
    static Random(min: number, max: number): number {
   
        return Math.floor(Math.random() * (max - min + 1) + min);
    }

    /**
     * 求两点之间的距离
     * @param point1 点1
     * @param point2 点2
     */
    static Distance(point1: Vec2, point2: Vec2): number {
   
        return Math.sqrt(Math.pow(point1.x - point2.x, 2) + Math.pow(point1.y - point2.y, 2));
    }

    //直线与坐标轴的夹角 (斜率转角度) 返回的是一个弧度值
    static Angle(point1: Vec2, point2: Vec2): number {
   
        let x = point2.x - point1.x;
        let y = point2.y - point1.y;
        return Math.atan2(y, x);
    }

    /**
     * 将秒数格式化为00:00
     * @param second 秒数
     */
    static GetTimeFormat(second: number) {
   
        let mStr = "";
        let sStr = "";
        let m = Math.floor(second / 60);
        let s = second % 60;
        if (m < 10)
            mStr = "0" + m;
        else
            mStr = m.toString();
        if (s < 10)
            sStr = "0" + s;
        else
            sStr = s.toString();
        return "mm:ss".replace("mm", mStr).replace("ss", sStr);
    }

    /**
     * 获取当前时间戳。得到距 1970 年 1 月 1 日之间的毫秒数,13位
     */
    static GetNowTimestamp(): number {
   
        return new Date().getTime();
    }

    /**
     * 获取当前时间戳。单位秒
     */
    static GetTime(): number{
   
        let time = Date.now() / 1000;
        let second = Math.floor(time);
        return second;
    }

    /**
     * 时间戳转换日期
     * @param timestamp 时间戳,13位
     */
    static TimestampToDate(timestamp): string {
   
        let time = parseInt(timestamp);
        if (timestamp.length < 13) //要转化为毫秒
            time = time * 1000;

        return new Date(time).toLocaleString();
    }

    //深拷贝
    static DeepClone(obj): any {
   
        let
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值