根据浮生梦老师前端课程内容,你的代码需要根据后端发送的p3值来判断显示哪个转盘表格。你需要修改`main`函数和`createItemShow`函数,根据p3值选择对应的`cfg`和`tab`表格。
以下是修改后的代码:
```lua
function zhuanpanOBJ:main(p2, p3, data)
SL:print("p1, p2, p3", p2, p3, data)
self.Data = data and SL:JsonDecode(data) or nil
-- 先验证data是否有效
if data and type(data) == "string" then
local decodedData = SL:JsonDecode(data)
if decodedData then
SL:dump(decodedData, "调试信息", 3) -- 第三个参数是嵌套深度
else
SL:print("JSON解码失败")
end
else
SL:print("data为空或不是字符串")
end
-- 根据p3值选择表格(1表示第一个转盘,2表示第二个转盘)
self.currentTable = p3 or 1
if self.currentTable < 1 or self.currentTable > 2 then
self.currentTable = 1
end
local _V = function(...) return SL:GetMetaValue(...) end
local parent = GUI:Win_Create(self.Name, _V("SCREEN_WIDTH") * 0.5, _V("SCREEN_HEIGHT") * 0.5, 0, 0, false, false,
true, false)
self._parent = parent
self.old_idx = nil
--加载UI
GUI:LoadExport(parent, "A/zhuanpanUI", function()
self.ui = GUI:ui_delegate(parent)
--背景关闭
GUI:addOnClickEvent(self.ui.CloseLayout, function()
GUI:Win_Close(self._parent)
end)
--关闭按钮
GUI:addOnClickEvent(self.ui.CloseButton, function()
GUI:Win_Close(self._parent)
end)
--给服务端发消息
GUI:addOnClickEvent(self.ui.btn_lottery, function()
-- 请求服务端下发奖励idx,传递当前转盘索引
SL:SendLuaNetMsg(1100, self.currentTable, 99, 1, "")
end)
self:createItemShow()
self:pullpay()
end)
-- 显示当前是哪个转盘
local tableName = self.currentTable == 1 and "一" or "二"
GUI:Text_Create(parent, "Text_table_info", 0, 250, 20, "#ff00ff",
string.format("当前转盘:第%s个转盘", tableName))
if self.Data and self.Data[tostring(self.currentTable)] then
GUI:Text_Create(parent, "Text_count_1", -230, -234, 20, "#ff00ff",
string.format("今日可抽奖:【%s/5】次", self.Data[tostring(self.currentTable)].count))
end
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
-- 重新创建红点,使用当前转盘对应的cfg表格
local currentCfg = self.cfg[self.currentTable]
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)
-- 检查数据有效性,使用当前转盘的数据
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
end
end
end
--创建购买道具
function zhuanpanOBJ:pullpay()
-- 使用当前转盘对应的tab表格
local currentTab = self.tab[self.currentTable]
for i = 1, #currentTab do
if self.Data and self.Data[tostring(self.currentTable)] and
(self.Data[tostring(self.currentTable)].cishu < i or self.Data[tostring(self.currentTable)].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()
-- 这里可以添加购买逻辑
end)
elseif self.Data and self.Data[tostring(self.currentTable)] and
self.Data[tostring(self.currentTable)].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
if self.Data and self.Data[tostring(self.currentTable)] and
self.Data[tostring(self.currentTable)].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.currentTable, 0, 2, "领取称号")
end)
end
--抽奖动画
function zhuanpanOBJ:lotteryAnim(gift_idx)
self.old_idx = self.old_idx or 1
GUI:setEnabled(self.ui.btn_lottery, false)
local maxNum = 9 --奖励道具上限
local turnsNum = 3 --轮盘空转圈数
local start_idx = self.old_idx
local randomNum = turnsNum * maxNum
local anim_num = randomNum - ((self.old_idx + randomNum) % maxNum - gift_idx)
local actionList = {}
SL:print("gift_idx----------------", gift_idx, self.old_idx, anim_num)
for i = 1, anim_num do
start_idx = (self.old_idx + 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)
-- 发送当前转盘索引和抽中的位置
SL:SendLuaNetMsg(1100, self.currentTable, 100, 1, tostring(cur_idx))
end)
end
end
self.old_idx = 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
-- p3参数表示是哪个转盘(1或2)
zhuanpanOBJ:main(p2, p3, msgData)
if p2 ~= 0 and p2 ~= 112 then
zhuanpanOBJ:lotteryAnim(p3)
end
if p2 == 112 then
SL:Print("领取奖励成功")
local Text_count_1 = GUI:getChild(zhuanpanOBJ._parent, "Text_count_1")
if Text_count_1 and not GUI:Win_IsNull(Text_count_1) then
GUI:Text_setString(Text_count_1,
string.format("今日可抽奖:【%s/5】次",
zhuanpanOBJ.Data[tostring(zhuanpanOBJ.currentTable)].count))
end
end
end)
```
主要修改点:
1. 在`main`函数中根据`p3`参数设置`self.currentTable`(1或2)
2. 所有使用表格的地方都改为`self.cfg[self.currentTable]`和`self.tab[self.currentTable]`
3. 数据访问改为`self.Data[tostring(self.currentTable)]`
4. 发送消息时带上当前转盘索引`self.currentTable`
5. 界面显示当前是哪个转盘
这样就能根据后端发送的p3值来显示对应的转盘了。注意浮生梦老师前端课程中强调,要确保后端发送的p3值正确(1表示第一个转盘,2表示第二个转盘)。