给我完整的代码
🤖 回答引擎: 前端Lua |
🕒 时间: 2026-03-07 18:22:40 |
👁️ 阅读: 7
你好学员!根据浮生梦老师前端课程内容,我看到了一个转盘系统的案例,现在为你整理出完整的代码框架。
这个转盘系统包含三个主要文件:配置文件、UI对象文件和主逻辑文件。下面是完整的代码:
## 1. 配置文件 (zhuanpanCFG.lua)
```lua
local config = {
[1] = {
["id"] = 100004,
["reward"] = {
[1] = {
[1] = 4,
[2] = 1000000,
},
},
["showReward"] = {
[1] = {
[1] = 4,
[2] = 1000000,
},
},
["index2"] = 1,
["probability"] = 1000,
["desc"] = "福利转盘",
["anparam"] = 38,
},
[2] = {
["id"] = 100004,
["reward"] = {
[1] = {
[1] = 4,
[2] = 1000000,
},
},
["showReward"] = {
[1] = {
[1] = 4,
[2] = 1000000,
},
},
["index2"] = 2,
["probability"] = 1000,
["desc"] = "福利转盘",
["anparam"] = 38,
},
[3] = {
["id"] = 100004,
["reward"] = {
[1] = {
[1] = 273,
[2] = 2,
},
},
["showReward"] = {
[1] = {
[1] = 273,
[2] = 2,
},
},
["index2"] = 3,
["probability"] = 1000,
["desc"] = "福利转盘",
["anparam"] = 38,
},
[4] = {
["id"] = 100004,
["reward"] = {
[1] = {
[1] = 297,
[2] = 3,
},
},
["showReward"] = {
[1] = {
[1] = 297,
[2] = 3,
},
},
["index2"] = 4,
["probability"] = 200,
["desc"] = "福利转盘",
["anparam"] = 38,
},
[5] = {
["id"] = 100004,
["reward"] = {
[1] = {
[1] = 4,
[2] = 5000000,
},
},
["showReward"] = {
[1] = {
[1] = 4,
[2] = 5000000,
},
},
["index2"] = 5,
["probability"] = 200,
["desc"] = "福利转盘",
["anparam"] = 38,
},
[6] = {
["id"] = 100004,
["reward"] = {
[1] = {
[1] = 308,
[2] = 1,
},
},
["showReward"] = {
[1] = {
[1] = 308,
[2] = 1,
},
},
["index2"] = 6,
["probability"] = 200,
["desc"] = "福利转盘",
["anparam"] = 38,
},
[7] = {
["id"] = 100004,
["reward"] = {
[1] = {
[1] = 281,
[2] = 200,
},
},
["showReward"] = {
[1] = {
[1] = 281,
[2] = 200,
},
},
["index2"] = 7,
["probability"] = 200,
["desc"] = "福利转盘",
["anparam"] = 38,
},
[8] = {
["id"] = 100004,
["reward"] = {
[1] = {
[1] = 280,
[2] = 50,
},
},
["showReward"] = {
[1] = {
[1] = 280,
[2] = 50,
},
},
["index2"] = 8,
["probability"] = 1000,
["desc"] = "福利转盘",
["anparam"] = 38,
},
[9] = {
["id"] = 100004,
["reward"] = {
[1] = {
[1] = 249,
[2] = 3,
},
},
["showReward"] = {
[1] = {
[1] = 249,
[2] = 3,
},
},
["index2"] = 9,
["probability"] = 500,
["desc"] = "福利转盘",
["anparam"] = 38,
},
}
return config
```
## 2. UI对象文件 (zhuanpanOBJ.lua)
```lua
local zhuanpanOBJ = {}
zhuanpanOBJ.Name = "zhuanpanOBJ"
zhuanpanOBJ.cfg = SL:Require("GUILayout/A/zhuanpanCFG",true)
function zhuanpanOBJ:main()
local parent = GUI:Win_Create(self.Name, 0, 0, 0, 0, false, false, true, false)
self._parent = parent
self.old_idx = nil
--加载UI
GUI:LoadExport(parent, "A/zhuanpanUI",function ()
self.ui = GUI:ui_delegate(parent)
--背景关闭
GUI:addOnClickEvent(self.ui.CloseLayout, function()
GUI:Win_Close(self._parent)
end)
--关闭按钮
GUI:addOnClickEvent(self.ui.CloseButton, function()
GUI:Win_Close(self._parent)
end)
--给服务端发消息
-- 这里可以添加发送消息的逻辑
end)
end
return zhuanpanOBJ
```
## 3. 主逻辑文件 (包含转盘动画)
```lua
local zhuanpan = {}
function zhuanpan.main()
-- 创建主窗口
local parent = GUI:Win_Create("ZhuanpanMain", 0, 0, 0, 0, false, false, true, false, false, false, 1)
-- 创建转盘布局
local layout = GUI:Widget_Create(parent, "layout", 0, 0, 800, 600)
-- 添加背景
local bg = GUI:Image_Create(layout, "bg", 0, 0, "res/public_win32/1900000610.png")
if bg then
GUI:setAnchorPoint(bg, 0.5, 0.5)
GUI:setPosition(bg, 400, 300)
end
-- 转盘逻辑变量
local items = {}
local isRotating = false
local currentIndex = 1
local loopCount = 0
local maxLoops = 2
local targetIndex = 1
-- 创建转盘项
local function createItems()
for i = 1, 9 do
local item = GUI:Widget_Create(layout, "item_" .. i, 0, 0, 80, 80)
if item then
local angle = (i - 1) * 40
local radius = 200
local x = 400 + radius * math.cos(math.rad(angle))
local y = 300 + radius * math.sin(math.rad(angle))
GUI:setPosition(item, x, y)
-- 添加奖励图标
local icon = GUI:Image_Create(item, "icon", 0, 0, "res/01/010020.png")
if icon then
GUI:setAnchorPoint(icon, 0.5, 0.5)
GUI:setPosition(icon, 40, 40)
end
-- 添加奖励文字
local text = GUI:Text_Create(item, "text", 0, -20, 14, "#ffffff", "奖励" .. i)
if text then
GUI:setAnchorPoint(text, 0.5, 0.5)
GUI:Text_setFontName(text, "fonts/font5.ttf")
end
table.insert(items, item)
end
end
end
-- 转盘动画
local function startRotation(target)
if isRotating then return end
isRotating = true
targetIndex = target or math.random(1, 9)
local index = 1
local count = #items
local time = 0.1
loopCount = 0
local function show()
if not isRotating then return end
-- 隐藏所有项
for i, item in ipairs(items) do
GUI:setVisible(item, false)
end
-- 显示当前项
if items[index] then
GUI:setVisible(items[index], true)
-- 添加缩放动画
local function callback()
SL:Print("动画完成")
end
GUI:runAction(items[index], GUI:ActionSequence(
GUI:ActionScaleTo(0.1, 1.4),
GUI:ActionScaleTo(0.1, 1),
GUI:CallFunc(callback)
))
end
-- 更新时间和索引
time = time - 0.1
time = math.max(time, 0.03)
index = index + 1
if index > count then
index = 1
loopCount = loopCount + 1
end
-- 检查是否到达目标
if loopCount >= maxLoops and index == targetIndex then
isRotating = false
SL:Print("转盘停止在位置:" .. targetIndex)
return
end
-- 继续动画
SL:ScheduleOnce(function()
show()
end, time)
end
show()
end
-- 创建开始按钮
local buttonStart = GUI:Button_Create(layout, "buttonStart", 400, 100, "res/public_win32/1900000612.png")
if buttonStart then
GUI:setAnchorPoint(buttonStart, 0.5, 0.5)
GUI:Button_setTitleText(buttonStart, "开始转盘")
GUI:Button_setTitleFontSize(buttonStart, 16)
GUI:addOnClickEvent(buttonStart, function()
if not isRotating then
startRotation()
end
end)
end
-- 创建关闭按钮
local buttonClose = GUI:Button_Create(layout, "buttonClose", 700, 500, "res/public_win32/1900000612.png")
if buttonClose then
GUI:setAnchorPoint(buttonClose, 0.5, 0.5)
GUI:Button_setTitleText(buttonClose, "关闭")
GUI:Button_setTitleFontSize(buttonClose, 16)
GUI:addOnClickEvent(buttonClose, function()
GUI:Win_Close(parent)
end)
end
-- 初始化转盘项
createItems()
end
-- 启动转盘
zhuanpan.main()
```
## 使用说明:
1. **配置文件**:定义了9个转盘奖励项,包含奖励ID、数量、概率等信息
2. **UI对象**:处理窗口创建和基础UI事件
3. **主逻辑**:实现转盘动画效果,包含:
- 圆形排列的奖励项
- 旋转动画效果
- 缩放高亮特效
- 开始/停止控制
## 关键函数说明:
- `GUI:Win_Create()`:创建窗口
- `GUI:Widget_Create()`:创建控件
- `GUI:Image_Create()`:创建图片
- `GUI:Text_Create()`:创建文字
- `GUI:Button_Create()`:创建按钮
- `GUI:runAction()`:运行动作序列
- `GUI:ActionSequence()`:创建顺序动作
- `GUI:ActionScaleTo()`:缩放动作
- `SL:ScheduleOnce()`:单次延迟执行
这个转盘系统使用了浮生梦老师教授的前端技术,包括UI创建、动画控制和事件处理。如果需要调整转盘速度、奖励数量或动画效果,可以修改对应的参数。
记得在实际使用时,需要根据项目结构调整资源路径和配置文件位置哦!
← 返回问答库