local moshen = {}
function moshen.main(param1, param2, param3, param4)
lib:Require(param1, function(state)
if state then
if param1 then
moshen.parent = lib:openwin(param1, nil, true, param4) or {}
我来帮你分析和修改这段代码。首先让我指出几个需要注意的问题:
## 主要问题:
1. **变量名冲突**:在`upmain()`函数中,`var1`变量被重复定义
2. **函数调用错误**:`GUI:delayTouchEnabled()`函数在浮生梦老师前端课程内容中不存在
3. **逻辑优化**:进度条计算逻辑可以简化
## 修改后的完整代码:
```lua
local moshen = {}
function moshen.main(param1, param2, param3, param4)
lib:Require(param1, function(state)
if state then
if param1 then
moshen.parent = lib:openwin(param1, nil, true, param4) or {}
moshen.param1 = param1
end
if param2 then
moshen.param2 = param2
end
if param3 then
moshen.param3 = param3
end
if moshen.parent.cfg and next(moshen.parent.cfg) then
moshen.upmain()
else
lib:msg("系统未配置!")
GUI:Win_Close(moshen.parent._parent)
end
end
end)
end
function moshen.upmain()
if not moshen.parent or not moshen.parent._ui then
return
end
local ui = moshen.parent._ui
local cfg = moshen.parent.cfg
if not cfg then
return
end
-- 获取服务器变量值
local var = tonumber(SL:GetMetaValue("SERVER_VALUE", "U44")) or 0
SL:Print("获取当前U44值 var =", var)
local var45 = tonumber(SL:GetMetaValue("SERVER_VALUE", "U45")) or 0 -- 修改变量名避免冲突
SL:Print("获取当前U45值 var45 =", var45)
-- 检查是否已满级
if var >= #cfg then
lib:msg("魔神已满级!")
if GUI:Win_IsNotNull(ui["Button_1"]) then
GUI:setTouchEnabled(ui["Button_1"], false)
end
return
end
-- 显示当前等级
if GUI:Win_IsNotNull(ui.Text_1) then
GUI:Text_setString(ui.Text_1, "魔王Lv:" .. (var or 0))
end
-- 设置进度条逻辑
if GUI:Win_IsNotNull(ui["LoadingBar_1"]) then
GUI:LoadingBar_setDirection(ui["LoadingBar_1"], 0)
-- 定义各阶段最大值
local maxValues = {1000, 3000, 4000, 8000, 16000, 32000, 64000, 128000, 256000, 512000}
local currentMax = 1000
local currentStageValue = var45
local currentStage = 1
-- 确定当前阶段
for i, value in ipairs(maxValues) do
if var45 >= value then
if i < #maxValues then
currentStage = i + 1
else
currentStage = i
end
else
currentStage = i
currentMax = value
if i > 1 then
currentStageValue = var45 - maxValues[i-1]
end
break
end
end
-- 计算百分比
local percent = 0
if currentMax > 0 then
percent = (currentStageValue / currentMax) * 100
if percent > 100 then
percent = 100
end
end
-- 设置进度条
GUI:LoadingBar_setPercent(ui["LoadingBar_1"], percent)
-- 显示进度文字
if GUI:Win_IsNotNull(ui["Text_Progress"]) then
GUI:Text_setString(ui["Text_Progress"], string.format("%d/%d", currentStageValue, currentMax))
end
SL:Print(string.format("进度条: var45=%d, 阶段值=%d, 最大值=%d, 百分比=%.1f%%",
var45, currentStageValue, currentMax, percent))
end
-- 显示下一级属性
local next_level = var + 1
local next_cfg = cfg[next_level]
if GUI:Win_IsNotNull(ui.Text_2) and next_cfg then
GUI:Text_setString(ui.Text_2, "+" .. (next_cfg.cur_attr_3 or 0))
end
if GUI:Win_IsNotNull(ui.Text_3) and next_cfg then
GUI:Text_setString(ui.Text_3, "+" .. (next_cfg.cur_attr_1 or 0))
end
if GUI:Win_IsNotNull(ui.Text_4) and next_cfg then
GUI:Text_setString(ui.Text_4, "+" .. (next_cfg.cur_attr_9 or 0))
end
-- 控制Effect显示逻辑
for i = 12, 21 do
local effectName = "Effect_" .. i
if GUI:Win_IsNotNull(ui[effectName]) then
GUI:setVisible(ui[effectName], false)
end
end
if var > 0 then
local showCount = var % 10
if showCount == 0 then
showCount = 10
end
for i = 1, showCount do
local effectIndex = 11 + i
local effectName = "Effect_" .. effectIndex
if GUI:Win_IsNotNull(ui[effectName]) then
GUI:setVisible(ui[effectName], true)
end
end
SL:Print("var=" .. var .. ",显示" .. showCount .. "个Effect")
end
-- 控制Effect_1显示
local isMobile = SL:GetMetaValue("CURRENT_OPERMODE") or 1
if GUI:Win_IsNotNull(ui.Effect_1) then
local u44 = tonumber(SL:GetMetaValue("SERVER_VALUE", "U44")) or 0
local u45 = tonumber(SL:GetMetaValue("SERVER_VALUE", "U45")) or 0
local showEffect1 = true
if u44 >= 10 and u45 >= 1000 then
showEffect1 = false
end
GUI:setVisible(ui.Effect_1, showEffect1)
end
-- 根据等级和魔力值获取物品ID
local function getItemIdByLevel(level, magicValue)
local item_id = 50834
if level >= 100 and magicValue >= 512000 then
item_id = 50843
elseif level >= 90 and magicValue >= 256000 then
item_id = 50842
elseif level >= 80 and magicValue >= 128000 then
item_id = 50841
elseif level >= 70 and magicValue >= 32000 then
item_id = 50840
elseif level >= 60 and magicValue >= 16000 then
item_id = 50839
elseif level >= 50 and magicValue >= 8000 then
item_id = 50838
elseif level >= 40 and magicValue >= 8000 then
item_id = 50837
elseif level >= 30 and magicValue >= 4000 then
item_id = 50836
elseif level >= 20 and magicValue >= 3000 then
item_id = 50835
elseif level >= 10 and magicValue >= 1000 then
item_id = 50834
end
return item_id
end
-- 显示物品需求
if GUI:Win_IsNotNull(ui.itemroom) and next_cfg then
GUI:removeAllChildren(ui.itemroom)
local item_name = next_cfg.name1 or "化魔石"
local itemCount = tonumber(SL:GetMetaValue("ITEM_COUNT", item_name)) or 0
local needItemCount = tonumber(next_cfg.value1) or 0
local goldCount = tonumber(SL:GetMetaValue("MONEY", 2)) or 0
local needGold = tonumber(next_cfg.value2) or 0
local item_id = 0
if item_name == "化魔石" then
item_id = 11549
else
item_id = lib:getitemidx(item_name) or 11549
end
local money_id = 2
local function safe_lt(a, b)
local num_a = tonumber(a) or 0
local num_b = tonumber(b) or 0
return num_a < num_b
end
local itemColor = 255
if safe_lt(itemCount, needItemCount) then
itemColor = 58
end
local goldColor = 255
if safe_lt(goldCount, needGold) then
goldColor = 58
end
local goldX, goldY = 280, 90
local itemX, itemY = 193, 90
local itemX2, itemY2 = 333, 240
if isMobile == 2 then
goldX, goldY = 84, 90
itemX, itemY = 20, 90
itemX2, itemY2 = -455, 280
end
-- 创建物品显示
GUI:ItemShow_Create(ui.itemroom, "ItemShow_1", itemX, itemY, {
index = item_id,
count = needItemCount,
look = true,
bgVisible = false,
color = itemColor
})
GUI:ItemShow_Create(ui.itemroom, "ItemShow_2", goldX, goldY, {
index = money_id,
count = needGold,
look = true,
bgVisible = false,
color = goldColor
})
if GUI:Win_IsNotNull(ui.itemroom) then
local u44 = tonumber(SL:GetMetaValue("SERVER_VALUE", "U44")) or 0
local u45 = tonumber(SL:GetMetaValue("SERVER_VALUE", "U45")) or 0
local current_item_id = getItemIdByLevel(u44, u45)
GUI:ItemShow_Create(ui.itemroom, "ItemShow_3", itemX2, itemY2, {
index = current_item_id,
count = 1,
look = true,
bgVisible = false,
})
end
end
-- 按钮事件处理
if GUI:Win_IsNotNull(ui.Button_1) then
GUI:addOnClickEvent(ui.Button_1, function()
SL:Print("点击了升级按钮")
-- 注意:GUI:delayTouchEnabled()函数不存在,建议使用以下方式
GUI:setTouchEnabled(ui.Button_1, false)
SL:ScheduleOnce(function()
GUI:setTouchEnabled(ui.Button_1, true)
end, 0.2)
local msg = {}
msg.Script = moshen.param1
local jsonstr = SL:JsonEncode(msg)
SL:SendLuaNetMsg(58003, moshen.param2, 0, 0, jsonstr)
end)
end
if GUI:Win_IsNotNull(ui.Button_2) then
GUI:addOnClickEvent(ui.Button_2, function()
SL:Print("点击了Button2")
GUI:setTouchEnabled(ui.Button_2, false)
SL:ScheduleOnce(function()
GUI:setTouchEnabled(ui.Button_2, true)
end, 0.2)
local u44 = tonumber(SL:GetMetaValue("SERVER_VALUE", "U44")) or 0
local u45 = tonumber(SL:GetMetaValue("SERVER_VALUE", "U45")) or 0
if u44 < 10 or u45 < 1000 then
lib:msg("条件不满足:需要等级≥10且魔力值≥1000")
return
end
local new_item_id = getItemIdByLevel(u44, u45)
-- 更新物品显示
if GUI:Win_IsNotNull(ui.ItemShow_3) then
GUI:ItemShow_SetData(ui.ItemShow_3, {
index = new_item_id,
count = 1
})
end
if GUI:Win_IsNotNull(ui.ItemShow_4) then
GUI:ItemShow_SetData(ui.ItemShow_4, {
index = new_item_id,
count = 1
})
end
-- 隐藏Effect_1
if GUI:Win_IsNotNull(ui.Effect_1) then
GUI:setVisible(ui.Effect_1, false)
end
lib:msg("成功更新物品ID为:" .. new_item_id)
local msg = {}
msg.Script = moshen.param1
local jsonstr = SL:JsonEncode(msg)
SL:SendLuaNetMsg(58003, moshen.param1, 0, 0, jsonstr)
end)
end
end
-- 注册游戏事件
SL:RegisterLUAEvent(LUA_EVENT_PLAYER_EQUIP_CHANGE, "moshen", function(data)
if data and data.opera == 1 then
moshen.upmain()
end
end)
SL:RegisterLUAEvent(LUA_EVENT_SERVER_VALUE_CHANGE, "moshen", function(data)
if data and (data.key == "U44" or data.key == "U45") then
moshen.upmain()
end
end)
SL:RegisterLUAEvent(LUA_EVENT_CLOSEWIN, "moshen", function(CLOSEWINdata)
if CLOSEWINdata and CLOSEWINdata == "moshen" then
SL:UnRegisterLUAEvent(LUA_EVENT_PLAYER_EQUIP_CHANGE, "moshen")
SL:UnRegisterLUAEvent(LUA_EVENT_SERVER_VALUE_CHANGE, "moshen")
SL:UnRegisterLUAEvent(LUA_EVENT_CLOSEWIN, "moshen")
end
end)
return moshen
```
## 主要修改点:
1. **变量名修复**:将重复的`var1`改为`var45`,避免冲突
2. **按钮防重复点击**:使用`SL:ScheduleOnce`替代不存在的`GUI:delayTouchEnabled()`
3. **代码优化**:简化了进度条计算逻辑
4. **增加安全性检查**:在访问`next_cfg`前增加判断
注意:浮生梦老师前端课程中确实没有`GUI:delayTouchEnabled()`这个函数,所以我用`SL:ScheduleOnce`配合`GUI:setTouchEnabled()`实现了相同的功能。
如果还有其他问题,随时问我!