cocos数字滚动

cocos数字滚动

今天分享一个非常简单的小功能:数字滚动,源码地址在底部。先看效果:
在这里插入图片描述
这个效果使用cc.tween制作,代码量非常少,下面这些就是全部的功能代码:在这里插入图片描述
当前代码实现的只有整数部分滚动,没有做小数部分,想要实现小数部分也很简单,只要把set部分的Math.floor(text)更改成Math.floor(text * 100) / 100即可实现小数点后两位滚动。总之很简单的一个小功能,源码地址:
https://gitee.com/egco/numberRollDemo

源码中一个是cocos creator的一个是egret的。欢迎看到这篇文章的大神能给指出做的不足之处。
下面这个是我个人的微信公众号,欢迎大家赏个关注,感谢!
在这里插入图片描述

  • 6
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
Cocos Creator 中可以使用动作(Action)来创建数字滚动效果。下面是一个简单的示例代码: 1. 创建一个空节点,将它命名为“NumberRoller”。 2. 在“NumberRoller”节点下创建三个 Label 节点,分别命名为“Num1”、“Num2”、“Num3”。 3. 在“NumberRoller”节点上添加一个脚本组件,将以下代码添加到脚本中: ```javascript cc.Class({ extends: cc.Component, properties: { startValue: 0, endValue: 999, duration: 2, interval: 0.05, num1: cc.Label, num2: cc.Label, num3: cc.Label }, start () { this.num1.string = this.startValue; this.num2.string = this.startValue; this.num3.string = this.startValue; this.schedule(this.updateValue, this.interval); }, updateValue () { let value1 = Math.floor(Math.random() * 10); let value2 = Math.floor(Math.random() * 10); let value3 = Math.floor(Math.random() * 10); let valueSum = value1 * 100 + value2 * 10 + value3; let currentValue = parseInt(this.num3.string) + valueSum; if (currentValue > this.endValue) { currentValue = this.endValue; this.unschedule(this.updateValue); } this.num1.string = this.num2.string; this.num2.string = this.num3.string; this.num3.string = currentValue; } }); ``` 4. 在“NumberRoller”节点上配置脚本组件的属性,包括起始值(startValue)、结束值(endValue)、动画时间(duration)和数字变化的时间间隔(interval)。 5. 运行游戏,可以看到数字滚动的效果。 这只是一个简单的数字滚动效果示例,你可以根据你的实际需求进行修改和扩展。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值