if isMobile == 2 then
-- 手机端坐标调整
goldX, goldY = 84, 90
itemX, itemY = 20, 90
itemX2, itemY2 =-455, 280
itemX3, itemY3 =-255, 280
end
GUI:ItemShow_C
🤖 回答引擎: 前端Lua |
🕒 时间: 2026-03-20 19:12:54 |
👁️ 阅读: 18
根据浮生梦老师前端课程内容,我来帮你实现这个功能。首先需要创建界面和按钮,然后根据等级和魔力值条件来控制显示和切换。
```lua
-- 创建主界面
local parent = GUI:Win_Create("GMMain", 0, 0, 0, 0, false, false, true, false, false, false, 1)
-- 创建itemroom节点
local itemroom = GUI:Node_Create(parent, "itemroom", 0, 0)
-- 创建Button2
local button2 = GUI:Button_Create(parent, "Button2", 200, 100, "res/public/1900000612.png")
GUI:Button_setTitleText(button2, "升级宝石")
-- 创建ItemShow_4(不在itemroom中)
local itemShow4 = GUI:ItemShow_Create(parent, "ItemShow_4", 300, 100, {
index = 50834,
count = 1,
look = true,
bgVisible = false
})
-- 初始化变量
local current_gemid = 50834
local isMobile = 2 -- 假设是手机端
-- 根据平台设置坐标
local goldX, goldY = 84, 90
local itemX, itemY = 20, 90
local itemX2, itemY2 = -455, 280
local itemX3, itemY3 = -255, 280
-- 创建ItemShow_1和ItemShow_2
GUI:ItemShow_Create(itemroom, "ItemShow_1", itemX, itemY, {
index = 10001, -- 示例物品ID
count = 10,
look = true,
bgVisible = false,
color = "#ffffff"
})
GUI:ItemShow_Create(itemroom, "ItemShow_2", goldX, goldY, {
index = 10002, -- 示例金币ID
count = 1000,
look = true,
bgVisible = false,
color = "#ffff00"
})
-- 创建ItemShow_3(初始隐藏)
local itemShow3 = GUI:ItemShow_Create(itemroom, "ItemShow_3", itemX2, itemY2, {
index = current_gemid,
count = 1,
look = true,
bgVisible = false
})
GUI:setVisible(itemShow3, false)
-- 创建Effect_1
local effect1 = GUI:Effect_Create(itemroom, "Effect_1", 0, 0, 0, 33791, 0, 0, 0, 1)
-- 检查条件并更新显示的函数
local function checkAndUpdate()
-- 获取等级和魔力值(这里需要根据实际项目获取方式)
local u44 = SL:GetMetaValue("PLAYER_LEVEL", 0) or 0
local magicValue = SL:GetMetaValue("PLAYER_MAGIC", 0) or 0
-- 条件1:等级>=10且魔力值>=1000
if u44 >= 10 and magicValue >= 1000 then
GUI:setVisible(effect1, false)
GUI:setVisible(itemShow3, true)
GUI:setVisible(itemShow4, true)
end
-- 更新宝石ID
local new_gemid = 50834 -- 默认ID
if u44 >= 100 and magicValue >= 512000 then
new_gemid = 50843
elseif u44 >= 90 and magicValue >= 256000 then
new_gemid = 50842
elseif u44 >= 80 and magicValue >= 128000 then
new_gemid = 50841
elseif u44 >= 70 and magicValue >= 32000 then
new_gemid = 50840
elseif u44 >= 60 and magicValue >= 16000 then
new_gemid = 50839
elseif u44 >= 50 and magicValue >= 8000 then
new_gemid = 50838
elseif u44 >= 40 and magicValue >= 8000 then
new_gemid = 50837
elseif u44 >= 30 and magicValue >= 4000 then
new_gemid = 50836
elseif u44 >= 20 and magicValue >= 3000 then
new_gemid = 50835
end
-- 如果ID发生变化,更新显示
if new_gemid ~= current_gemid then
current_gemid = new_gemid
-- 移除旧的ItemShow_3
GUI:removeFromParent(itemShow3)
-- 创建新的ItemShow_3
itemShow3 = GUI:ItemShow_Create(itemroom, "ItemShow_3", itemX2, itemY2, {
index = current_gemid,
count = 1,
look = true,
bgVisible = false
})
-- 更新ItemShow_4
GUI:ItemShow_setIndex(itemShow4, current_gemid)
end
end
-- 按钮点击事件
GUI:addOnClickEvent(button2, function()
-- 获取当前等级和魔力值
local u44 = SL:GetMetaValue("PLAYER_LEVEL", 0) or 0
local magicValue = SL:GetMetaValue("PLAYER_MAGIC", 0) or 0
-- 根据条件更新宝石ID
local old_gemid = current_gemid
if u44 >= 100 and magicValue >= 512000 then
current_gemid = 50843
elseif u44 >= 90 and magicValue >= 256000 then
current_gemid = 50842
elseif u44 >= 80 and magicValue >= 128000 then
current_gemid = 50841
elseif u44 >= 70 and magicValue >= 32000 then
current_gemid = 50840
elseif u44 >= 60 and magicValue >= 16000 then
current_gemid = 50839
elseif u44 >= 50 and magicValue >= 8000 then
current_gemid = 50838
elseif u44 >= 40 and magicValue >= 8000 then
current_gemid = 50837
elseif u44 >= 30 and magicValue >= 4000 then
current_gemid = 50836
elseif u44 >= 20 and magicValue >= 3000 then
current_gemid = 50835
end
-- 如果ID发生变化,更新显示
if old_gemid ~= current_gemid then
-- 移除旧的ItemShow_3
GUI:removeFromParent(itemShow3)
-- 创建新的ItemShow_3
itemShow3 = GUI:ItemShow_Create(itemroom, "ItemShow_3", itemX2, itemY2, {
index = current_gemid,
count = 1,
look = true,
bgVisible = false
})
-- 更新ItemShow_4
GUI:ItemShow_setIndex(itemShow4, current_gemid)
SL:print("宝石已升级到ID:" .. current_gemid)
else
SL:print("条件不满足,无法升级宝石")
end
-- 检查并更新显示状态
checkAndUpdate()
end)
-- 初始检查
checkAndUpdate()
-- 添加定时检查(每秒检查一次)
SL:Schedule(function()
if not GUI:Win_IsNull(parent) then
checkAndUpdate()
end
end, 1)
```
**代码说明:**
1. **界面创建**:创建了主界面、itemroom节点、Button2按钮和ItemShow_4
2. **条件判断逻辑**:
- `checkAndUpdate()`函数检查等级和魔力值条件
- 当等级≥10且魔力值≥1000时,隐藏Effect_1,显示ItemShow_3和ItemShow_4
- 根据不同的等级和魔力值条件设置对应的宝石ID
3. **按钮点击事件**:
- 点击Button2时,根据当前等级和魔力值更新宝石ID
- 每次更新后重新创建ItemShow_3并更新ItemShow_4
4. **注意事项**:
- `SL:GetMetaValue("PLAYER_LEVEL", 0)` 和 `SL:GetMetaValue("PLAYER_MAGIC", 0)` 需要根据实际项目中的变量名调整
- 定时器每秒检查一次条件,确保显示状态实时更新
- 使用了`GUI:Win_IsNull()`判断界面是否有效
**学习建议**:
1. 理解条件判断的层级关系,注意条件的顺序
2. 掌握GUI控件的创建、显示/隐藏和更新操作
3. 学会使用定时器进行状态监控
如果还有不明白的地方,随时问浮生梦老师哦!
← 返回问答库