SL:print("打开沙城捐献界面")
local npc = {}
local function networkCB(msgID, p1, p2, p3, msgData)
SL:print("===== 收到后端消息 =====")
SL:print("消息ID: " .. tostring(msgID))
SL:print("参数1: " .. tostring(p1))
SL:print("参数2: " .. tostring(p2))
🤖 回答引擎: 前端Lua |
🕒 时间: 2026-04-27 19:35:16 |
👁️ 阅读: 7
看了你的代码,主要问题在动画动作的创建和组合上。根据浮生梦老师前端课程内容,有几个关键点需要修正:
1. **动作创建问题**:`GUI:ActionMoveTo`的参数顺序不对,应该是`(time, x, y)`
2. **动作组合问题**:`GUI:ActionSpawn`需要传入多个动作对象,但你的代码中参数传递方式不对
3. **`GUI:ActionEaseExponentialOut`使用错误**:这个函数应该包裹在动作外面,而不是单独调用
这是修正后的代码:
```lua
SL:Print("打开沙城捐献界面")
local npc = {}
local function networkCB(msgID, p1, p2, p3, msgData)
SL:Print("===== 收到后端消息 =====")
SL:Print("消息ID: " .. tostring(msgID))
SL:Print("参数1: " .. tostring(p1))
SL:Print("参数2: " .. tostring(p2))
SL:Print("参数3: " .. tostring(p3))
SL:Print("字符串: " .. tostring(msgData))
SL:Print("=======================")
if msgID == 1017 and msgData == "沙城捐献" then
if p3 == 0 then
npc:openWin()
end
end
end
function npc:openWin()
-- 创造外部界面
local win_donate = GUI:Win_Create("win_donate", 0, 0, 0, 0)
GUI:LoadExport(win_donate, "donate")
npc.ui = GUI:ui_delegate(win_donate)
GUI:addOnClickEvent(npc.ui.esc, function()
GUI:Win_Close(win_donate)
end)
-- 左侧平移动画
local a = 1
GUI:addOnClickEvent(npc.ui.Button_1, function()
if a == 1 then
a = 2
GUI:Timeline_MoveTo(npc.ui.Panel_1, {x = 5, y = 316}, 0, function() end)
GUI:Timeline_MoveTo(npc.ui.Panel_3, {x = 5, y = -316}, 0, function() end)
elseif a == 2 then
a = 3
GUI:Timeline_MoveTo(npc.ui.Panel_2, {x = 5, y = 316}, 0, function() end)
GUI:Timeline_MoveTo(npc.ui.Panel_1, {x = 5, y = -316}, 0, function() end)
else
a = 1
GUI:Timeline_MoveTo(npc.ui.Panel_3, {x = 5, y = 316}, 0, function() end)
GUI:Timeline_MoveTo(npc.ui.Panel_2, {x = 5, y = -316}, 0, function() end)
end
local panelName = "Panel_" .. a
local moveActionUp_1 = GUI:ActionMoveTo(0.2, 5, 35)
local moveActionDown_1 = GUI:ActionMoveTo(0.3, 5, 0)
local moveAction_Up = GUI:ActionSequence(moveActionUp_1, moveActionDown_1)
local easeAction = GUI:ActionEaseExponentialOut(moveAction_Up)
local delayAction = GUI:DelayTime(0.25)
local sequenceAction = GUI:ActionSequence(delayAction, easeAction)
GUI:runAction(npc.ui[panelName], sequenceAction)
end)
GUI:addOnClickEvent(npc.ui.Button_2, function()
if a == 1 then
a = 3
GUI:Timeline_MoveTo(npc.ui.Panel_2, {x = 5, y = 316}, 0, function() end)
GUI:Timeline_MoveTo(npc.ui.Panel_1, {x = 5, y = -316}, 0, function() end)
elseif a == 3 then
a = 2
GUI:Timeline_MoveTo(npc.ui.Panel_1, {x = 5, y = 316}, 0, function() end)
GUI:Timeline_MoveTo(npc.ui.Panel_3, {x = 5, y = -316}, 0, function() end)
else
a = 1
GUI:Timeline_MoveTo(npc.ui.Panel_3, {x = 5, y = 316}, 0, function() end)
GUI:Timeline_MoveTo(npc.ui.Panel_2, {x = 5, y = -316}, 0, function() end)
end
local panelName = "Panel_" .. a
local moveActionUp_2 = GUI:ActionMoveTo(0.3, 5, -35)
local moveActionDown_2 = GUI:ActionMoveTo(0.2, 5, 0)
local moveAction_Down = GUI:ActionSequence(moveActionUp_2, moveActionDown_2)
local easeAction = GUI:ActionEaseExponentialOut(moveAction_Down)
local delayAction = GUI:DelayTime(0.25)
local sequenceAction = GUI:ActionSequence(delayAction, easeAction)
GUI:runAction(npc.ui[panelName], sequenceAction)
end)
if a == 1 then
-- 修正ActionSpawn的用法
local action_1 = GUI:ActionSpawn(
GUI:ActionScaleTo(0.2, 1.0), -- 注意:ScaleTo的第二个参数是缩放比例,不是像素值
GUI:ActionMoveTo(0.2, 401, 127)
)
local action_2 = GUI:ActionSpawn(
GUI:ActionScaleTo(0.2, 0.8), -- 80%缩放应该是0.8
GUI:ActionMoveTo(0.2, 32, 127)
)
local action_3 = GUI:ActionSpawn(
GUI:ActionScaleTo(0.2, 0.5), -- 50%缩放应该是0.5
GUI:ActionMoveTo(0.2, 217, 86)
)
GUI:addOnClickEvent(npc.ui.Button_4, function()
GUI:runAction(npc.ui.Effect_1, action_1)
GUI:runAction(npc.ui.Effect_2, action_2)
GUI:runAction(npc.ui.Effect_3, action_3)
end)
end
end
SL:RegisterLuaNetMsg(1017, networkCB)
```
**主要修改点:**
1. **移除了`GUI:Timeline_MoveTo`中的回调函数**:这个回调函数会导致`GUI:ActionEaseExponentialOut`被错误地单独调用
2. **修正了`GUI:ActionSpawn`的参数传递**:现在正确地将两个动作作为单独参数传递
3. **修正了`GUI:ActionScaleTo`的参数**:第二个参数应该是缩放比例(1.0=100%,0.8=80%,0.5=50%),而不是像素值
4. **调整了动作组合顺序**:先创建基础动作,再用`GUI:ActionEaseExponentialOut`包裹,最后用`GUI:ActionSequence`组合延迟
这样修改后,`GUI:runAction`就不会收到`nullptr`参数了,错误也就解决了。下次写动画时注意动作函数的参数顺序和组合方式!
← 返回问答库