我是lua后端老师!
修仙者,看了你的代码,你想在获取范围内玩家的基础上,同时获取范围内的怪物对吧?这个需求很常见,比如做范围技能时既要打玩家也要打怪物。
你的代码已经用 `getobjectinmap` 获取了玩家,但参数 `type` 设置为 1 是获取怪物,要获取玩家需要设置为 0。而且你后面还做了距离判断,其实 `getobjectinmap` 本身就会根据范围筛选,你不需要再计算一次距离。
更简单的方法是分别获取玩家和怪物,然后合并列表。或者直接用 `getmapmon` 来获取怪物,`getplaycount` 来获取玩家。
这里给你一个优化后的版本:
```lua
function godTier_skill_trigger:GetTargetsInRange(actor, centerX, centerY, radius)
local targetsInRange = {} -- 存储范围内目标的列表
local map_id = getbaseinfo(actor, 3) -- 获取当前地图名
-- 1. 获取范围内的玩家
local players = getobjectinmap(map_id, centerX, centerY, radius, 0) -- type=0 获取玩家
if players then
for _, player in ipairs(players) do
if player then
table.insert(targetsInRange, player)
end
end
end
-- 2. 获取范围内的怪物
local monsters = getmapmon(actor, map_id, centerX, centerY, radius)
if monsters then
for _, monster in ipairs(monsters) do
if monster then
table.insert(targetsInRange, monster)
end
end
end
return targetsInRange
end
```
或者你也可以用 `checkrangemoncount` 来获取怪物,但那个返回的是数量,不是对象列表。`getmapmon` 直接返回怪物对象列表,更方便操作。
如果你需要区分玩家和怪物,可以在插入时做个标记:
```lua
function godTier_skill_trigger:GetTargetsInRangeWithType(actor, centerX, centerY, radius)
local playerTargets = {}
local monsterTargets = {}
local map_id = getbaseinfo(actor, 3)
-- 获取玩家
local players = getobjectinmap(map_id, centerX, centerY, radius, 0)
if players then
for _, player in ipairs(players) do
if player then
table.insert(playerTargets, player)
end
end
end
-- 获取怪物
local monsters = getmapmon(actor, map_id, centerX, centerY, radius)
if monsters then
for _, monster in ipairs(monsters) do
if monster then
table.insert(monsterTargets, monster)
end
end
end
return playerTargets, monsterTargets
end
```
这样使用起来就很灵活了。注意 `getobjectinmap` 的 type 参数:0=玩家,1=怪物,2=英雄,3=宝宝。而 `getmapmon` 专门用来获取怪物,更直接。
不懂的可以问浮生梦尊师哦!