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)
🤖 回答引擎: 前端Lua |
🕒 时间: 2026-04-20 15:02:03 |
👁️ 阅读: 14
根据你的代码,我注意到你想要实现一个卡牌动画效果,但有几个关键问题需要修正:
1. **所有卡牌都会移动**:你为每个卡牌都执行了移动动画,没有保持中间位置不动
2. **动作嵌套混乱**:`ActionSequence` 内部嵌套了新的 `runAction`,这会导致动作执行顺序混乱
3. **坐标设置问题**:你使用了绝对坐标,但应该考虑父节点的锚点
这是修正后的代码:
```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 fullScreenAnim = GUI:Frames_Create(
parent,
"FullScreenFrames",
screenWidth/2,
screenHeight/2,
"res/public/word_fubentg_",
".png",
1,
10,
{speed = 100, count = 10, loop = -1}
)
GUI:setAnchorPoint(fullScreenAnim, 0.5, 0.5)
GUI:setContentSize(fullScreenAnim, screenWidth, screenHeight)
GUI:setIgnoreContentAdaptWithSize(fullScreenAnim, false)
local layout = GUI:Layout_Create(parent, "layout", screenWidth/2, screenHeight/2, 900, 500)
if layout then
GUI:setAnchorPoint(layout, 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()
-- 移除按钮
GUI:removeFromParent(ui.Button_1)
local cardPositions = {}
local cardSizes = {}
-- 记录中间位置的卡牌(假设第5张卡牌在中间)
local middleCardIndex = 5
for i = 1, 10 do
if GUI:Win_IsNull(ui['Image_'..i]) then
SL:print("卡牌 "..i.." 不存在")
goto continue
end
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:removeFromParent(ui['Image_'..i])
-- 创建卡牌背面
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)
-- 如果是中间卡牌,保持不动
if i == middleCardIndex then
-- 中间卡牌只执行移动到中心点的动作
GUI:runAction(beimian, GUI:ActionSequence(
GUI:ActionMoveTo(1, -72, -103),
GUI:DelayTime(0.5)
))
else
-- 其他卡牌执行完整的动画
local actionSequence = GUI:ActionSequence()
-- 第一步:移动到中心点
actionSequence = GUI:ActionSequence(actionSequence,
GUI:ActionMoveTo(1, -72, -103),
GUI:DelayTime(0.5)
)
-- 第二步:上下分开动画
local targetY = -103
if i % 2 == 1 then -- 奇数编号卡牌往上
targetY = -103 + 172
else -- 偶数编号卡牌往下
targetY = -103 - 172
end
actionSequence = GUI:ActionSequence(actionSequence,
GUI:ActionMoveTo(0.5, -72, targetY),
GUI:DelayTime(0.3),
GUI:ActionMoveTo(0.5, -72, -103),
GUI:DelayTime(0.5)
)
-- 第三步:左右分开动画
local targetX = -72
if i % 3 == 1 then -- 1,4,7,10号卡牌往左
targetX = -72 - 142
else -- 其他卡牌往右
targetX = -72 + 142
end
actionSequence = GUI:ActionSequence(actionSequence,
GUI:ActionMoveTo(0.5, targetX, -103),
GUI:DelayTime(0.3),
GUI:ActionMoveTo(0.5, -72, -103)
)
-- 执行完整的动作序列
GUI:runAction(beimian, actionSequence)
end
::continue::
end
SL:print("中间卡牌(第"..middleCardIndex.."张)保持不动")
end)
end
```
**主要修改点:**
1. **固定中间卡牌**:通过 `middleCardIndex` 变量指定哪张卡牌保持不动
2. **动作序列重构**:使用 `ActionSequence` 正确连接所有动作,避免嵌套 `runAction`
3. **空值检查**:使用 `GUI:Win_IsNull()` 检查组件是否存在
4. **锚点修正**:将布局的锚点设置为 `(0.5, 0.5)` 确保居中
这样修改后,第5张卡牌会移动到中心点后保持不动,其他卡牌会执行完整的上下左右分开动画。如果你想让其他卡牌也不动,只需调整 `middleCardIndex` 的值即可。
← 返回问答库