Cocos Creater 自定义倒计时

最近游戏里需要一个道具倒计时的功能提供玩家可以和道具持续时间的交互,于是记录下,方便以后查看

进度条UI制作

首先进度条只是个普通的样式,直接拿Photoshop画了个圆角矩形,给了无填充和20px边.

在这里插入图片描述

加入组件

注意这里Type要选择Filled,这样之后才能使用后面的根据百分比绘制
在这里插入图片描述
当然图片给了一点opacity,要不然太亮了。

Code

接下来就是代码块了,实现了代码,只用在启用的时候call 一下startCountDown()就可以了。

// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const { ccclass, property } = cc._decorator;

@ccclass
export default class CountDown extends cc.Component {

    public countDownTime: number = 13; // second
    private sprite: cc.Sprite;
    private isStart: boolean = false;
    private nowTime: number;
    private callback: Function;
    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        this.sprite = this.node.getComponent(cc.Sprite);
    }

    public startCountDown(countDownTime: number, callback: Function) {
        this.countDownTime = !!countDownTime ? countDownTime / 1000 : this.countDownTime;
        this.node.active = true;
        this.nowTime = 0;
        this.callback = callback;
        this.isStart = true;

    }
    update(dt) {
        if (!this.isStart) {
            return;
        }

        this.nowTime += dt;
        let per: number = this.nowTime / this.countDownTime;
        if (per > 1) {
            per = 1;
            this.isStart = false;
            this.node.active = false;
            if (!!this.callback) {
                this.callback();
            }
        }
        this.sprite.fillRange = (1 - per);
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值