-- 创建父窗口
local win = GUI:Win_Create("TrigWin", 100, 100, 300, 300, false, false, true, false, false, false, 1)
-- 创建显示结果的文本对象
local textResult = GUI:Text_Create(win, "text_result", 10, 150, 16, "#ffffff", "结果: 0")
-- 正弦按钮
local btnSin = GUI:Button_
🤖 回答引擎: 前端Lua |
🕒 时间: 2026-03-16 13:20:13 |
👁️ 阅读: 8
```lua
-- 创建父窗口
local win = GUI:Win_Create("TrigWin", 100, 100, 300, 300, false, false, true, false, false, false, 1)
-- 创建背景图片
local imgPath = "res/public/1900000600.png"
local Image_bg = GUI:Image_Create(win, "Image_bg", 150, 150, imgPath)
GUI:setAnchorPoint(Image_bg, 0.5, 0.5)
-- 创建显示结果的文本对象
local textResult = GUI:Text_Create(win, "text_result", 10, 150, 16, "#ffffff", "结果: 0")
-- 按钮1: 正弦运动 - 上下移动
local btnSin = GUI:Button_Create(win, "btn_sin", 10, 10, "res/public/1900000612.png")
GUI:Button_setTitleText(btnSin, "正弦运动")
GUI:addOnClickEvent(btnSin, function()
local startY = 150
local amplitude = 50 -- 振幅
local time = 2 -- 时间
local startTime = 0
local function update()
local t = startTime
local y = startY + amplitude * math.sin(t * math.pi * 2)
GUI:setPosition(Image_bg, 150, y)
startTime = startTime + 0.1
if startTime < time then
SL:ScheduleOnce(update, 0.1)
end
end
update()
end)
-- 按钮2: 余弦运动 - 左右移动
local btnCos = GUI:Button_Create(win, "btn_cos", 10, 50, "res/public/1900000612.png")
GUI:Button_setTitleText(btnCos, "余弦运动")
GUI:addOnClickEvent(btnCos, function()
local startX = 150
local amplitude = 50
local time = 2
local startTime = 0
local function update()
local t = startTime
local x = startX + amplitude * math.cos(t * math.pi * 2)
GUI:setPosition(Image_bg, x, 150)
startTime = startTime + 0.1
if startTime < time then
SL:ScheduleOnce(update, 0.1)
end
end
update()
end)
-- 按钮3: 圆周运动
local btnCircle = GUI:Button_Create(win, "btn_circle", 10, 90, "res/public/1900000612.png")
GUI:Button_setTitleText(btnCircle, "圆周运动")
GUI:addOnClickEvent(btnCircle, function()
local centerX, centerY = 150, 150
local radius = 50
local time = 2
local startTime = 0
local function update()
local t = startTime
local x = centerX + radius * math.cos(t * math.pi * 2)
local y = centerY + radius * math.sin(t * math.pi * 2)
GUI:setPosition(Image_bg, x, y)
startTime = startTime + 0.1
if startTime < time then
SL:ScheduleOnce(update, 0.1)
end
end
update()
end)
-- 按钮4: 旋转角度
local btnRotate = GUI:Button_Create(win, "btn_rotate", 120, 10, "res/public/1900000612.png")
GUI:Button_setTitleText(btnRotate, "旋转45°")
GUI:addOnClickEvent(btnRotate, function()
local currentRotation = 0
local targetRotation = 45
local steps = 10
local function rotateStep()
currentRotation = currentRotation + (targetRotation / steps)
GUI:setRotation(Image_bg, currentRotation)
if currentRotation < targetRotation then
SL:ScheduleOnce(rotateStep, 0.1)
end
end
rotateStep()
end)
-- 按钮5: 计算距离
local btnDistance = GUI:Button_Create(win, "btn_distance", 120, 50, "res/public/1900000612.png")
GUI:Button_setTitleText(btnDistance, "计算距离")
GUI:addOnClickEvent(btnDistance, function()
local pos = GUI:getPosition(Image_bg)
local distance = math.sqrt((pos.x - 150)^2 + (pos.y - 150)^2)
GUI:Text_setString(textResult, "距中心距离: " .. math.floor(distance))
end)
-- 按钮6: 计算角度
local btnAngle = GUI:Button_Create(win, "btn_angle", 120, 90, "res/public/1900000612.png")
GUI:Button_setTitleText(btnAngle, "计算角度")
GUI:addOnClickEvent(btnAngle, function()
local pos = GUI:getPosition(Image_bg)
local dx = pos.x - 150
local dy = pos.y - 150
local angle = math.deg(math.atan2(dy, dx))
GUI:Text_setString(textResult, "与X轴夹角: " .. math.floor(angle) .. "°")
end)
-- 按钮7: 重置位置
local btnReset = GUI:Button_Create(win, "btn_reset", 120, 130, "res/public/1900000612.png")
GUI:Button_setTitleText(btnReset, "重置位置")
GUI:addOnClickEvent(btnReset, function()
GUI:setPosition(Image_bg, 150, 150)
GUI:setRotation(Image_bg, 0)
GUI:Text_setString(textResult, "已重置到中心")
end)
-- 按钮8: 缩放效果
local btnScale = GUI:Button_Create(win, "btn_scale", 120, 170, "res/public/1900000612.png")
GUI:Button_setTitleText(btnScale, "缩放效果")
GUI:addOnClickEvent(btnScale, function()
local function callback()
SL:print("缩放完成")
end
GUI:runAction(Image_bg, GUI:ActionSequence(
GUI:ActionScaleTo(0.2, 1.5),
GUI:ActionScaleTo(0.2, 1.0),
GUI:CallFunc(callback)
))
end)
-- 按钮9: 淡入淡出
local btnFade = GUI:Button_Create(win, "btn_fade", 10, 130, "res/public/1900000612.png")
GUI:Button_setTitleText(btnFade, "淡入淡出")
GUI:addOnClickEvent(btnFade, function()
GUI:runAction(Image_bg, GUI:ActionSequence(
GUI:ActionFadeTo(0.5, 50),
GUI:DelayTime(0.5),
GUI:ActionFadeTo(0.5, 255)
))
end)
```
这个示例展示了如何通过三角函数控制图片的运动和旋转:
1. **正弦运动**:让图片在Y轴上做上下波动运动
2. **余弦运动**:让图片在X轴上做左右波动运动
3. **圆周运动**:结合sin和cos实现圆周运动
4. **旋转角度**:使用`GUI:setRotation()`控制旋转
5. **计算距离**:使用勾股定理计算与中心点的距离
6. **计算角度**:使用`math.atan2()`计算与X轴的夹角
7. **重置位置**:恢复到初始位置和角度
8. **缩放效果**:使用动作序列实现缩放动画
9. **淡入淡出**:使用`GUI:ActionFadeTo()`控制透明度变化
所有运动都基于三角函数原理,通过按钮点击触发不同的数学计算和动画效果。
← 返回问答库