FGUI+TS如何实现数字翻滚

FGUI+TS如何实现数字翻滚

实现效果如下:

实现步骤:

  1. fgui制作组件和特效

    1. fgui制作组件,设置一条竖向数字包含1-9或者小数点符号等,可见区域为一个数字大小,最好可见区域紧贴数字,这样滚动的时候滚动区域范围不会太大

    2. fgui制作动效,设置一个起始帧以及一个结束帧,起始帧改变位置x为0,y为0;结束帧改变位置x为0,y为你后一个数字或符号的位置,在之间设置缓动函数,达到连贯的效果

    3. 制作一个列表,将这个组件放进去,调整位置

  2. 代码控制

    1. 制作一个记录y轴位置的map,将你所有的数字和符号位置放进去
    2. 初始化开始值,针对更变值进行动效播放,实现效果~9527.1314数字翻动的效果
    js 复制代码
    // 记录每个位置
    setNumPositionMap() {
            let cellHeight = 75
            for (let index = 0; index <= 9; index++) {
                this.numPositionMap.set(index, -(index * cellHeight))
            }
            this.numPositionMap.set('symbol', -(10 * cellHeight))
            this.numPositionMap.set('point', -(11 * cellHeight))
        }
    
    // 更新列表为当前显示值
    updateNumList(pre: string){
            this.numList.removeChildrenToPool()
            for (let index = 0; index < pre.length; index++) {
                let preNum = pre[index]
                let com = this.numList.addItemFromPool().asCom
                if (preNum == ".")  {
                    com.getChild("n0").setPosition(0, this.numPositionMap.get("point"))
                }else if (preNum == "~") {
                    com.getChild("n0").setPosition(0, this.numPositionMap.get("symbol"))
                } else {
                    com.getChild("n0").setPosition(0, this.numPositionMap.get(Number.parseInt(preNum)))
                }
            }
        }
    
    // 播放动效
    playAnim(pre: string, cur: string){
            for (let index = this.numList.numItems - 1; index >= 0; index--) {
                let listItem = this.numList.getChildAt(index)
                let preNum = pre[index]
                let curNum = cur[index]
                
                if (preNum != "." && preNum != "~") {
                    let trans = listItem.asCom.getTransition("t0")
                    trans.setValue("start", 0, this.numPositionMap.get(Number.parseInt(preNum)))
                    trans.setValue("end", 0, this.numPositionMap.get(Number.parseInt(curNum)))
                    trans.play(function() {
                        if (index == 1) {
                            this.updateNumList(cur)
                        }
                    }.bind(this))
                }
            }
            
        }
相关推荐
寂寞的船1 小时前
【Unity知识速递】第13期 Unity 内存泄漏排查实战:从 Memory Profiler 到 Lua 引用泄漏的生产级全链路诊断
unity·junit·c#·游戏引擎·lua
Agent世界1 小时前
用 CDP 做发布自动化:四道机器闸门与产物验收
前端·javascript
FungLeo1 小时前
成为全栈·Vue 管理后台篇·编辑器图片上传:从文件选择到文章附件关联
前端·vue.js·编辑器·markdown·文件上传·formdata·附件管理
某哈压力大2 小时前
SSH 突然 Connection reset?不是密钥坏了,是 OpenSSL 把 sshd 干趴了
前端·node.js·命令行
程序员大阳2 小时前
副队长ES6+教程(3)--解构赋值
前端·javascript·es6·赋值·解构
神膘护体小月半2 小时前
css - 元素背景图由 hover 触发,初次加载时闪动的解决办法
前端·javascript·css
程序员大阳2 小时前
副队长ES6+教程(2)--let 与 const
前端·javascript·es6·const·let·var
deli0073 小时前
写满 200GB 后 SSD 为什么掉到 95MB/s?SLC 缓存模型实测
前端
曦樂~4 小时前
【Web Development】03JavaScript 基础和事件
开发语言·前端·javascript
deli0074 小时前
HTTPS 的锁是怎么挂上的?TLS 1.3 握手 6 步实测
前端