看了你的代码,我发现有几个地方需要调整来匹配你的配置表结构。你的配置表 `zhuanpanOBJ.cfg` 和 `zhuanpanOBJ.tab` 都是二维数组,需要根据后端传来的 `p1` 参数来决定使用哪一个子数组。
主要问题在于:
1. `createItemShow` 和 `pullpay` 函数中,没有根据 `p1` 来索引正确的配置表。
2. 动画逻辑中的 `self.old_idx` 需要区分不同的转盘,否则切换转盘时动画起点会错乱。
以下是修改后的关键部分:
```lua
function zhuanpanOBJ:main(p1, p2, p3, data)
SL:print("p1, p2, p3", p1, p2, p3, data)
self.p1 = p1 or 1 -- 保存p1,用于区分是哪个转盘(1或2)
self.Data = data and SL:JsonDecode(data) or nil
-- ... 其他初始化代码保持不变 ...
self:createItemShow()
self:pullpay()
-- 显示抽奖次数时,使用正确的Data索引
local countKey = tostring(self.p1)
local count = self.Data and self.Data[countKey] and self.Data[countKey].count or 0
GUI:Text_Create(parent, "Text_count_" .. self.p1, -230, -234, 20, "#ff00ff",
string.format("今日可抽奖:【%s/5】次", count))
end
function zhuanpanOBJ:createItemShow()
-- 先清理已存在的红点
for i = 1, 9 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
-- 根据p1获取对应的配置
local currentCfg = self.cfg[self.p1] or {}
-- 重新创建红点
for i = 1, 9 do
local cfgData = currentCfg[i]
if cfgData then
local item = GUI:ItemShow_Create(self.ui["item_" .. i], "item", 0, 0,
{ index = cfgData.showReward[1][1], look = true, count = cfgData.showReward[1][2] })
GUI:setAnchorPoint(item, 0.5, 0.5)
-- 检查数据有效性,注意Data的key也根据p1变化
local dataKey = tostring(self.p1)
if self.Data and self.Data[dataKey] and self.Data[dataKey].reward and
self.Data[dataKey].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
end
end
end
--创建购买道具
function zhuanpanOBJ:pullpay()
-- 根据p1获取对应的购买配置
local currentTab = self.tab[self.p1] or {}
for i = 1, #currentTab do
local dataKey = tostring(self.p1)
if self.Data and self.Data[dataKey] and (self.Data[dataKey].cishu < i or self.Data[dataKey].cishu <= 0) then
GUI:createButton(self._parent, "btn_" .. i, 300, 132 - (i - 1) * 42, "res/public/1900000680.png",
currentTab[i].jg .. "元购买", "#FF00FF3C")
GUI:onClick(self.ui["btn_" .. i], function()
-- 这里需要根据p1发送不同的消息
SL:SendLuaNetMsg(1100, self.p1, 999, i, "购买")
end)
elseif self.Data and self.Data[dataKey] and self.Data[dataKey].cishu >= i then
local imgPath = "res/custom/common/zt/yz03.png"
local Image_bg = GUI:Image_Create(self._parent, "Image_ygm_" .. i, 255, 112 - (i - 1) * 42, imgPath)
end
end
local dataKey = tostring(self.p1)
if self.Data and self.Data[dataKey] and self.Data[dataKey].all_pack ~= 1 then
local btn_goumai = GUI:createButton(self._parent, "btn_goumai", 225, -160, "res/public/1900000680.png",
"一键购买", "#FF00FBFF")
GUI:onClick(btn_goumai, function()
self:pullUpRecharge(count)
end)
else
local imgPath = "res/custom/common/zt/yz03.png"
GUI:Image_Create(self._parent, "Image_goumai_", 190, -180, imgPath)
end
local btn_lingqu = GUI:createButton(self._parent, "btn_lingqu", 255, -220, "res/public/1900000680.png",
"领取称号", "#FF00FBFF")
GUI:onClick(btn_lingqu, function()
SL:SendLuaNetMsg(1100, self.p1, 0, 2, "领取称号")
end)
end
--抽奖动画
function zhuanpanOBJ:lotteryAnim(gift_idx)
-- 为每个转盘单独记录old_idx
self.old_idx = self.old_idx or {}
self.old_idx[self.p1] = self.old_idx[self.p1] or gift_idx * 3
GUI:setEnabled(zhuanpanOBJ.ui.btn_lottery, false)
local maxNum = 9
local turnsNum = 3
local start_idx = self.old_idx[self.p1]
local randomNum = turnsNum * maxNum
local anim_num = randomNum - ((self.old_idx[self.p1] + randomNum) % maxNum - gift_idx)
local actionList = {}
SL:print("抽奖动画参数", "p1:", self.p1, "gift_idx:", gift_idx, "old_idx:", self.old_idx[self.p1], "anim_num:", anim_num)
for i = 1, anim_num do
start_idx = (self.old_idx[self.p1] + i) % maxNum
local cur_idx = start_idx == 0 and maxNum or start_idx
local delayTime = 0.05
if i <= 3 then
delayTime = 0.4 - 0.1 * i
elseif i >= anim_num - 3 then
delayTime = 0.1 + 0.1 * (i + 3 - anim_num)
end
actionList[#actionList + 1] = GUI:DelayTime(delayTime)
actionList[#actionList + 1] = GUI:CallFunc(function()
for k = 1, maxNum do
GUI:setVisible(self.ui["select_" .. k], k == cur_idx)
end
end)
if i == anim_num then
actionList[#actionList + 1] = GUI:CallFunc(function()
GUI:setEnabled(self.ui.btn_lottery, true)
local redDot = GUI:Image_Create(self.ui["item_" .. cur_idx], "reward_redDot_" .. i, 45, 25,
"res/custom/common/zt/zaixian1.png")
GUI:setAnchorPoint(redDot, 1, 1)
-- 发送消息时带上p1参数
SL:SendLuaNetMsg(1100, self.p1, 100, 1, tostring(cur_idx))
end)
end
end
-- 更新对应转盘的old_idx
self.old_idx[self.p1] = gift_idx
GUI:runAction(self._parent, GUI:ActionSequence(actionList))
end
-------------------------------↓↓↓ 网络消息 ↓↓↓-------------------------------
--收到服务端消息,抽奖动画
SL:RegisterLuaNetMsg(1200, function(msgID, p1, p2, p3, msgData)
zhuanpanOBJ.Data = msgData and SL:JsonDecode(msgData) or nil
-- 调用main时传入p1
zhuanpanOBJ:main(p1, p2, p3, msgData)
if p2 ~= 0 and p2 ~= 112 then
zhuanpanOBJ:lotteryAnim(p3)
end
if p2 == 112 then
SL:Print("领取奖励成功")
local dataKey = tostring(zhuanpanOBJ.p1)
local count = zhuanpanOBJ.Data and zhuanpanOBJ.Data[dataKey] and zhuanpanOBJ.Data[dataKey].count or 0
local Text_count = GUI:getChildByName(zhuanpanOBJ._parent, "Text_count_" .. zhuanpanOBJ.p1)
if Text_count and not GUI:Win_IsNull(Text_count) then
GUI:Text_setString(Text_count, string.format("今日可抽奖:【%s/5】次", count))
end
end
end)
```
**主要修改点:**
1. **保存p1参数**:在`main`函数中保存`self.p1`,用于后续区分转盘。
2. **配置表索引**:在`createItemShow`和`pullpay`函数中,使用`self.cfg[self.p1]`和`self.tab[self.p1]`来获取对应转盘的配置。
3. **数据索引**:访问`self.Data`时,使用`tostring(self.p1)`作为key,如`self.Data[tostring(self.p1)]`。
4. **动画状态隔离**:将`self.old_idx`改为表结构`self.old_idx[self.p1]`,确保两个转盘的动画起点互不干扰。
5. **消息发送**:在发送网络消息时,都带上`self.p1`参数,让服务端知道是哪个转盘的操作。
6. **文本更新**:创建和更新文本控件时,使用`"Text_count_" .. self.p1`作为ID,避免两个转盘的文本控件冲突。
这样修改后,你的代码就能根据后端传来的`p1`参数(1或2)正确显示对应的转盘配置和进行抽奖了。记得测试时确保后端发送的`p1`参数正确哦!