local function zhuanpan(msgID, msgData)
local screenWidth = SL:GetMetaValue("SCREEN_WIDTH", 0)
local screenHeight = SL:GetMetaValue("SCREEN_HEIGHT", 0)
local parent = GUI:Win_Create("zhuanpan", 0, 0, 0, 0, false, false, true, false,
🤖 回答引擎: 前端Lua |
🕒 时间: 2026-04-20 20:14:59 |
👁️ 阅读: 21
从错误信息来看,问题出现在 `GUI:removeFromParent(fullScreenAnim)` 这一行,提示 `target is nil value`。这通常是因为 `fullScreenAnim` 这个对象在回调函数执行时已经无效或者为 nil。
根据浮生梦老师前端课程内容,当你在序列帧动画的回调函数中直接移除自己时,可能会遇到对象生命周期的问题。以下是修改建议:
```lua
local function zhuanpan(msgID, msgData)
local screenWidth = SL:GetMetaValue("SCREEN_WIDTH", 0)
local screenHeight = SL:GetMetaValue("SCREEN_HEIGHT", 0)
local parent = GUI:Win_Create("zhuanpan", 0, 0, 0, 0, false, false, true, false, false, false, 1)
-- 修改回调函数,避免直接使用可能已失效的对象
local function firstAnimCallback()
-- 先检查对象是否有效
if not GUI:Win_IsNull(fullScreenAnim) then
GUI:removeFromParent(fullScreenAnim)
end
-- 创建第二个序列帧
local secondAnim = GUI:Frames_Create(
parent,
"SecondFrames",
screenWidth/2,
screenHeight/2,
"res/yuding/donghua/b/b",
".png",
1,
20,
{
speed = 100,
count = 20,
loop = -1,
}
)
GUI:setAnchorPoint(secondAnim, 0.5, 0.5)
GUI:setContentSize(secondAnim, screenWidth, screenHeight)
GUI:setIgnoreContentAdaptWithSize(secondAnim, false)
GUI:Win_SetMainHide(parent, true)
local layout = GUI:Layout_Create(parent, "layout", screenWidth/2, screenHeight/2, 900, 500)
if layout then
GUI:setAnchorPoint(layout, 0.5, 0.5) -- 修正:应该设置为0.5, 0.5居中
GUI:setTouchEnabled(layout, true)
end
-- 背景图 - 居中显示
local bg_kuang = GUI:Image_Create(layout, "bg_kuang", 450, 250, "res/01/010011.jpg")
if bg_kuang then
GUI:setAnchorPoint(bg_kuang, 0.5, 0.5)
GUI:setContentSize(bg_kuang, {width = 900, height = 500})
end
GUI:LoadExport(layout, "zhuanpan")
local ui = GUI:ui_delegate(layout)
GUI:setCascadeOpacityEnabled(layout, true)
GUI:setOpacity(layout, 0)
GUI:runAction(layout, GUI:ActionFadeIn(2))
GUI:addOnClickEvent(ui.Button_1, function()
if GUI:Win_IsNull(ui.Button_1) then return end
GUI:removeFromParent(ui.Button_1)
local cardPositions = {}
local cardSizes = {}
-- 先记录所有初始位置
for i = 1, 10 do
local pos = GUI:getPosition(ui['Image_'..i])
local size = GUI:getContentSize(ui['Image_'..i])
cardPositions[i] = {x = pos.x, y = pos.y}
cardSizes[i] = {width = size.width, height = size.height}
GUI:setOpacity(ui['Image_'..i], 0)
end
-- 创建背面卡牌数组
local beimianCards = {}
-- 创建背面卡牌并执行动画
for i = 1, 10 do
local beimian = GUI:Button_Create(layout, "bei"..i, cardPositions[i].x, cardPositions[i].y, "res/public_win32/1900000610.png")
GUI:setContentSize(beimian, cardSizes[i].width, cardSizes[i].height)
GUI:setVisible(beimian, true)
beimianCards[i] = beimian
-- 先移动到第一个目标位置
GUI:runAction(beimian, GUI:ActionSequence(
GUI:ActionMoveTo(0.3, -72, -103),
GUI:DelayTime(0.3),
GUI:CallFunc(function()
-- 上下移动
local targetY = -103
if i % 2 == 1 then
targetY = -103 + 172
else
targetY = -103 - 172
end
GUI:runAction(beimian, GUI:ActionSequence(
GUI:ActionMoveTo(0.3, -72, targetY),
GUI:DelayTime(0.3),
GUI:ActionMoveTo(0.3, -72, -103),
GUI:DelayTime(0.3),
GUI:CallFunc(function()
-- 左右移动
local targetX = -72
if i % 3 == 1 then
targetX = -72 - 142
else
targetX = -72 + 142
end
GUI:runAction(beimian, GUI:ActionSequence(
GUI:ActionMoveTo(0.3, targetX, -103),
GUI:DelayTime(0.3),
GUI:ActionMoveTo(0.3, -72, -103),
GUI:DelayTime(0.3),
GUI:CallFunc(function()
-- 所有动画完成后,开始依次回位
if i == 10 then
-- 最后一张卡牌完成动画后,开始依次回位
local function moveBackToOriginal(index)
if index > 10 then return end
local card = beimianCards[index]
if GUI:Win_IsNull(card) then
moveBackToOriginal(index + 1)
return
end
local originalPos = cardPositions[index]
GUI:setAnchorPoint(card, 0.5, 0.5)
-- 旋转并移动回初始位置
GUI:runAction(card, GUI:ActionSpawn(
GUI:ActionRotateBy(0.4, 360), -- 旋转360度
GUI:ActionMoveTo(0.4, originalPos.x+60, originalPos.y+100),
GUI:CallFunc(function()
-- 当前卡牌回位完成后,开始下一张
moveBackToOriginal(index + 1)
end)
))
end
moveBackToOriginal(1)
end
end)
))
end)
))
end)
))
end
end)
end
-- 创建第一个序列帧
local fullScreenAnim = GUI:Frames_Create(
parent,
"FullScreenFrames",
screenWidth/2,
screenHeight/2,
"res/yuding/donghua/a/a",
".png",
1,
20,
{
speed = 100,
count = 20,
loop = 1,
callback = firstAnimCallback
}
)
GUI:setAnchorPoint(fullScreenAnim, 0.5, 0.5)
GUI:setContentSize(fullScreenAnim, screenWidth, screenHeight)
GUI:setIgnoreContentAdaptWithSize(fullScreenAnim, false)
end
```
主要修改点:
1. 将回调函数提取为独立的 `firstAnimCallback` 函数
2. 在移除对象前使用 `GUI:Win_IsNull()` 检查对象是否有效
3. 修正了 `layout` 的锚点设置(应该是 0.5, 0.5 而不是 0, 00)
4. 在动画回调中也添加了空值检查
这样修改后,当回调函数执行时,会先检查 `fullScreenAnim` 是否仍然有效,避免出现 `target is nil value` 的错误。下次写代码时要注意对象生命周期管理,不懂的可以问浮生梦老师!
← 返回问答库