MainRightTop = {}
local buttonLayout, topIcons, Button
local IconData = {
{
{ notes = "在线充值按钮", path = "res/custom/top/11.png" },
{ notes = "狂暴之力按钮", path = "res/custom/top/12.png" },
{ notes = "首领挑战按钮", path = "res/custom/top/13.png" },
},
🤖 回答引擎: 前端Lua |
🕒 时间: 2026-04-20 18:58:49 |
👁️ 阅读: 10
根据浮生梦老师前端课程内容,你的代码中移动端点击地图伸缩没有触发事件的原因是没有正确注册移动端的小地图状态变化事件。
在移动端模式下,小地图的伸缩状态变化需要通过 `SL:RegisterLUAEvent` 来监听 `LUA_EVENT_MINIMAP_STATUS_CHANGE` 事件,而不是 `LUA_EVENT_PCMINIMAP_STATUS_CHANGE`。
修改 `MainRightTop.initIconData` 函数中的事件注册部分:
```lua
function MainRightTop.initIconData(topIcons)
local iconWidth = 80
local iconHeight = 80
local iconPaddingX = 10 -- 图标之间的水平间隔
local tableLen1 = MainRightTop.getTableLen(IconData[1])
local tableLen2 = MainRightTop.getTableLen(IconData[2])
local tableLen3 = MainRightTop.getTableLen(IconData[3])
-- 根据当前操作模式注册不同的事件
local isOnPC = SL:GetMetaValue("CURRENT_OPERMODE")
if isOnPC == 1 then
-- PC端注册PC小地图状态变化事件
SL:RegisterLUAEvent("LUA_EVENT_PCMINIMAP_STATUS_CHANGE", "MainRightTop", MainRightTop.PcMiniMapCHANGE)
else
-- 移动端注册小地图状态变化事件
SL:RegisterLUAEvent("LUA_EVENT_MINIMAP_STATUS_CHANGE", "MainRightTop", MainRightTop.MobileMiniMapCHANGE)
end
-- 循环创建一行图标
for col = 1, tableLen1 do
local iconX = 360 - (iconWidth + iconPaddingX) * (col - 1) ---数字越大越往右靠
local iconY = iconHeight * 2
local path = IconData[1][col].path
-- 创建图标,使用 iconX 和 iconY 作为左上角坐标
local button = GUI:Button_Create(topIcons, "icon1_" .. col, iconX, iconY, path)
GUI:addOnClickEvent(button, function()
SL:SendNetMsg(99, 0, 0, 0, IconData[1][col].notes)
end)
end
-- 循环创建二行图标
for col = 1, tableLen2 do
local iconX = 360 - (iconWidth + iconPaddingX) * (col - 1) ---数字越大越往右靠
local iconY = iconHeight
local path = IconData[2][col].path
-- 创建图标,使用 iconX 和 iconY 作为左上角坐标
local button = GUI:Button_Create(topIcons, "icon2_" .. col, iconX, iconY, path)
GUI:addOnClickEvent(button, function()
SL:SendNetMsg(99, 0, 0, 0, IconData[2][col].notes)
end)
end
-- 循环创建三行图标
for col = 1, tableLen3 do
local iconX = 360 - (iconWidth + iconPaddingX) * (col - 1) ---数字越大越往右靠
local iconY = 0
local path = IconData[3][col].path
-- 创建图标,使用 iconX 和 iconY 作为左上角坐标
local button = GUI:Button_Create(topIcons, "icon3_" .. col, iconX, iconY, path)
GUI:addOnClickEvent(button, function()
SL:SendNetMsg(99, 0, 0, 0, IconData[3][col].notes)
end)
end
end
```
然后需要添加移动端的事件处理函数:
```lua
-- 移动端小地图状态变化事件处理
-- status: 0-隐藏 1-显示小地图 2-显示大地图
function MainRightTop.MobileMiniMapCHANGE(status)
if status == 0 then
-- 小地图隐藏
GUI:Timeline_MoveTo(buttonLayout, { x = -500, y = -265 }, 0.1)
GUI:Timeline_MoveTo(Button, { x = -55, y = -140 }, 0.1)
elseif status == 1 then
-- 显示小地图
GUI:Timeline_MoveTo(buttonLayout, { x = -665, y = -265 }, 0.1)
GUI:Timeline_MoveTo(Button, { x = -220, y = -140 }, 0.1)
elseif status == 2 then
-- 显示大地图界面
GUI:Timeline_MoveTo(buttonLayout, { x = -500, y = -265 }, 0.1)
GUI:Timeline_MoveTo(Button, { x = -55, y = -140 }, 0.1)
else
-- 默认情况
GUI:Timeline_MoveTo(buttonLayout, { x = -665, y = -265 }, 0.1)
GUI:Timeline_MoveTo(Button, { x = -220, y = -140 }, 0.1)
end
end
```
主要问题是你只注册了PC端的事件 `LUA_EVENT_PCMINIMAP_STATUS_CHANGE`,移动端需要使用 `LUA_EVENT_MINIMAP_STATUS_CHANGE` 事件。这两个事件对应不同的操作模式,参数含义也不同。
注意:移动端的小地图状态只有3种(0隐藏、1小地图、2大地图),而PC端有4种状态(0不显示、1小尺寸、2大尺寸、3打开界面)。下次写代码时要仔细区分不同平台的事件哦!
← 返回问答库