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
你的代码中显示上一个特效的问题,主要是在创建新特效前没有清除旧的。根据浮生梦老师前端课程内容,特效不会自动移除,需要手动处理。
**问题分析:**
1. 每次条件满足时都创建新特效,但旧特效仍然存在
2. 没有检查是否已有特效,导致特效叠加
**解决方案:**
```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. 添加了清除旧特效的逻辑:`GUI:getChildByName()`获取已有特效
2. 使用`GUI:Win_IsNull()`判断特效是否存在
3. 使用`GUI:removeFromParent()`移除旧特效
这样每次创建新特效前都会先清理旧的,就不会显示上一个特效了。下次写代码要注意资源管理,创建新对象前记得清理旧的哦!