function MainProperty.OnPlayMagicBallEffect(data)
local prefixL = {"hp_", "mp_", "fhp_"}
local tagList = {"HPSFX", "MPSFX", "FHPSFX"}
if data.type < 0 or data.type > 2 or data.count < 0 or data.interval < 0 then
return
end
你的代码有几个关键问题导致动画不跟动:
1. **重复的sprite变量**:第二个动画创建时使用了相同的`sprite`变量名,覆盖了第一个sprite
2. **路径前缀错误**:`prefix = prefixL[data.type + 2]`应该用`data.type + 1`
3. **UI面板获取错误**:`widget2 = MainProperty._ui[string.format("Panel_%ssfx", prefix)]`获取的是同一个面板
修正后的关键部分:
```lua
-- 第一个动画(血球)
local sprite1
if tag and widget then
MainProperty._pSize[tag] = contentSize
GUI:setContentSize(widget, contentSize.width, contentSize.height)
if not GUI:getChildByName(widget, tag) then
sprite1 = GUI:Sprite_Create(widget, tag, 0, 0)
GUI:setScale(sprite1, scale)
GUI:runAction(sprite1, GUI:ActionRepeatForever(GUI:ActionAnimate(ani)))
end
end
-- 第二个动画(篮球)修正
local prefix2 = prefixL[data.type + 1] or "" -- 这里应该是+1,不是+2
local tag2 = tagList[data.type + 1]
local widget2 = MainProperty._ui[string.format("Panel_%ssfx", prefix2)] -- 注意:这个可能不存在
local sprite2
if tag2 and widget2 then
MainProperty._pSize[tag2] = contentSize
GUI:setContentSize(widget2, contentSize.width, contentSize.height)
if not GUI:getChildByName(widget2, tag2) then
sprite2 = GUI:Sprite_Create(widget2, tag2, 0, 0)
GUI:setScale(sprite2, scale)
GUI:runAction(sprite2, GUI:ActionRepeatForever(GUI:ActionAnimate(ani)))
end
end
```
主要问题是你第二个动画试图使用`Panel_%ssfx`,但根据你的代码逻辑,可能没有对应的UI面板定义。建议检查:
1. 确保`MainProperty._ui`中有`Panel_hp_sfx`、`Panel_mp_sfx`、`Panel_fhp_sfx`的定义
2. 两个动画使用不同的sprite变量名
3. 第二个动画的路径前缀应该与第一个相同