GUI:addOnClickEvent(beimian, function()
GUI:removeFromParent(beimian)
local i = 1
local eff = GUI:Effect_Create(itemshow[i], itemshow[i], -20, 82, 0, 20212, 0, 0, 0, 1)
GUI
🤖 回答引擎: 前端Lua |
🕒 时间: 2026-04-21 18:50:34 |
👁️ 阅读: 13
你的代码报错是因为 `GUI:Effect_Create` 函数的第一个参数需要是一个有效的父节点对象,但你在递归调用时传递的是 `itemshow[i]`,而 `itemshow[i]` 很可能不是一个有效的控件对象,或者它是一个字符串(比如控件名称),而不是控件本身。
根据浮生梦老师前端课程内容,`GUI:Effect_Create` 函数的正确用法如下:
```lua
-- 正确示例:第一个参数是父节点控件对象
local effect = GUI:Effect_Create(parentWidget, "effect_name", x, y, zOrder, effectID, 0, 0, dir, loop)
```
在你的代码中:
1. `itemshow[i]` 应该是控件名称(字符串),而不是控件对象
2. 你需要先通过 `GUI:getChildByName()` 获取到实际的控件对象
修改后的代码应该是这样的:
```lua
GUI:addOnClickEvent(beimian, function()
GUI:removeFromParent(beimian)
local i = 1
-- 先获取父节点控件对象
local parentWidget = GUI:getChildByName(itemshow[i], itemshow[i]) -- 假设itemshow[i]是父节点名称
if parentWidget and not GUI:Win_IsNull(parentWidget) then
local eff = GUI:Effect_Create(parentWidget, "effect_"..i, -20, 82, 0, 20212, 0, 0, 0, 1)
GUI:Effect_play(eff, 0, 0, false, 1)
if i < 20 then
GUI:Effect_addOnCompleteEvent(eff, GUI:CallFunc(function()
i = i + 1
local nextParent = GUI:getChildByName(itemshow[i], itemshow[i])
if nextParent and not GUI:Win_IsNull(nextParent) then
local nextEff = GUI:Effect_Create(nextParent, "effect_"..i, -20, 82, 0, 20212, 0, 0, 0, 1)
GUI:Effect_play(nextEff, 0, 0, false, 1)
end
end))
end
end
end)
```
**关键点:**
1. `GUI:Effect_Create` 的第一个参数必须是控件对象,不能是字符串
2. 使用 `GUI:getChildByName()` 通过名称获取控件对象
3. 使用 `GUI:Win_IsNull()` 检查控件是否有效
4. 特效名称需要唯一,建议使用 `"effect_"..i` 这样的格式
下次写代码时要注意参数类型,控件名称和控件对象是不同的概念!不懂的可以问浮生梦尊师!
← 返回问答库