根据浮生梦老师前端课程内容,要调整飞剑轨道位置,需要获取玩家坐标作为中心点。以下是调整方案:
```lua
-- 获取玩家当前坐标
local playerID = SL:GetMetaValue("USER_ID")
local centerX = SL:GetMetaValue("ACTOR_POSITION_X", playerID)
local centerY = SL:GetMetaValue("ACTOR_POSITION_Y", playerID)
-- 如果坐标不存在,设置默认值
if not centerX then centerX = 0 end
if not centerY then centerY = 0 end
-- 设置飞剑环绕的椭圆轨道参数
local orbitIndex = 1 -- 第1把飞剑的位置索引
local radiusX = CONST.ORBIT_RADIUS_X[orbitIndex] or 80
local radiusY = CONST.ORBIT_RADIUS_Y[orbitIndex] or 40
local offsetAngle = CONST.ORBIT_OFFSET_ANGLE[orbitIndex] or 0
-- 计算飞剑在椭圆轨道上的位置
local currentTime = os.clock()
local angle = CONST.ORBIT_SPEED[orbitIndex] * currentTime + offsetAngle
local swordX = centerX + radiusX * math.cos(angle)
local swordY = centerY + radiusY * math.sin(angle) + CONST.OFFSET_Y
-- 如果是GUI控件,设置位置
if GUI:Win_IsNull(feiId) == false then
GUI:setPosition(feiId, swordX, swordY)
end
-- 如果是粒子特效,也需要调整位置
if hitEffId and GUI:Win_IsNull(hitEffId) == false then
GUI:setPosition(hitEffId, swordX, swordY)
end
```
主要调整点:
1. 使用 `SL:GetMetaValue("ACTOR_POSITION_X", playerID)` 获取玩家X坐标
2. 使用 `SL:GetMetaValue("ACTOR_POSITION_Y", playerID)` 获取玩家Y坐标
3. 椭圆轨道计算时以玩家坐标为基准
4. 添加了 `CONST.OFFSET_Y` 的Y轴偏移
5. 使用 `GUI:Win_IsNull()` 判断控件是否有效
这样飞剑就会始终围绕玩家旋转,不会偏离人物位置了。