根据浮生梦老师前端课程内容,你的代码已经实现了根据转生等级显示对应按钮和特效的功能。要实现"在哪个地图就显示哪个Effect",需要修改特效显示逻辑,将特效显示与地图关联而不是转生等级。
以下是修改后的代码部分:
```lua
-- 根据当前地图显示对应的特效
local currentMapId = SL:GetMetaValue("MAP_ID") or ""
local currentMapName = SL:GetMetaValue("MAP_NAME") or ""
-- 创建地图与特效的对应关系
-- 这里假设每个大陆的第一个地图对应Effect_1,第二个对应Effect_2,以此类推
local mapToEffectMap = {}
for continentId, continentData in pairs(config) do
for index, mapName in ipairs(continentData.maps) do
-- 计算特效编号(1-9循环)
local effectIndex = ((index - 1) % 9) + 1
mapToEffectMap[mapName] = effectIndex
end
end
-- 初始隐藏所有特效
for i = 1, 9 do
local effect_name = "Effect_" .. i
if GUI:Win_IsNotNull(ui[effect_name]) then
GUI:setVisible(ui[effect_name], false)
end
end
-- 根据当前地图显示对应特效
local targetEffectIndex = nil
-- 先尝试通过地图ID匹配
if currentMapId ~= "" then
targetEffectIndex = mapToEffectMap[currentMapId]
end
-- 如果没找到,再尝试通过地图名称匹配
if not targetEffectIndex and currentMapName ~= "" then
targetEffectIndex = mapToEffectMap[currentMapName]
end
-- 显示对应特效
if targetEffectIndex and targetEffectIndex >= 1 and targetEffectIndex <= 9 then
local effect_name = "Effect_" .. targetEffectIndex
if GUI:Win_IsNotNull(ui[effect_name]) then
GUI:setVisible(ui[effect_name], true)
SL:Print("在地图[" .. (currentMapName ~= "" and currentMapName or currentMapId) .. "]显示特效:" .. effect_name)
end
else
SL:Print("未找到当前地图对应的特效配置")
end
-- 保留原来的转生等级按钮显示逻辑(这部分不变)
for _, requirement in ipairs(levelRequirements) do
if reLevel >= requirement.level then
local button_name = "Button_" .. requirement.button
if GUI:Win_IsNotNull(ui[button_name]) then
GUI:setVisible(ui[button_name], true)
GUI:addOnClickEvent(ui[button_name], function()
SL:Print("点击了第" .. requirement.button .. "个按钮,转生等级:" .. reLevel)
GUI:delayTouchEnabled(ui[button_name], 0.2)
SL:SendLuaNetMsg(58004, 0, requirement.msgType, 0, 0)
GUI:Win_CloseAll()
end)
end
end
end
```
**关键修改说明:**
1. **创建地图-特效映射表**:遍历所有大陆的所有地图,为每个地图分配一个特效编号(1-9循环)
2. **特效显示逻辑**:
- 先通过`MAP_ID`查找对应特效
- 如果没找到,再通过`MAP_NAME`查找
- 找到后显示对应的`Effect_X`
3. **按钮逻辑保持不变**:按钮仍然根据转生等级显示
4. **特效与按钮分离**:现在特效显示只与地图相关,按钮显示只与转生等级相关
**注意事项:**
- 如果地图数量超过9个,特效会循环使用(第10个地图使用Effect_1)
- 需要确保UI中有`Effect_1`到`Effect_9`这9个特效控件
- 如果某个地图没有配置,对应的特效不会显示
这样修改后,你的特效就会根据当前所在的地图来显示了!