当前位置: 首页 > news >正文

【cocos creator】输入框滑动条联动小组建

在这里插入图片描述
滑动条滑动输入框内容会改变
输入框输入,滑动条位置改变
在这里插入图片描述


const { ccclass, property } = cc._decorator;@ccclass()
export default class SliderEnter extends cc.Component {@property({ type: cc.Float, displayName: "最大值", tooltip: "" })public maxNum: number = 100;@property({ type: cc.Float, displayName: "最小值", tooltip: "" })public minNum: number = 0;@property({ type: cc.Float, displayName: "输入框初始值", tooltip: "" })public editBoxStart: number = 0;@property({ type: cc.Float, displayName: "滑动条初始位置", min: 0, max: 1, tooltip: "0~1,支持小数,对应输入框初始值" })sliderStart = 0;@property({ type: cc.Integer, displayName: "保留小数位数", tooltip: "" })public fixTo: number = 0;@property({ type: cc.EditBox, displayName: "输入框", tooltip: "" })public editBox: cc.EditBox = null;@property({ type: cc.Slider, displayName: "滑动条", tooltip: "" })public slider: cc.Slider = null;_sliderStart = 0;_editBoxStart = 0;protected onLoad(): void {this.onResetBtn()// this._sliderStart = this.slider.progress;// this._editBoxStart = this.checkEditBox()this.addHandler(this.slider.slideEvents, this.onSlide, "onSlide", this)this.addHandler(this.editBox.editingDidEnded, this.onSlide, "onEnter", this)}getData() {let num = this.checkEditBox()return num}onResetBtn() {this._sliderStart = this.sliderStart;this._editBoxStart = this.editBoxStartthis.slider.progress = this._sliderStartthis.editBox.string = this.fixForceStr(this._editBoxStart, this.fixTo)}checkEditBox() {let num = this.getInt(this.editBox.string, 0)if (num > this.maxNum) num = this.maxNum;if (num < this.minNum) num = this.minNum;this.editBox.string = num + ""return num;}onEnter() {let num = this.checkEditBox()if (num == this._editBoxStart) {num = this._sliderStart}else if (num <= this._editBoxStart) {this.slider.progress = (num - this.minNum) / (this._editBoxStart - this.minNum) * this._sliderStart}else {this.slider.progress = this._sliderStart + (num - this._editBoxStart) / (this.maxNum - this._editBoxStart) * (1 - this._sliderStart)}}onSlide() {let progress = this.slider.progresslet num = 0if (progress < 0) progress = 0if (progress > 1) progress = 1this.slider.progress = progressif (progress == this._sliderStart) {num = this._editBoxStart}else if (progress <= this._sliderStart) {num = this.minNum + (this._editBoxStart - this.minNum) * progress / this._sliderStart}else {num = this._editBoxStart + (progress - this._sliderStart) * (this.maxNum - this._editBoxStart) / (1 - this._sliderStart)}this.editBox.string = this.fixForceStr(num, this.fixTo)}addHandler(fun: any, handler, fnName = "", scope, eventData = "") {if (!fnName || !scope[fnName]) {fnName = "__BtnClick__" + (Math.random() + "").slice(2, 15);scope[fnName] = handler;}let eventHandler = new cc.Component.EventHandler();eventHandler.target = scope.node;eventHandler.component = cc.js.getClassName(this);eventHandler.handler = fnName;eventHandler.customEventData = eventData;if (typeof (fun) == "object" && cc.isValid(fun.length)) fun.push(eventHandler);return eventHandler}fixForceStr(count, fixTo): string {let a = (count + "").split(".");if (fixTo == 0) return a[0]let b = a[0];if (a.length > 1) b = a[0] + "." + a[1].slice(0, fixTo);if (b == "0.00" || b == "0.0") {return a[0]}return b;}getInt(data, defaultNum) {let num = Number(data)return isNaN(num) ? defaultNum : num;}
}
http://www.lryc.cn/news/460930.html

相关文章:

  • Flink时间窗口程序骨架结构
  • 计算机视觉之可做什么
  • 观察者模式的思考
  • ORACLE SELECT INTO 赋值为空,抛出 NO DATA FOUND 异常
  • GPT提示词
  • Redis协议详解及其异步应用
  • LeetCode213:打家劫舍II
  • linux一二三章那些是重点呢
  • C语言中的程序入口:超越main函数的探索
  • 《面试之MQ篇》
  • Git 分支操作-开发规范
  • JSONArray根据指定字段去重
  • 线程有哪几种状态? 分别说明从一种状态到另一种状态转变有哪些方式?
  • 自注意力机制self-attention中的KV 缓存
  • 前端库--nanoid(轻量级的uuid)
  • 计算机基础-什么是网络端口?
  • 力扣动态规划基础版(斐波那契类型)
  • Java重修笔记 InetAddress 类和 Socket 类
  • 秋招突击——8/6——万得数据面试总结
  • STM32定时器
  • 第七课 Vue中的v-for遍历指令
  • 【NTN 卫星通信】卫星通信的专利
  • vue3 element table 插槽外的数据更新,插槽内的数据未更新。
  • 飞凌嵌入式FET527N-C核心板已适配OpenHarmony4.1
  • CVPR 2024最佳论文候选-pixelSplat论文解读
  • 在Android中如何切割一张图片中的不规则“消息体/图片/表情包等等”?
  • Jenkins+Ant+Jmeter接口自动化集成测试
  • JavaSE——集合4:List接口实现类—LinkedList
  • FPGA图像处理之三行缓存
  • 10月15日,每日信息差