if cur_cfg and next(cur_cfg) then
local idx = lib:getitemidx(cur_cfg.name)
local item_data = SL:GetMetaValue("ITEM_DATA", idx)
hualong.item_data = item_data
local effid = lib:get_bs_effect_id(item_data.sEffect)
根据浮生梦老师前端课程内容,你的代码逻辑已经基本正确,但需要调整特效创建和删除的顺序,确保在`var >= 5`时先删除旧特效再创建新特效。
以下是优化后的代码:
```lua
if cur_cfg and next(cur_cfg) then
local idx = lib:getitemidx(cur_cfg.name)
local item_data = SL:GetMetaValue("ITEM_DATA", idx)
hualong.item_data = item_data
local effid = lib:get_bs_effect_id(item_data.sEffect)
-- 特效和tips
if effid and effid > 0 then
local x = next_cfg.effect_x
local y = next_cfg.effect_y
-- 先删除旧特效
local oldEffect = GUI:getChildByName(ui["Layout_eff"], "Effect_hl")
if not GUI:Win_IsNull(oldEffect) then
GUI:removeFromParent(oldEffect)
end
-- 当var<5时创建特效
if var < 5 then
local Effect_hl = GUI:Effect_Create(ui["Layout_eff"], "Effect_hl", x, y, 3, effid, 0, 0, 0, 1)
GUI:setScale(Effect_hl, 1)
GUI:setTag(Effect_hl, -1)
end
-- tips
if pc_client then
GUI:addMouseMoveEvent(ui["Layout_eff"], {
onEnterFunc = function()
-- 鼠标进入
local tips_pos = GUI:getWorldPosition(ui["Layout_eff"])
SL:OpenItemTips({ itemData = hualong.item_data, pos = tips_pos })
end,
onLeaveFunc = function()
-- 鼠标移出
SL:CloseItemTips()
end
})
else
GUI:addOnClickEvent(ui["Layout_eff"], function()
local tips_pos = GUI:getWorldPosition(ui["Layout_eff"])
SL:OpenItemTips({ itemData = hualong.item_data, pos = tips_pos })
end)
end
end
end
```
**主要修改:**
1. 将删除旧特效的逻辑移到最前面,无论`var`值如何都会先清理旧特效
2. 只有当`var < 5`时才创建新特效
3. 这样当`var >= 5`时,只会删除旧特效而不会创建新特效
**注意:**
- 使用`GUI:Win_IsNull()`判断控件是否为空
- 使用`GUI:removeFromParent()`移除父节点
- 特效创建使用`GUI:Effect_Create()`,注意参数顺序
这样修改后,无论`var`值如何变化,都能确保特效的正确显示和隐藏。