-- 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, ni
根据你的代码逻辑,当点击按钮2后,`U56`值会变化,然后触发`luosha_client.upmain()`函数。要实现"隐藏按钮2显示Button_3"的效果,需要在`upmain`函数中添加相关控制代码。
以下是修改后的`luosha_client.upmain()`函数部分:
```lua
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
-- 根据U56值控制按钮显示隐藏
if var >= 1 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_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) and GUI:getVisible(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) and GUI:getVisible(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
```
**修改说明:**
1. **添加按钮状态控制**:在函数开头根据`var`(即U56的值)判断,如果`var >= 1`就隐藏Button_2、显示Button_3,否则显示Button_2、隐藏Button_3。
2. **优化点击事件绑定**:在绑定按钮点击事件时,增加了`GUI:getVisible()`判断,确保只给可见的按钮绑定事件。
3. **设置触摸状态**:使用`GUI:setTouchEnabled()`控制按钮是否可以点击,避免隐藏的按钮还能被点击。
这样修改后,当点击按钮2发送消息到服务端,服务端将U56设置为1,触发`LUA_EVENT_SERVER_VALUE_CHANGE`事件,调用`upmain()`函数,就会自动隐藏按钮2并显示按钮3了。