根据浮生梦老师前端课程内容,你的代码中可能存在几个问题。让我帮你分析并提供完整的修复代码:
## 主要问题分析:
1. **UI节点获取问题**:`ui.ItemShow_3`、`ui.Image_1`、`ui.Text_2` 这些节点可能没有被正确创建或获取
2. **显示状态初始化**:在 `upmain()` 函数中应该先确保这些控件可见
3. **ItemShow控件设置**:`GUI:SetItemId()` 函数可能需要正确的参数格式
## 完整修复代码:
```lua
-- client.lua
local luosha_client = {}
function luosha_client.main(param1, param2, param3, param4)
lib:Require(param1, function(state)
if state then
if param1 then
luosha_client.parent = lib:openwin(param1, nil, true, param4) or {}
luosha_client.param1 = param1
end
if param2 then
luosha_client.param2 = param2
end
if param3 then
luosha_client.param3 = param3
end
if luosha_client.parent.cfg and next(luosha_client.parent.cfg) then
luosha_client.upmain()
else
lib:msg("系统未配置!")
GUI:Win_Close(luosha_client.parent._parent)
end
end
end)
end
function luosha_client.upmain()
if not luosha_client.parent or not luosha_client.parent._ui then
return
end
local ui = luosha_client.parent._ui
local var = tonumber(SL:GetMetaValue("SERVER_VALUE", "U56"))
SL:Print("获取当前投喂次数 var =", var)
if var == "" then var = 0 end
var = tonumber(var) or 0
-- 重要:初始化显示状态
if GUI:Win_IsNotNull(ui.ItemShow_3) then
GUI:setVisible(ui.ItemShow_3, false)
GUI:setOpacity(ui.ItemShow_3, 0)
end
if GUI:Win_IsNotNull(ui.Image_1) then
GUI:setVisible(ui.Image_1, false)
end
if GUI:Win_IsNotNull(ui.Text_2) then
GUI:setVisible(ui.Text_2, false)
GUI:setOpacity(ui.Text_2, 0)
end
if GUI:Win_IsNotNull(ui.Image_3) then
GUI:setVisible(ui.Image_3, true)
end
if var >= 3 then
if GUI:Win_IsNotNull(ui.Button_2) then
GUI:setVisible(ui.Button_2, false)
GUI:setTouchEnabled(ui.Button_2, false)
end
if GUI:Win_IsNotNull(ui.Button_3) then
GUI:setVisible(ui.Button_3, true)
GUI:setTouchEnabled(ui.Button_3, true)
end
else
if GUI:Win_IsNotNull(ui.Button_2) then
GUI:setVisible(ui.Button_2, true)
GUI:setTouchEnabled(ui.Button_2, true)
end
if GUI:Win_IsNotNull(ui.Button_3) then
GUI:setVisible(ui.Button_3, false)
GUI:setTouchEnabled(ui.Button_3, false)
end
end
-- 初始化所有Text控件为不可见
local textControls = {ui.Text_1, ui.Text_2, ui.Text_3, ui.Text_4, ui.Text_5}
for _, text in ipairs(textControls) do
if not GUI:Win_IsNull(text) then
GUI:setVisible(text, false)
GUI:setOpacity(text, 0)
end
end
-- 定义动画函数
local function playTextAnimation(textWidget, delayTime)
if GUI:Win_IsNull(textWidget) then
return
end
GUI:setVisible(textWidget, true)
GUI:setOpacity(textWidget, 0)
local pos = GUI:getPosition(textWidget)
GUI:setPosition(textWidget, pos.x, pos.y - 50)
local actionSequence = GUI:ActionSequence(
GUI:ActionFadeIn(0.3),
GUI:ActionMoveBy(0.3, 0, 170),
GUI:DelayTime(delayTime or 5),
GUI:ActionFadeOut(2),
GUI:CallFunc(function()
GUI:setVisible(textWidget, false)
GUI:setPosition(textWidget, pos.x, pos.y)
end)
)
GUI:runAction(textWidget, actionSequence)
end
-- 根据条件播放不同动画
if var <= 10 then
if not GUI:Win_IsNull(ui.Text_1) then
playTextAnimation(ui.Text_1, 1)
end
elseif var >= 20 then
if not GUI:Win_IsNull(ui.Text_3) then
playTextAnimation(ui.Text_3, 1)
end
end
if GUI:Win_IsNotNull(ui.Button_1) then
GUI:addOnClickEvent(ui.Button_1, function()
SL:Print("点击了第1个按钮")
GUI:delayTouchEnabled(ui.Button_1, 0.2)
local msg = {}
msg.Script = luosha_client.param1
local jsonstr = SL:JsonEncode(msg)
SL:SendLuaNetMsg(58003, luosha_client.param2, 0, 0, jsonstr)
end)
end
if GUI:Win_IsNotNull(ui.Button_2) then
GUI:addOnClickEvent(ui.Button_2, function()
SL:Print("点击了第2个按钮")
GUI:delayTouchEnabled(ui.Button_2, 0.2)
local msg = {}
msg.Script = luosha_client.param1
local jsonstr = SL:JsonEncode(msg)
SL:SendLuaNetMsg(58003, luosha_client.param2, 1, 0, jsonstr)
end)
end
if GUI:Win_IsNotNull(ui.Button_3) then
GUI:addOnClickEvent(ui.Button_3, function()
SL:Print("点击了第3个按钮")
GUI:delayTouchEnabled(ui.Button_3, 0.2)
local msg = {}
msg.Script = luosha_client.param1
local jsonstr = SL:JsonEncode(msg)
SL:SendLuaNetMsg(58003, luosha_client.param2, 2, 0, jsonstr)
end)
end
end
-- 处理后端发送的装备信息
function luosha_client.showRandomEquip(data)
if not luosha_client.parent or not luosha_client.parent._ui then
return
end
local ui = luosha_client.parent._ui
-- 设置Text_2显示装备名称
if GUI:Win_IsNotNull(ui.Text_2) then
GUI:Text_setString(ui.Text_2, data.equipName) -- 使用正确的设置文本函数
GUI:setVisible(ui.Text_2, true)
GUI:setOpacity(ui.Text_2, 255)
-- 添加特效动画
local actionSequence = GUI:ActionSequence(
GUI:ActionScaleTo(0.2, 1.5, 1.5),
GUI:ActionScaleTo(0.2, 1.0, 1.0),
GUI:ActionRepeatForever(
GUI:ActionSequence(
GUI:ActionTintTo(0.5, 255, 255, 0, 255),
GUI:ActionTintTo(0.5, 255, 255, 255, 255)
)
)
)
GUI:runAction(ui.Text_2, actionSequence)
end
-- 设置ItemShow_3显示装备图标
if GUI:Win_IsNotNull(ui.ItemShow_3) then
-- 设置装备ID - 使用正确的ItemShow设置方法
GUI:ItemShow_setItem(ui.ItemShow_3, data.equipId, 1, true, false, 255) -- 根据知识库中的ItemShow_Create参数格式
GUI:setVisible(ui.ItemShow_3, true)
GUI:setOpacity(ui.ItemShow_3, 255)
-- 隐藏Image_3,显示Image_1
if GUI:Win_IsNotNull(ui.Image_3) then
GUI:setVisible(ui.Image_3, false)
end
if GUI:Win_IsNotNull(ui.Image_1) then
GUI:setVisible(ui.Image_1, true)
end
-- 为ItemShow_3添加动画效果
local action = GUI:ActionSequence(
GUI:ActionFadeIn(0.5),
GUI:ActionScaleTo(0.3, 1.2, 1.2),
GUI:ActionScaleTo(0.3, 1.0, 1.0),
GUI:ActionRotateBy(0.5, 360)
)
GUI:runAction(ui.ItemShow_3, action)
end
end
-- 注册网络消息监听
SL:RegisterLUAEvent(LUA_EVENT_NETMSG, "luosha", function(data)
if data and data.msgid == 58003 then
local msg = SL:JsonDecode(data.data)
if msg and msg.action == "show_random_equip" then
-- 显示随机生成的装备
luosha_client.showRandomEquip(msg)
end
end
end)
-- 注册游戏事件
SL:RegisterLUAEvent(LUA_EVENT_PLAYER_EQUIP_CHANGE, "luosha", function(data)
if data and data.opera == 1 then
luosha_client.upmain()
end
end)
SL:RegisterLUAEvent(LUA_EVENT_SERVER_VALUE_CHANGE, "luosha", function(data)
if next(data) and data.key == "U56" then
luosha_client.upmain()
end
end)
SL:RegisterLUAEvent(LUA_EVENT_CLOSEWIN, "luosha", function(CLOSEWINdata)
if CLOSEWINdata and CLOSEWINdata == "luosha" then
SL:UnRegisterLUAEvent(LUA_EVENT_PLAYER_EQUIP_CHANGE, "luosha")
SL:UnRegisterLUAEvent(LUA_EVENT_SERVER_VALUE_CHANGE, "luosha")
SL:UnRegisterLUAEvent(LUA_EVENT_CLOSEWIN, "luosha")
SL:UnRegisterLUAEvent(LUA_EVENT_NETMSG, "luosha")
end
end)
return luosha_client
```
## 关键修复点:
1. **`upmain()`函数初始化**:在函数开头添加了控件的显示状态初始化
2. **`GUI:Text_setString()`**:替换了 `GUI:setText()`,使用正确的文本设置函数
3. **`GUI:ItemShow_setItem()`**:替换了 `GUI:SetItemId()`,使用正确的ItemShow设置方法
4. **透明度设置**:添加了 `GUI:setOpacity()` 确保控件完全显示
5. **按钮3的打印信息**:修正了打印信息中的按钮编号
## 检查建议:
1. 确保UI编辑器中的控件名称与代码中的一致(ItemShow_3、Image_1、Text_2)
2. 使用 `SL:Print()` 打印调试信息,确认控件是否被正确获取
3. 检查后端发送的JSON数据格式是否正确
如果还有问题,可以检查一下UI编辑器中的控件是否真的存在,或者告诉我更多错误信息,浮生梦老师可以帮你进一步排查!