完整的装备图鉴系统 一共25套装备,25个按钮 选择按钮后区别于其他按钮,每个按钮代表一套装备
🤖 回答引擎: 前端Lua |
🕒 时间: 2026-06-10 10:52:37 |
👁️ 阅读: 2
要实现一个完整的装备图鉴系统,包含25套装备和对应的25个按钮,并且选择按钮后有区别显示,可以按照以下步骤编写Lua脚本:
首先创建主界面作为父节点,然后使用循环创建25个按钮,并为每个按钮添加点击事件来切换选中状态。
```lua
-- 创建主界面父节点
local parent = GUI:Win_Create("EquipAlbum", 0, 0, 0, 0, false, false, true, false, false, false, 1)
-- 定义按钮位置和大小参数
local buttonWidth = 100
local buttonHeight = 40
local startX = 50
local startY = 500
local spacingX = 110
local spacingY = 50
local columns = 5
-- 存储所有按钮的表格
local equipButtons = {}
local selectedButton = nil
-- 创建25个装备按钮
for i = 1, 25 do
-- 计算按钮位置
local col = (i - 1) % columns
local row = math.floor((i - 1) / columns)
local posX = startX + col * spacingX
local posY = startY - row * spacingY
-- 创建按钮
local buttonName = "btn_equip_" .. i
local button = GUI:Button_Create(parent, buttonName, posX, posY, "res/public/1900000612.png")
-- 设置按钮属性
GUI:setContentSize(button, buttonWidth, buttonHeight)
GUI:setIgnoreContentAdaptWithSize(button, false)
GUI:Button_setTitleText(button, "装备" .. i)
GUI:Button_setTitleColor(button, "#ffffff")
GUI:Button_setTitleFontSize(button, 14)
GUI:Button_titleEnableOutline(button, "#000000", 1)
GUI:setAnchorPoint(button, 0.5, 0.5)
GUI:setTouchEnabled(button, true)
-- 存储按钮引用
equipButtons[i] = button
-- 为按钮添加点击事件
GUI:addOnClickEvent(button, function()
-- 如果已经有选中的按钮,恢复其正常状态
if selectedButton and not GUI:Win_IsNull(selectedButton) then
GUI:Button_loadTextureNormal(selectedButton, "res/public/1900000612.png")
GUI:Button_setTitleColor(selectedButton, "#ffffff")
end
-- 设置当前按钮为选中状态
GUI:Button_loadTextureNormal(button, "res/public/1900000610.png")
GUI:Button_setTitleColor(button, "#ffff00")
-- 更新选中的按钮
selectedButton = button
-- 显示对应的装备图鉴
showEquipAlbum(i)
end)
end
-- 显示装备图鉴的函数
function showEquipAlbum(equipSetId)
SL:print("显示第" .. equipSetId .. "套装备图鉴")
-- 这里可以添加显示具体装备图鉴内容的代码
-- 例如:创建装备图标、属性信息等
-- 示例:创建一个文本显示当前选中的装备套
local textName = "Text_currentSet"
local existingText = GUI:getChildByName(parent, textName)
if not GUI:Win_IsNull(existingText) then
GUI:removeFromParent(existingText)
end
local text = GUI:Text_Create(parent, textName, 300, 550, 20, "#ffff00", "当前选中:第" .. equipSetId .. "套装备")
GUI:Text_enableOutline(text, "#000000", 2)
end
-- 默认选中第一套装备
if equipButtons[1] and not GUI:Win_IsNull(equipButtons[1]) then
GUI:addOnClickEvent(equipButtons[1], function()
-- 模拟点击第一个按钮
local clickFunc = GUI:getOnClickEvent(equipButtons[1])
if clickFunc then
clickFunc()
end
end)
-- 触发第一次点击
local clickFunc = GUI:getOnClickEvent(equipButtons[1])
if clickFunc then
clickFunc()
end
end
```
这个脚本实现了以下功能:
1. **创建25个按钮**:使用循环创建5行5列的按钮网格
2. **按钮状态管理**:
- 正常状态使用`res/public/1900000612.png`作为背景
- 选中状态使用`res/public/1900000610.png`作为背景,文字颜色变为黄色
3. **点击事件处理**:点击按钮时,先恢复之前选中按钮的状态,然后设置当前按钮为选中状态
4. **装备图鉴显示**:点击按钮后调用`showEquipAlbum`函数显示对应的装备图鉴
**关键点说明**:
- 使用`GUI:Button_loadTextureNormal()`来切换按钮的正常状态图片
- 使用`GUI:Button_setTitleColor()`改变文字颜色来区分选中状态
- 使用`GUI:getChildByName()`和`GUI:removeFromParent()`来更新显示内容
- 通过`selectedButton`变量记录当前选中的按钮
你可以根据需要扩展`showEquipAlbum`函数,添加具体的装备图标、属性信息等内容显示逻辑。
← 返回问答库