function lib:create_attr_list(parent, cur_data, next_data, hide_bg, gap, attr_bg_hight, bg_img_path, bg_img_prefix,
offset_x, has_simple, name_offset_x)
GUI:removeAllChildren(parent)
attr_bg_hight = attr_bg_hight or 22
local parent_si
🤖 回答引擎: 前端Lua |
🕒 时间: 2026-04-22 13:03:25 |
👁️ 阅读: 13
看了你的代码,这是个创建属性列表的函数,整体结构不错,但有几个地方使用了浮生梦老师前端课程中不存在的函数,需要修改。
**问题点:**
1. **`GUI:moveBy(widget, time, x, y)`** - 这个函数在课程中不存在,应该使用动作序列来实现移动。
2. **`GUI:fadeIn(widget, time)`** - 这个函数也不存在,淡入效果应该使用`GUI:ActionFadeTo`动作。
3. **`GUI:UserUILayout(parent, config)`** - 这个函数在课程中未提及,可能是自定义的布局函数。
**修改后的代码:**
```lua
function lib:create_attr_list(parent, cur_data, next_data, hide_bg, gap, attr_bg_hight, bg_img_path, bg_img_prefix,
offset_x, has_simple, name_offset_x)
GUI:removeAllChildren(parent)
attr_bg_hight = attr_bg_hight or 22
local parent_size = GUI:getContentSize(parent)
for index = 1, next_data.show_attr_count do
--属性背景图
local attr_bg = nil
if not hide_bg then
if bg_img_path then
bg_img_path = bg_img_path or "res/custom/shuxing/bag.png"
attr_bg = GUI:Image_Create(parent, "attr_bg_" .. index, 0.00, 0.00, bg_img_path)
elseif bg_img_prefix then
attr_bg = GUI:Image_Create(parent, "attr_bg_" .. index, 0.00, 0.00, bg_img_prefix .. index .. ".png")
end
GUI:setTag(attr_bg, index)
else
attr_bg = GUI:Layout_Create(parent, "attr_bg_" .. index, 0.00, 0.00, parent_size.width, attr_bg_hight,
false)
GUI:setTag(attr_bg, index)
end
--属性值x偏移量
offset_x = offset_x or 0
local attr_bg_size = GUI:getContentSize(attr_bg)
attr_bg_size.width = attr_bg_size.width - offset_x
--属性名
if not next_data.hide_att_name and next_data["att_name_" .. index] then
next_data["att_name_" .. index] = next_data["att_name_" .. index] or ""
local name_color = SL:GetColorByStyleId(next_data.name_color) or "#ffffff"
local attr_name = GUI:Text_Create(attr_bg, "attr_name_" .. index, name_offset_x or 0, attr_bg_size.height / 2,
14, name_color, next_data["att_name_" .. index] .. ":")
GUI:setAnchorPoint(attr_name, 0, 0.5)
end
--指向箭头
local next_img = GUI:Image_Create(attr_bg, "next_img_" .. index, offset_x + attr_bg_size.width / 2,
attr_bg_size.height / 2)
GUI:setAnchorPoint(next_img, 0.50, 0.50)
local next_img_pos = GUI:getPosition(next_img)
local next_img_size = GUI:getContentSize(next_img)
--计算左侧x坐标范围
local left_x_range = { offset_x, next_img_pos.x - next_img_size.width / 2 + offset_x }
--计算右侧x坐标范围
local right_x_range = { next_img_pos.x + next_img_size.width / 2, offset_x + attr_bg_size.width }
--左侧当前属性
local cur_attr_val = cur_data["cur_attr_" .. index] or 0
if type(cur_attr_val) == "number" and has_simple then
cur_attr_val = SL:GetSimpleNumber(cur_attr_val, 2)
end
local cur_color = SL:GetColorByStyleId(cur_data.cur_color) or "#ffffff"
local cur_x_pos = left_x_range[2] / 2
local cur_att = GUI:Text_Create(attr_bg, "cur_att_" .. index, cur_x_pos, attr_bg_size.height / 2, 14,
cur_color, cur_attr_val)
GUI:setAnchorPoint(cur_att, 0.5, 0.5)
GUI:setOpacity(cur_att, 0)
-- 使用动作序列实现移动和淡入效果(替换不存在的moveBy和fadeIn)
GUI:runAction(cur_att, GUI:ActionSequence(
GUI:ActionMoveBy(0.3, -30, 0), -- 移动动作
GUI:ActionFadeTo(0.3, 255) -- 淡入到不透明
))
--右侧下级属性
local next_attr_val = next_data["cur_attr_" .. index] or 0
if type(next_attr_val) ~= "number" and has_simple then
next_attr_val = SL:GetSimpleNumber(next_attr_val, 2)
end
if next_data and next_attr_val then
local next_color = SL:GetColorByStyleId(next_data.next_color) or "#ffffff"
local next_x_pos = right_x_range[1] + ((right_x_range[2] - right_x_range[1]) / 2)
local next_att = GUI:Text_Create(attr_bg, "cur_attr_" .. index .. "_2", next_x_pos,
attr_bg_size.height / 2, 14, next_color, next_attr_val)
GUI:setAnchorPoint(next_att, 0.5, 0.5)
GUI:setOpacity(next_att, 0)
-- 使用延迟动作和淡入动作(替换不存在的fadeIn)
GUI:runAction(next_att, GUI:ActionSequence(
GUI:DelayTime(0.1), -- 延迟0.1秒
GUI:ActionFadeTo(0.4, 255) -- 淡入到不透明
))
end
--序列帧箭头
local frames = lib:frames(attr_bg, "/common", 1, 10, offset_x + attr_bg_size.width/2 , attr_bg_size.height / 2,
100, -1, 0, index)
GUI:setAnchorPoint(frames, 0.5, 0.5)
GUI:setScaleX(frames,0.5)
GUI:setScaleY(frames,0.5)
end
-- 注意:UserUILayout函数在课程中未提及,可能是自定义函数
-- 如果这个布局函数不存在,你需要自己实现布局逻辑
-- 这里保留原代码,但需要你确认这个函数是否存在
gap = gap or 10
GUI:UserUILayout(parent, {
dir = 1,
gap = { y = gap },
sortfunc = function(lists)
table.sort(lists, function(a, b)
return GUI:getTag(a) < GUI:getTag(b)
end)
end
})
end
```
**主要修改:**
1. **`GUI:moveBy`** → 替换为 `GUI:ActionMoveBy(time, x, y)` 放在动作序列中
2. **`GUI:fadeIn`** → 替换为 `GUI:ActionFadeTo(time, opacity)` 放在动作序列中
3. **`GUI:UserUILayout`** → 这个函数需要你确认是否存在,如果不存在需要自己写布局逻辑
**建议:**
- 如果 `GUI:UserUILayout` 不存在,你可以用循环设置每个属性背景的位置来实现垂直布局
- 记得在使用动作前先设置初始状态(比如透明度为0)
- 动作序列可以组合多个动作,按顺序执行
下次写代码时,如果不确定某个函数是否存在,可以先查一下浮生梦老师的课程文档,或者先用 `SL:print()` 测试一下。不懂的地方随时问我!
← 返回问答库