我是lua后端老师!小仙,你这个需求是要在每次进入转盘界面时,让8个物品从中心位置飞到它们各自的位置,这个效果在996引擎里可以用`GUI:runAction`配合移动动作来实现。
你需要修改`createItemShow`函数,在创建物品后先设置到中心位置,然后执行一个移动到目标位置的动画。
下面是修改后的`createItemShow`函数代码:
```lua
function zhuanpanOBJ:createItemShow()
-- 先清理已存在的红点
for i = 1, 8 do
local item = self.ui["item_" .. i]
if item and not GUI:Win_IsNull(item) then
local oldRedDot = GUI:getChildByName(item, "Image_bg_" .. i)
if oldRedDot and not GUI:Win_IsNull(oldRedDot) then
GUI:removeFromParent(oldRedDot)
end
end
end
-- 重新创建红点,使用当前转盘对应的cfg表格
local currentCfg = self.cfg[self.currentTable]
for i = 1, 8 do
local cfgData = currentCfg[i]
if cfgData then
-- 先获取item容器控件的中心位置
local container = self.ui["item_" .. i]
local containerX, containerY = GUI:getPosition(container)
-- 在容器中心创建物品(先放在中间)
local item = GUI:ItemShow_Create(container, "item", 0, 0,
{ index = cfgData.showReward[1][1], look = true, count = cfgData.showReward[1][2] })
GUI:setAnchorPoint(item, 0.5, 0.5)
-- 设置物品初始位置在容器中心
GUI:setPosition(item, 0, 0)
-- 检查数据有效性,使用当前转盘的数据
if self.Data and self.Data[tostring(self.currentTable)] and
self.Data[tostring(self.currentTable)].reward and
self.Data[tostring(self.currentTable)].reward[tostring(i)] == 1 then
-- 添加红点标记已获取
local imgPath = "res/custom/common/zt/zaixian1.png"
local Image_bg = GUI:Image_Create(item, "Image_bg_" .. i, -25, -15, imgPath)
end
-- 创建飞行动画:从中心飞到正确位置
-- 这里假设每个item容器的位置就是目标位置
-- 因为物品已经在容器中心,所以目标位置就是(0,0)
-- 如果需要从屏幕中心飞到容器位置,可以计算偏移量
-- 方法1:从当前位置飞到容器位置(有弹性效果)
local moveAction = GUI:MoveBy(0.5, 0, 0) -- 实际上不需要移动,因为已经在正确位置
local easeAction = GUI:EaseElasticOut(moveAction, 0.8) -- 弹性效果
-- 方法2:如果需要从屏幕中心飞到各自位置(更复杂的计算)
-- local screenCenterX = _V("SCREEN_WIDTH") * 0.5
-- local screenCenterY = _V("SCREEN_HEIGHT") * 0.5
-- local targetX = containerX - screenCenterX
-- local targetY = containerY - screenCenterY
-- GUI:setPosition(item, targetX, targetY) -- 先放到屏幕中心
-- local moveAction = GUI:MoveBy(0.5, -targetX, -targetY) -- 飞回容器位置
-- 执行动画
GUI:runAction(item, easeAction)
end
end
end
```
如果你想要更炫酷的效果,比如所有物品从屏幕中心分别飞到8个不同位置,可以这样修改:
```lua
function zhuanpanOBJ:createItemShow()
-- 先清理已存在的红点
for i = 1, 8 do
local item = self.ui["item_" .. i]
if item and not GUI:Win_IsNull(item) then
local oldRedDot = GUI:getChildByName(item, "Image_bg_" .. i)
if oldRedDot and not GUI:Win_IsNull(oldRedDot) then
GUI:removeFromParent(oldRedDot)
end
end
end
-- 获取屏幕中心坐标
local _V = function(...) return SL:GetMetaValue(...) end
local screenCenterX = _V("SCREEN_WIDTH") * 0.5
local screenCenterY = _V("SCREEN_HEIGHT") * 0.5
-- 重新创建红点,使用当前转盘对应的cfg表格
local currentCfg = self.cfg[self.currentTable]
for i = 1, 8 do
local cfgData = currentCfg[i]
if cfgData then
-- 获取item容器的位置
local container = self.ui["item_" .. i]
local containerX, containerY = GUI:getPosition(container)
-- 计算从屏幕中心到容器的偏移量
local offsetX = containerX - screenCenterX
local offsetY = containerY - screenCenterY
-- 在容器中创建物品,但先放在屏幕中心
local item = GUI:ItemShow_Create(container, "item", offsetX, offsetY,
{ index = cfgData.showReward[1][1], look = true, count = cfgData.showReward[1][2] })
GUI:setAnchorPoint(item, 0.5, 0.5)
-- 检查数据有效性,使用当前转盘的数据
if self.Data and self.Data[tostring(self.currentTable)] and
self.Data[tostring(self.currentTable)].reward and
self.Data[tostring(self.currentTable)].reward[tostring(i)] == 1 then
-- 添加红点标记已获取
local imgPath = "res/custom/common/zt/zaixian1.png"
local Image_bg = GUI:Image_Create(item, "Image_bg_" .. i, -25, -15, imgPath)
end
-- 创建飞行动画:从屏幕中心飞到容器位置
local moveAction = GUI:MoveBy(0.6, -offsetX, -offsetY)
local easeAction = GUI:EaseBackOut(moveAction) -- 回弹效果
-- 可以添加一点延迟,让8个物品依次飞出
local delayAction = GUI:DelayTime(i * 0.05) -- 每个物品延迟0.05秒
local sequence = GUI:ActionSequence({delayAction, easeAction})
-- 执行动画
GUI:runAction(item, sequence)
end
end
end
```
这样修改后,每次打开转盘界面,8个物品就会从中心位置飞到各自的位置了。你可以调整动画时间、延迟时间和缓动效果来达到最佳视觉效果。
如果还有不明白的地方,随时问我哦!