local MainButton = {}
MainButton.cfg = {
{ but_id = "id1", x = 230, y = -270, but_img = "res/public/996kf_red.png", cmd = { 3100, 1 } }, --GM功能
}
function MainButton:main()
SL:ScheduleOnce(function()
local parent = GUI:Attach_LeftTop
🤖 回答引擎: 前端Lua |
🕒 时间: 2026-03-09 01:59:45 |
👁️ 阅读: 8
根据浮生梦老师前端课程内容,要实现北京时间显示的倒计时(小时、分钟、秒钟),可以使用`Text_COUNTDOWN`函数。这个函数专门用于处理倒计时显示,可以自动格式化为时分秒。
以下是修改后的代码:
```lua
local MainButton = {}
MainButton.cfg = {
{ but_id = "id1", x = 230, y = -270, but_img = "res/public/996kf_red.png", cmd = { 3100, 1 } }, --GM功能
}
function MainButton:main()
SL:ScheduleOnce(function()
local parent = GUI:Attach_LeftTop()
SL:print("parent:----------------1", parent)
if GUI:Win_IsNull(parent) then
SL:print("parent为空")
return
end
-- 检查并移除旧节点
if GUI:getChildByName(parent, "node_rightSide_1") then
local oldNode = GUI:getChildByName(parent, "node_rightSide_1")
GUI:removeFromParent(oldNode)
SL:print("删除了旧节点")
end
-- 创建新节点
local Top_node_obj = GUI:Widget_Create(parent, "node_rightSide_1", 0, 0, 0, 0)
if GUI:Win_IsNull(Top_node_obj) then
SL:print("创建节点失败")
return
end
for _, v in pairs(MainButton.cfg) do
-- 创建按钮
local but_switch = GUI:Button_Create(Top_node_obj, "switch", v.x, v.y, v.but_img)
SL:print("点击了" .. v.but_id .. "按钮", v.x, v.y)
GUI:addOnClickEvent(but_switch, function()
SL:print("点击了" .. v.but_id .. "按钮")
end)
-- 在按钮正下方添加倒计时文本
-- 按钮高度假设为46,设置倒计时在按钮下方10像素位置
local timeText = GUI:Text_Create(Top_node_obj, "timeText_" .. v.but_id, v.x, v.y - 46 - 10, 16, "#FF0000", "")
GUI:setAnchorPoint(timeText, 0.5, 0.5)
-- 使用Text_COUNTDOWN函数设置倒计时
-- 参数说明:
-- timeText: 文本对象
-- 3600: 倒计时时间(3600秒 = 1小时),你可以根据需要修改
-- nil: 倒计时结束回调函数(可选)
-- 0: 显示方式(0: xx时xx分xx秒)
GUI:Text_COUNTDOWN(timeText, 3600, nil, 0)
end
end, 0.2)
end
return MainButton
```
**主要修改点:**
1. **使用`Text_COUNTDOWN`函数**:这个函数会自动处理时分秒的格式化显示
2. **显示格式参数**:第四个参数设为`0`,表示显示为"xx时xx分xx秒"格式
3. **初始文本**:创建文本时初始字符串设为空字符串`""`,因为倒计时内容由`Text_COUNTDOWN`自动更新
4. **移除了手动定时器**:不再需要`SL:schedule`和手动计算剩余时间
**参数说明:**
- `timeText`:要显示倒计时的文本对象
- `3600`:倒计时总秒数(这里示例为1小时)
- `nil`:倒计时结束时的回调函数(可选,不需要可设为nil)
- `0`:显示方式,0表示"xx时xx分xx秒"格式
**注意:**
- 倒计时时间是基于服务器时间的,所以会自动同步
- 如果你需要不同的倒计时时长,只需修改第二个参数(秒数)
- 如果需要倒计时结束后的处理,可以在第三个参数传入回调函数
这样修改后,你的按钮下方就会显示一个格式为"01时00分00秒"这样的北京时间倒计时了。
← 返回问答库