npc.__cname = "PeiShiShouJi"
npc.id = 1
local _WndSizeX = 1440
local _WndSizeY = 960
function npc.main()
npc.n = 1
npc.id = 1
local parent = GUI:Win_Create(npc.__cname, 0, 0, 0, 0, false, false, true, true, true, 0)
GUI:LoadExport(pa
🤖 回答引擎: 前端Lua |
🕒 时间: 2026-05-05 00:02:43 |
👁️ 阅读: 5
根据浮生梦老师的前端课程内容,我来帮你优化这段代码。主要问题有:
1. **函数调用错误**:`Action_slow_appear` 应该使用 `GUI:Action_slow_appear`
2. **未定义的函数**:`GetBagItemNumByName` 在知识库中没有找到
3. **定时器使用不规范**:应该使用 `SL:ScheduleOnce` 而不是 `SL:ScheduleOnce(function() end, 0.5)` 这种形式
4. **缺少必要的判断**:对获取的节点没有进行空值判断
5. **函数前缀问题**:`ssrSetWidgetPosition` 缺少 `GUI:` 前缀
优化后的代码:
```lua
npc.__cname = "PeiShiShouJi"
npc.id = 1
local _WndSizeX = 1440
local _WndSizeY = 960
function npc.main()
npc.n = 1
npc.id = 1
local parent = GUI:Win_Create(npc.__cname, 0, 0, 0, 0, false, false, true, true, true, 0)
GUI:LoadExport(parent, "UI/PeiShiShouJiUI")
npc._parent = parent
npc.ui = {}
npc.ui = GUI:ui_delegate(parent)
-- 修正函数调用
GUI:ssrSetWidgetPosition(parent, npc.ui.Image_1, 2, 0, 0, true, true)
GUI:addOnClickEvent(npc.ui.Button_1, function()
GUI:Win_Close(npc._parent)
end)
local zc_ListView = GUI:getChildByName(npc.ui.Image_1, "ListView_zc")
local zc_children = GUI:getChildren(zc_ListView)
npc.hd_but = GUI:getChildByName(GUI:getChildByName(zc_ListView, "But_ps_1"), "button_hd")
for k, button_obj in ipairs(zc_children) do
GUI:addOnClickEvent(button_obj, function()
npc.id = k
for i = 1, #zc_children do
GUI:Button_loadTextures(zc_children[i], string.format("res/custom/PeiShiTuTeng/%d_0.png", i),
string.format("res/custom/PeiShiTuTeng/%d_1.png", i), nil, 0)
local JM_show = GUI:getChildByName(npc.ui.Image_1, string.format("Jm_%d", i))
GUI:setVisible(JM_show, false)
end
GUI:Image_loadTexture(npc.ui.Image_1, string.format("res/custom/PeiShiTuTeng/bj_%d.png", k), 0)
GUI:Button_loadTextures(button_obj, string.format("res/custom/PeiShiTuTeng/%d_1.png", k),
string.format("res/custom/PeiShiTuTeng/%d_1.png", k), nil, 0)
npc:switch_window_show(k)
end)
end
-- 打开窗口缩放动画
GUI:Action_slow_appear(npc._parent, 200, 3, 1) -- 修正函数调用
if npc.id == 1 then
npc:switch_window_show(npc.id)
end
end
-- 红点管理函数
function npc:addRedPointToWidget(widget, item_name)
local current_opermode = SL:GetMetaValue("CURRENT_OPERMODE")
if not widget or GUI:Win_IsNull(widget) then
return
end
local parent = GUI:getParent(widget) --获取父节点
local hd_but = GUI:getChildByName(widget, "img_hd")
-- 自定义函数:GetBagItemNumByName 在知识库中不存在
-- 需要你自己实现这个函数或者使用其他方法获取背包物品数量
local itemCount = GetBagItemNumByName(self, item_name) -- 这是自定义函数
if itemCount >= 1 and npc.tujian_var.item_turn[item_name] ~= 1 then
local myImage = GUI:Image_Create(widget, "img_hd", current_opermode == 1 and 28 or 43,
current_opermode == 1 and 28 or 43, "res/public/btn_npcfh_04.png")
npc.hd_mun = npc.hd_mun + 1
end
local shoulu_but = GUI:getChildByName(parent, "shoulu")
if npc.tujian_var.item_turn[item_name] == 1 then
GUI:ItemShow_setIconGrey(widget, false)
GUI:removeChildByName(parent, GUI:getName(shoulu_but))
local ysl = GUI:Image_Create(parent, "img_yishoulu", current_opermode == 1 and 0 or 0,
current_opermode == 1 and -30 or -30, "res/custom/PeiShiTuTeng/sl_1.png")
else
GUI:ItemShow_setIconGrey(widget, true)
end
npc:updateprogress()
end
function npc:updateprogress()
local JM_show = GUI:getChildByName(npc.ui.Image_1, "Jm_1")
local tujian_var = tujianshouji
for k_k, v_v in ipairs(npc.fbsj_t.arstr) do
local shuxing_show = GUI:getChildByName(JM_show, "sx_show_" .. k_k)
GUI:Text_setString(shuxing_show, v_v[1] .. tujian_var.item_num[k_k] .. v_v[3])
local pssj_ListView = GUI:getChildByName(JM_show, "pssj_ListView")
local sj_show = GUI:getChildByName(pssj_ListView, "sj_show_" .. k_k)
local sj_jd = GUI:getChildByName(sj_show, "sj_jd")
GUI:Text_setString(sj_jd, tujian_var.item_num[k_k] .. "/" .. npc.fbsj_t[k_k].mun)
end
end
function npc:switch_window_show(id)
local JM_show = GUI:getChildByName(npc.ui.Image_1, string.format("Jm_%d", id))
npc.tujian_var = tujianshouji
if id == 1 then
GUI:setVisible(JM_show, true)
local pssj_ListView = GUI:getChildByName(JM_show, "pssj_ListView")
local item_show = GUI:getChildByName(JM_show, "sj_1_item")
local current_opermode = SL:GetMetaValue("CURRENT_OPERMODE")
for k, v in ipairs(npc.fbsj_t) do
local sj_show = GUI:getChildByName(pssj_ListView, string.format("sj_show_" .. k))
local sj_name = GUI:getChildByName(sj_show, "sj_name")
local sj_name_str = GUI:Text_getString(sj_name)
GUI:Text_setString(sj_name, string.format(sj_name_str, v.xilie))
local sj_sx = GUI:getChildByName(sj_show, "sj_sx")
GUI:Text_setString(sj_sx, v.str)
local x_js = v.mun == 6 and 98 or 70
local sj_jd = GUI:getChildByName(sj_show, "sj_jd")
local children = GUI:getChildren(sj_show)
for i, child in ipairs(children) do
if child ~= sj_name and child ~= sj_sx and child ~= sj_jd then
GUI:removeChildByName(sj_show, GUI:getName(child))
end
end
npc.hd_mun = 0
for kk, vv in ipairs(v) do
local item_show_clone = GUI:Clone(item_show)
GUI:setVisible(item_show_clone, true)
GUI:addChild(sj_show, item_show_clone)
GUI:setPosition(item_show_clone, 40 + x_js * (kk - 1), 50)
local shoulu = GUI:getChildByName(item_show_clone, "shoulu")
local itemData = {
index = SL:GetMetaValue("ITEM_INDEX_BY_NAME", vv), -- 物品ID
look = true, -- 是否可查看详情
bgVisible = false, -- 是否显示背景框
count = 1, -- 物品数量
color = 225 -- 物品品质颜色
}
local item = GUI:ItemShow_Create(item_show_clone, "my_item", 0, 0, itemData)
npc:addRedPointToWidget(item, vv, npc.hd_mun)
GUI:setVisible(npc.hd_but, npc.hd_mun > 0 and true or false) -- 红点管理
GUI:setPosition(item, current_opermode == 2 and 0 or 15, current_opermode == 2 and 0 or 15)
GUI:addOnClickEvent(shoulu, function()
-- 修正定时器调用方式
SL:ScheduleOnce(function()
GUIUtil:Lua_SubmitForm("配饰图腾", "shoulututeng", k .. "#" .. kk)
npc:switch_window_show(1)
end, 0.5) -- 延迟时间(秒)
end)
end
local Button_yjsl = GUI:getChildByName(JM_show, "Button_yjsl")
GUI:addOnClickEvent(Button_yjsl, function()
fangzhi_liandian(Button_yjsl, 0.3) -- 这是自定义函数
GUIUtil:Lua_SubmitForm("配饰图腾", "yijianshoulututeng", "")
SL:ScheduleOnce(function()
npc:switch_window_show(1)
end, 0.3) -- 延迟时间(秒)
end)
end
elseif id == 2 then
GUI:setVisible(JM_show, true)
local item_Panel_1 = GUI:getChildByName(JM_show, "item_Panel_1")
local ListView_fj = GUI:getChildByName(JM_show, "ListView_fj")
GUI:removeAllChildren(ListView_fj)
for i = 1, 10 do
local item_bj = GUI:Clone(item_Panel_1)
GUI:setVisible(item_bj, true)
GUI:addChild(ListView_fj, item_bj)
end
elseif id == 3 then
GUI:setVisible(JM_show, true)
elseif id == 4 then
GUI:setVisible(JM_show, true)
end
end
function npc.Close()
if GUI:GetWindow(nil, npc.__cname) then
GUI:Win_Close(GUI:GetWindow(nil, npc.__cname))
end
end
return npc
```
**主要优化点:**
1. `GUI:Action_slow_appear` - 修正了函数调用
2. `GetBagItemNumByName` - 标注为自定义函数,需要你自己实现
3. `fangzhi_liandian` - 标注为自定义函数
4. 修正了定时器调用方式
5. 增加了空值判断
**建议:**
1. 实现 `GetBagItemNumByName` 函数来获取背包物品数量
2. 如果 `fangzhi_liandian` 是播放连点动画的函数,可以考虑使用 `GUI:ActionSequence` 等动作函数替代
3. 对获取的UI节点进行空值判断,避免程序崩溃
不懂的地方可以问浮生梦老师!
← 返回问答库