local isMobile = SL:GetMetaValue("CURRENT_OPERMODE") or 1
-- 显示物品需求
if GUI:Win_IsNotNull(ui.itemroom) and next_cfg then
GUI:removeAllChildren(ui.itemroom)
-- 焚天系统物品需求显示
local item_name = next_cfg.name1 or "化魔石"
根据你的需求,我来帮你实现完整的代码逻辑:
```lua
local isMobile = SL:GetMetaValue("CURRENT_OPERMODE") or 1
-- 显示物品需求
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
-- 获取物品ID
local item_id = 0
if item_name == "化魔石" then
item_id = 11549 -- 神源之石的物品ID
else
item_id = lib:getitemidx(item_name) or 11549
end
-- 获取货币ID
local money_id = 2 -- 金币的货币ID
-- 安全比较函数
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
})
-- 获取玩家等级和魔力值
local playerLevel = tonumber(SL:GetMetaValue("PLAYER_LEVEL")) or 0
local magicValue = tonumber(SL:GetMetaValue("MAGIC_VALUE")) or 0
-- 默认gemid
local default_gemid = 50834
-- 判断是否满足条件:等级>=10级且魔力值>=1000
if playerLevel >= 10 and magicValue >= 1000 then
-- 隐藏Effect_1
if GUI:Win_IsNotNull(ui.Effect_1) then
GUI:setVisible(ui.Effect_1, false)
end
-- 显示ItemShow_3
GUI:ItemShow_Create(ui.itemroom, "ItemShow_3", itemX2, itemY2, {
index = default_gemid,
count = 1,
look = true,
bgVisible = false,
})
end
-- Button2点击事件处理函数
local function onButton2Click()
local new_gemid = default_gemid
-- 根据等级和魔力值条件更新gemid
if playerLevel >= 20 and magicValue >= 3000 then
new_gemid = 50835
elseif playerLevel >= 30 and magicValue >= 4000 then
new_gemid = 50836
elseif playerLevel >= 40 and magicValue >= 8000 then
new_gemid = 50837
elseif playerLevel >= 50 and magicValue >= 8000 then
new_gemid = 50838
elseif playerLevel >= 60 and magicValue >= 16000 then
new_gemid = 50839
elseif playerLevel >= 70 and magicValue >= 32000 then
new_gemid = 50840
elseif playerLevel >= 80 and magicValue >= 128000 then
new_gemid = 50841
elseif playerLevel >= 90 and magicValue >= 256000 then
new_gemid = 50842
elseif playerLevel >= 100 and magicValue >= 512000 then
new_gemid = 50843
end
-- 更新ItemShow_3的显示
if GUI:Win_IsNotNull(ui.itemroom) then
-- 先移除旧的ItemShow_3
local oldItemShow = GUI:getChildByName(ui.itemroom, "ItemShow_3")
if GUI:Win_IsNotNull(oldItemShow) then
GUI:removeFromParent(oldItemShow)
end
-- 创建新的ItemShow_3
GUI:ItemShow_Create(ui.itemroom, "ItemShow_3", itemX2, itemY2, {
index = new_gemid,
count = 1,
look = true,
bgVisible = false,
})
end
end
-- 为Button2绑定点击事件
if GUI:Win_IsNotNull(ui.Button2) then
GUI:addOnClickEvent(ui.Button2, onButton2Click)
end
end
```
**代码说明:**
1. **条件判断逻辑**:
- 首先获取玩家等级和魔力值
- 当等级≥10且魔力值≥1000时,隐藏Effect_1并显示ItemShow_3
2. **Button2点击事件**:
- 根据不同的等级和魔力值条件,更新ItemShow_3的物品ID
- 每次点击都会重新创建ItemShow_3,确保显示正确的物品
3. **坐标处理**:
- 根据平台类型(PC/手机)设置不同的坐标
- ItemShow_3的位置在itemX2, itemY2坐标处
4. **注意事项**:
- 代码中使用了`SL:GetMetaValue("PLAYER_LEVEL")`获取玩家等级
- 使用了`SL:GetMetaValue("MAGIC_VALUE")`获取魔力值
- 需要确保ui对象中有Button2、Effect_1和itemroom这些控件
如果对获取玩家等级和魔力值的函数不熟悉,可以查看浮生梦老师的前端课程相关内容。