local function createEntryAnimation()
if not moshenyuanli.parent or not moshenyuanli.parent._ui then return end
local ui = moshenyuanli.parent._ui
-- 获取需要动画的控件
local image1 = ui["Image_1"]
local image3 = ui["Image_3"]
看了你的代码,这是一个从右侧滑入的入场动画。根据浮生梦老师前端课程内容,我注意到几个可以优化的地方:
1. **动画触发时机**:你希望只在点击NPC时触发,点击按钮不触发
2. **代码结构**:可以更简洁一些
3. **动画同步**:目前是各自独立的动画,可以做成一个完整的序列
这是优化后的版本:
```lua
local function createEntryAnimation()
if not moshenyuanli.parent or not moshenyuanli.parent._ui then
SL:print("界面未初始化,无法创建动画")
return
end
local ui = moshenyuanli.parent._ui
-- 获取需要动画的控件
local widgets = {
image1 = ui["Image_1"],
image3 = ui["Image_3"],
text1 = ui["Text_1"],
text2 = ui["Text_2"],
text3 = ui["Text_3"],
text4 = ui["Text_4"]
}
-- 存储原始位置
local originalPositions = {}
-- 先设置所有控件为不可见,并记录原始位置
for name, widget in pairs(widgets) do
if widget and not GUI:Win_IsNull(widget) then
local pos = GUI:getPosition(widget)
originalPositions[name] = {x = pos.x, y = pos.y}
GUI:setVisible(widget, false)
end
end
-- 获取屏幕宽度
local screenWidth = SL:GetMetaValue("SCREEN_WIDTH") or 1136
-- 设置初始位置(从屏幕右侧外开始)
for name, widget in pairs(widgets) do
if widget and not GUI:Win_IsNull(widget) and originalPositions[name] then
local pos = originalPositions[name]
GUI:setPosition(widget, screenWidth, pos.y)
GUI:setVisible(widget, true)
end
end
-- 创建动画序列
local actionList = {}
-- Image_1和Image_3同时移动(延迟0秒开始)
if widgets.image1 and not GUI:Win_IsNull(widgets.image1) and originalPositions.image1 then
local pos = originalPositions.image1
table.insert(actionList, GUI:CallFunc(function()
GUI:runAction(widgets.image1, GUI:ActionMoveTo(0.5, pos.x, pos.y))
end))
end
if widgets.image3 and not GUI:Win_IsNull(widgets.image3) and originalPositions.image3 then
local pos = originalPositions.image3
table.insert(actionList, GUI:CallFunc(function()
GUI:runAction(widgets.image3, GUI:ActionMoveTo(0.5, pos.x, pos.y))
end))
end
-- 文字阶梯动画
local textDelay = 0.1
local textAnims = {
{widget = widgets.text1, delay = 0.1, duration = 0.4},
{widget = widgets.text2, delay = 0.2, duration = 0.4},
{widget = widgets.text3, delay = 0.3, duration = 0.4},
{widget = widgets.text4, delay = 0.4, duration = 0.4}
}
for _, animInfo in ipairs(textAnims) do
local widget = animInfo.widget
local name = "text" .. string.match(tostring(widget), "Text_(%d)")
if widget and not GUI:Win_IsNull(widget) and originalPositions[name] then
local pos = originalPositions[name]
table.insert(actionList, GUI:DelayTime(animInfo.delay))
table.insert(actionList, GUI:CallFunc(function()
GUI:runAction(widget, GUI:ActionMoveTo(animInfo.duration, pos.x, pos.y))
end))
end
end
-- 所有动画完成后的回调
table.insert(actionList, GUI:DelayTime(0.8)) -- 等待所有动画完成
table.insert(actionList, GUI:CallFunc(function()
SL:print("所有入场动画完成")
end))
-- 执行动画序列
if moshenyuanli.parent and not GUI:Win_IsNull(moshenyuanli.parent) then
GUI:runAction(moshenyuanli.parent, GUI:ActionSequence(actionList))
end
end
-- 只在点击NPC时触发动画
SL:RegisterLUAEvent(LUA_EVENT_TALKTONPC, "moshenyuanli", function(...)
createEntryAnimation()
end)
```
**主要改进:**
1. **事件注册**:使用`SL:RegisterLUAEvent`只在点击NPC时触发动画
2. **代码结构**:使用表来管理控件,避免重复代码
3. **动画同步**:使用`ActionSequence`确保动画按顺序执行
4. **错误处理**:增加了空值判断和错误提示
**注意:**
- 记得在界面关闭时清理动画,避免内存泄漏
- 如果界面有多个实例,需要确保每个实例独立管理动画
- 动画完成后可以考虑添加一些视觉反馈,比如轻微震动或发光效果
这样修改后,动画就只在点击NPC时触发,点击按钮不会触发了。有什么不清楚的地方随时问我!