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 {}
🤖 回答引擎: 前端Lua |
🕒 时间: 2026-03-20 18:59:06 |
👁️ 阅读: 22
根据浮生梦老师前端课程内容,我来帮你修改Button_2的点击事件逻辑,实现你描述的等级和魔力值条件判断。
```lua
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 currentLevel = tonumber(SL:GetMetaValue("SERVER_VALUE", "U44")) or 0
local currentMana = tonumber(SL:GetMetaValue("SERVER_VALUE", "U45")) or 0
SL:Print("当前等级U44:", currentLevel, " 当前魔力值U45:", currentMana)
-- 定义条件映射表
local conditionMap = {
[10] = {mana = 1000, id = 50834, hideEffect = true, showItem3 = true, showItem4 = true},
[20] = {mana = 3000, id = 50835},
[30] = {mana = 4000, id = 50836},
[40] = {mana = 8000, id = 50837},
[50] = {mana = 8000, id = 50838},
[60] = {mana = 16000, id = 50839},
[70] = {mana = 32000, id = 50840},
[80] = {mana = 128000, id = 50841},
[90] = {mana = 256000, id = 50842},
[100] = {mana = 512000, id = 50843}
}
-- 检查是否满足条件
local targetLevel = nil
local targetCondition = nil
-- 从高到低检查等级条件
for level = 100, 10, -10 do
if currentLevel >= level and conditionMap[level] then
if currentMana >= conditionMap[level].mana then
targetLevel = level
targetCondition = conditionMap[level]
break
end
end
end
if targetLevel then
SL:Print("满足条件: 等级≥" .. targetLevel .. ", 魔力值≥" .. targetCondition.mana)
-- 处理Effect_1的显示/隐藏
if targetCondition.hideEffect and GUI:Win_IsNotNull(ui["Effect_1"]) then
GUI:setVisible(ui["Effect_1"], false)
SL:Print("隐藏Effect_1")
end
-- 处理ItemShow_3和ItemShow_4的显示
if targetCondition.showItem3 and GUI:Win_IsNotNull(ui.itemroom) then
-- 显示ItemShow_3
GUI:ItemShow_Create(ui.itemroom, "ItemShow_3", itemX2, itemY2, {
index = targetCondition.id,
count = 1,
look = true,
bgVisible = false
})
-- 显示ItemShow_4(假设需要)
if targetCondition.showItem4 then
GUI:ItemShow_Create(ui.itemroom, "ItemShow_4", itemX2 + 50, itemY2, {
index = targetCondition.id + 1, -- 假设ItemShow_4的id是ItemShow_3的id+1
count = 1,
look = true,
bgVisible = false
})
end
end
-- 发送网络消息
local msg = {}
msg.Script = moshen.param1
msg.TargetLevel = targetLevel
msg.TargetID = targetCondition.id
local jsonstr = SL:JsonEncode(msg)
SL:SendLuaNetMsg(58003, moshen.param2, 1, 0, jsonstr)
else
-- 不满足条件
local minLevel = 10
local minMana = conditionMap[minLevel].mana
lib:msg(string.format("条件不足!需要等级≥%d且魔力值≥%d", minLevel, minMana))
return
end
end)
end
```
**修改说明:**
1. **条件判断逻辑**:
- 在点击Button_2时获取当前的U44(等级)和U45(魔力值)
- 使用条件映射表`conditionMap`存储每个等级段的要求
2. **等级检查顺序**:
- 从高等级(100级)向低等级(10级)检查
- 这样会优先匹配最高满足的条件
3. **特殊处理**:
- 当等级≥10且魔力值≥1000时,会隐藏Effect_1并显示ItemShow_3和ItemShow_4
- 其他等级段只更换对应的物品ID
4. **网络消息增强**:
- 在发送的消息中添加了`TargetLevel`和`TargetID`字段
- 服务端可以根据这些信息处理对应的逻辑
**注意事项:**
- `itemX2`和`itemY2`需要根据你的UI布局定义坐标
- `ItemShow_4`的ID我假设为`ItemShow_3`的ID+1,你需要根据实际情况调整
- 如果Effect_1不存在,代码会安全跳过
这样修改后,点击Button_2时会根据当前等级和魔力值执行不同的操作,符合你的需求。
根据浮生梦老师前端课程内容,我来帮你修改Button_2的点击事件逻辑,实现你描述的等级和魔力值条件判断。
```lua
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 currentLevel = tonumber(SL:GetMetaValue("SERVER_VALUE", "U44")) or 0
local currentMana = tonumber(SL:GetMetaValue("SERVER_VALUE", "U45")) or 0
SL:Print("当前等级U44:", currentLevel, " 当前魔力值U45:", currentMana)
-- 定义条件映射表
local conditionMap = {
[10] = {mana = 1000, id = 50834, hideEffect = true, showItem3 = true, showItem4 = true},
[20] = {mana = 3000, id = 50835},
[30] = {mana = 4000, id = 50836},
[40] = {mana = 8000, id = 50837},
[50] = {mana = 8000, id = 50838},
[60] = {mana = 16000, id = 50839},
[70] = {mana = 32000, id = 50840},
[80] = {mana = 128000, id = 50841},
[90] = {mana = 256000, id = 50842},
[100] = {mana = 512000, id = 50843}
}
-- 检查是否满足条件
local targetLevel = nil
local targetCondition = nil
-- 从高到低检查等级条件
for level = 100, 10, -10 do
if currentLevel >= level and conditionMap[level] then
if currentMana >= conditionMap[level].mana then
targetLevel = level
targetCondition = conditionMap[level]
break
end
end
end
if targetLevel then
SL:Print("满足条件: 等级≥" .. targetLevel .. ", 魔力值≥" .. targetCondition.mana)
-- 处理Effect_1的显示/隐藏
if targetCondition.hideEffect and GUI:Win_IsNotNull(ui["Effect_1"]) then
GUI:setVisible(ui["Effect_1"], false)
SL:Print("隐藏Effect_1")
end
-- 处理ItemShow_3和ItemShow_4的显示
if targetCondition.showItem3 and GUI:Win_IsNotNull(ui.itemroom) then
-- 显示ItemShow_3
GUI:ItemShow_Create(ui.itemroom, "ItemShow_3", itemX2, itemY2, {
index = targetCondition.id,
count = 1,
look = true,
bgVisible = false
})
-- 显示ItemShow_4(假设需要)
if targetCondition.showItem4 then
GUI:ItemShow_Create(ui.itemroom, "ItemShow_4", itemX2 + 50, itemY2, {
index = targetCondition.id + 1, -- 假设ItemShow_4的id是ItemShow_3的id+1
count = 1,
look = true,
bgVisible = false
})
end
end
-- 发送网络消息
local msg = {}
msg.Script = moshen.param1
msg.TargetLevel = targetLevel
msg.TargetID = targetCondition.id
local jsonstr = SL:JsonEncode(msg)
SL:SendLuaNetMsg(58003, moshen.param2, 1, 0, jsonstr)
else
-- 不满足条件
local minLevel = 10
local minMana = conditionMap[minLevel].mana
lib:msg(string.format("条件不足!需要等级≥%d且魔力值≥%d", minLevel, minMana))
return
end
end)
end
```
**修改说明:**
1. **条件判断逻辑**:
- 在点击Button_2时获取当前的U44(等级)和U45(魔力值)
- 使用条件映射表`conditionMap`存储每个等级段的要求
2. **等级检查顺序**:
- 从高等级(100级)向低等级(10级)检查
- 这样会优先匹配最高满足的条件
3. **特殊处理**:
- 当等级≥10且魔力值≥1000时,会隐藏Effect_1并显示ItemShow_3和ItemShow_4
- 其他等级段只更换对应的物品ID
4. **网络消息增强**:
- 在发送的消息中添加了`TargetLevel`和`TargetID`字段
- 服务端可以根据这些信息处理对应的逻辑
**注意事项:**
- `itemX2`和`itemY2`需要根据你的UI布局定义坐标
- `ItemShow_4`的ID我假设为`ItemShow_3`的ID+1,你需要根据实际情况调整
- 如果Effect_1不存在,代码会安全跳过
这样修改后,点击Button_2时会根据当前等级和魔力值执行不同的操作,符合你的需求。
← 返回问答库