这是后端
FuLiDaTing2 = class("FuLiDaTing2", BaseSystemLogic)
function FuLiDaTing2:init()
self:initCfg()
EventManager:add(EventCfg.pickUpItemEx, handler(self, self.pickUpItem), self)--捡取任意物品后触发
EventManager:add(EventCfg.killMon, handler(self,
🤖 回答引擎: 前端Lua |
🕒 时间: 2026-06-03 15:09:06 |
👁️ 阅读: 5
根据浮生梦老师前端课程内容,我来帮你解决“已领取后显示已激活图片”的功能。主要问题是在前端需要根据后端保存的领取记录来判断每个装备是否已领取。
以下是修改后的前端代码:
```lua
function FuLiDaTing2:YuanBaoPanel(data)
local ui = self.ui
local cfg = self._cfg or {}
self.curPage = self.curPage or 1
-- 获取后端保存的领取记录
local serverData = SL:GetMetaValue("SERVER_VALUE", "FuLiDaTing2_Record") or ""
SL:Print("服务器领取记录:", serverData)
GUI:removeAllChildren(ui.ListView_1)
local cfgIndex = self.curPage
local curCfg = cfg[cfgIndex]
if not curCfg then return end
for i = 1, 10 do
--背景图
local Image_5 = GUI:Image_Create(ui.ListView_1, "Image_5"..self.curPage.."_i"..i, 100, 0, "res/custom/02bg2/28.png")
GUI:setContentSize(Image_5, 516, 52)
--装备名称
local name = curCfg.equip[i]
local itemIndex = SL:GetMetaValue("ITEM_NAME", name)
self.name = GUI:Text_Create(Image_5, "name", 0, 14, 16, "#ffffff", itemIndex)
local jiangli = curCfg.givetext
self.jiangli = GUI:Text_Create(Image_5, "jiangli", 206, 14, 16, "#ffffff", jiangli)
-- 判断是否已领取
local checkKey = tostring(name) .. ","
local isClaimed = string.find(serverData, checkKey) ~= nil
-- 根据领取状态显示不同图片
if isClaimed then
-- 已领取:显示res/custom/04mainui/05.png
local Image_weilq = GUI:Image_Create(Image_5, "Image_weilq", 396, 9, "res/custom/04mainui/05.png")
GUI:setContentSize(Image_weilq, 60, 30)
GUI:setTouchEnabled(Image_weilq, false)
else
-- 未领取:显示res/custom/04mainui/04.png
local Image_lingq = GUI:Image_Create(Image_5, "Image_lingq", 397, 10, "res/custom/04mainui/04.png")
GUI:setContentSize(Image_lingq, 60, 30)
GUI:setTouchEnabled(Image_lingq, true)
-- 添加点击事件(如果需要领取功能)
GUI:addOnClickEvent(Image_lingq, function()
self:claimReward(name, curCfg.give)
end)
end
end
self:updatePageButtons()
end
function FuLiDaTing2:ZuanShiPanel(data)
local ui = self.ui
local cfg = self._cfg or {}
self.curPage = self.curPage or 1
-- 获取后端保存的领取记录
local serverData = SL:GetMetaValue("SERVER_VALUE", "FuLiDaTing2_Record") or ""
GUI:removeAllChildren(ui.ListView_3)
local cfgIndex = self.curPage + 20 -- 钻石页从21-40
local curCfg = cfg[cfgIndex]
if not curCfg then return end
for i = 1, 10 do
--背景图
local Image_7 = GUI:Image_Create(ui.ListView_3, "Image_7"..self.curPage.."_i"..i, 0, 0, "res/custom/02bg2/28.png")
GUI:setContentSize(Image_7, 516, 52)
--装备名称
local name = curCfg.equip[i]
local itemIndex = SL:GetMetaValue("ITEM_NAME", name)
self.names = GUI:Text_Create(Image_7, "name", 0, 14, 16, "#ffffff", itemIndex)
local jiangli = curCfg.givetext
self.jianglis = GUI:Text_Create(Image_7, "jiangli", 206, 14, 16, "#ffffff", jiangli)
-- 判断是否已领取
local checkKey = tostring(name) .. ","
local isClaimed = string.find(serverData, checkKey) ~= nil
-- 根据领取状态显示不同图片
if isClaimed then
-- 已领取
local Image_weilq = GUI:Image_Create(Image_7, "Image_weilq", 396, 9, "res/custom/04mainui/05.png")
GUI:setContentSize(Image_weilq, 60, 30)
GUI:setTouchEnabled(Image_weilq, false)
else
-- 未领取
local Image_lingq = GUI:Image_Create(Image_7, "Image_lingq", 397, 10, "res/custom/04mainui/04.png")
GUI:setContentSize(Image_lingq, 60, 30)
GUI:setTouchEnabled(Image_lingq, true)
-- 添加点击事件
GUI:addOnClickEvent(Image_lingq, function()
self:claimReward(name, curCfg.give)
end)
end
end
self:updatePageButtons()
end
-- 添加领取奖励函数(需要与后端通信)
function FuLiDaTing2:claimReward(itemId, reward)
-- 这里需要调用后端接口来领取奖励
-- 示例:发送网络请求给后端
SL:SendToServer("FuLiDaTing2_Claim", {itemId = itemId, reward = reward})
-- 领取后刷新界面
SL:ScheduleOnce(function()
self:updataUI()
end, 0.5)
end
-- 在后端同步数据时更新界面
function FuLiDaTing2:syncData(showUI)
-- 从后端获取最新的领取记录
SL:SendToServer("FuLiDaTing2_GetData", {})
-- 如果有UI显示需求,可以在这里处理
if showUI then
self:updataUI()
end
end
```
**关键修改点:**
1. **获取后端数据**:使用 `SL:GetMetaValue("SERVER_VALUE", "FuLiDaTing2_Record")` 获取后端保存的领取记录字符串
2. **判断领取状态**:通过 `string.find(serverData, checkKey)` 检查当前装备ID是否在领取记录中
3. **图片切换逻辑**:
- 已领取:显示 `"res/custom/04mainui/05.png"`(灰色/已激活)
- 未领取:显示 `"res/custom/04mainui/04.png"`(彩色/可领取)
4. **交互处理**:未领取的按钮可点击,触发领取功能
**注意**:后端需要确保 `SetStr(0, actor, self.VarDT, newData)` 保存的数据格式是逗号分隔的字符串(如:"50112,50113,50114,"),这样前端才能正确解析。
这样修改后,玩家拾取装备后,对应的按钮就会从彩色变为灰色,表示已领取状态。
← 返回问答库