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)
你的代码逻辑有问题,会导致特效重复创建。应该统一管理特效的创建和删除。以下是修改后的代码:
```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. 先统一移除旧特效,避免残留
2. 只在`var < 5`时创建新特效
3. 当`var >= 5`时,特效会被移除且不会创建新的
这样无论var值如何变化,都能确保特效的正确创建和清理。