增加一些物品的快捷使用
🤖 回答引擎: 前端Lua |
🕒 时间: 2026-03-09 12:26:20 |
👁️ 阅读: 12
要实现物品快捷使用功能,可以参考浮生梦老师前端课程中的`auto_use_pop.lua`界面设计。这个界面包含物品展示、名称、倒计时和使用按钮,可以在此基础上进行扩展。
以下是核心代码示例,用于创建快捷使用弹窗:
```lua
-- 创建父窗口(必须先创建界面)
local parent = GUI:Win_Create("QuickUseWindow", 0, 0, 0, 0, false, false, true, false, false, false, 1)
-- 创建快捷使用主布局
local PPopUI = GUI:Layout_Create(parent, "PPopUI", 0.00, 0.00, 171.00, 186.00, false)
GUI:setChineseName(PPopUI, "快捷使用组合")
GUI:setTouchEnabled(PPopUI, true)
GUI:setTag(PPopUI, -1)
-- 创建背景
local pBg = GUI:Image_Create(PPopUI, "pBg", 0.00, 0.00, "res/public/bg_hhdb_02.jpg")
GUI:setChineseName(pBg, "快捷使用_背景图")
GUI:setTouchEnabled(pBg, false)
GUI:setTag(pBg, -1)
-- 创建标题
local TextTitle = GUI:Text_Create(PPopUI, "TextTitle", 72.00, 164.00, 18, "#ffffff", "快捷使用")
GUI:setChineseName(TextTitle, "快捷使用_标题_文本")
GUI:setAnchorPoint(TextTitle, 0.50, 0.50)
GUI:setTouchEnabled(TextTitle, false)
GUI:setTag(TextTitle, -1)
GUI:Text_enableOutline(TextTitle, "#000000", 1)
-- 创建物品背景框
local ItemBg = GUI:Image_Create(PPopUI, "ItemBg", 72.00, 112.00, "res/public/1900000651.png")
GUI:setChineseName(ItemBg, "快捷使用_物品_背景框")
GUI:setAnchorPoint(ItemBg, 0.50, 0.50)
GUI:setTouchEnabled(ItemBg, false)
GUI:setTag(ItemBg, -1)
-- 创建物品节点(用于显示物品图标)
local ItemNode = GUI:Layout_Create(PPopUI, "ItemNode", 72.00, 112.00, 0.00, 0.00, false)
GUI:setChineseName(ItemNode, "快捷使用_物品_节点")
GUI:setAnchorPoint(ItemNode, 0.50, 0.50)
GUI:setTouchEnabled(ItemNode, false)
GUI:setTag(ItemNode, -1)
-- 创建物品名称文本
local TextName = GUI:Text_Create(PPopUI, "TextName", 72.00, 68.00, 16, "#00ff00", "")
GUI:setChineseName(TextName, "快捷使用_物品名称_文本")
GUI:setAnchorPoint(TextName, 0.50, 0.50)
GUI:setTouchEnabled(TextName, false)
GUI:setTag(TextName, -1)
GUI:Text_enableOutline(TextName, "#000000", 1)
-- 创建倒计时文本
local TextTime = GUI:Text_Create(PPopUI, "TextTime", 123.00, 35.00, 16, "#ffffff", "")
GUI:setChineseName(TextTime, "快捷使用_倒计时_文本")
GUI:setAnchorPoint(TextTime, 0.50, 0.50)
GUI:setTouchEnabled(TextTime, false)
GUI:setTag(TextTime, -1)
GUI:Text_enableOutline(TextTime, "#000000", 1)
-- 创建使用按钮
local BtnUse = GUI:Button_Create(PPopUI, "BtnUse", 72.00, 35.00, "res/public/1900000679.png")
GUI:Button_setTitleText(BtnUse, "使用")
GUI:Button_setTitleColor(BtnUse, "#f8e6c6")
GUI:Button_setTitleFontSize(BtnUse, 16)
GUI:Button_titleEnableOutline(BtnUse, "#000000", 1)
GUI:setChineseName(BtnUse, "快捷使用_使用_按钮")
GUI:setAnchorPoint(BtnUse, 0.50, 0.50)
GUI:setTouchEnabled(BtnUse, true)
GUI:setTag(BtnUse, -1)
-- 创建关闭按钮
local BtnClose = GUI:Button_Create(PPopUI, "BtnClose", 144.00, 144.00, "res/public/1900000510.png")
GUI:Button_loadTexturePressed(BtnClose, "res/public/1900000511.png")
GUI:Button_setTitleText(BtnClose, "")
GUI:Button_setTitleColor(BtnClose, "#ffffff")
GUI:Button_setTitleFontSize(BtnClose, 10)
GUI:Button_titleEnableOutline(BtnClose, "#000000", 1)
GUI:setChineseName(BtnClose, "快捷使用_关闭_按钮")
GUI:setTouchEnabled(BtnClose, true)
GUI:setTag(BtnClose, -1)
-- 添加按钮点击事件
GUI:addOnClickEvent(BtnUse, function()
SL:print("使用按钮被点击")
-- 这里可以添加快捷使用物品的逻辑
-- 例如:SL:RequestUseItem(itemID)
end)
GUI:addOnClickEvent(BtnClose, function()
SL:print("关闭快捷使用窗口")
if not GUI:Win_IsNull(parent) then
GUI:removeFromParent(parent)
end
end)
```
**功能扩展建议:**
1. **多物品支持**:可以在`ItemNode`中创建多个物品槽位,使用`ListView`或`PageView`来管理
2. **动态更新**:通过定时器更新倒计时`TextTime`的显示
3. **物品图标**:在`ItemNode`中添加`Image`控件来显示物品图标
4. **数据绑定**:从服务端获取快捷物品列表,使用`SL:GetMetaValue`获取物品信息
**使用注意事项:**
- 所有控件创建后都要检查是否为空:`if GUI:Win_IsNull(widget) then return end`
- 定时器使用规范:单次延迟用`SL:ScheduleOnce`,循环定时用`SL:Schedule`
- 坐标设置使用`GUI:setPosition(widget, x, y)`
- 获取坐标用`local pos = GUI:getPosition(widget)`,返回的是table
这个框架可以方便地扩展为支持多个物品的快捷使用界面,只需在`ItemNode`中添加更多物品槽位并绑定相应的事件即可。
← 返回问答库