Hello,
In an enterFrame function how to constantly check for the closest enemy to the player and point a variable to that enemy?
Here is an illustration…
Thanks in advance!!!
Hello,
In an enterFrame function how to constantly check for the closest enemy to the player and point a variable to that enemy?
Here is an illustration…
Thanks in advance!!!
use Pythagorean Theorem to calculate distance
Thanks @jstrahan for the reply but I don’t get it, plus i suck in maths
Can you please provide an example please?
If you’re enemies are in a table/array, then I would so something like this:
local function distBetween( point1, point2 ) local xFactor = point2.x-point1.x local yFactor = point2.y-point1.y local dist = math\_sqrt((xFactor\*xFactor) + (yFactor\*yFactor)) return dist end local closestEnemy = 1 local closestDistance = 1000000000000 for i = 1, #enemies do local d = distBetween(player, enemies[i]) if d \< closestDistance then closestDistance = d closestEnemy = i end end -- closest enemy is enemies[closestEnemy]
or something like that.
Thanks Rob for the method,
can i use it in a enterFrame event constantly? wouldn’t it affect the performance?
note that you don’t actually need the sqrt(), closest “distance squared” gives same result:
local dx = enemy.x - player.x
local dy = enemy.y - player.y
local distsq = dx*dx+dy*dy
if (distsq < closest_distsq_so_far) then…
This is my solution!!!
I have to run it in enterFrame so is it ok?
players = {} local nearestBot = nil local nearestDist = 10000 local player = display.newCircle( 250, 200, 30 ) for i=1, 4 do local bot = display.newCircle( 100\*i, 100, 30 ) bot.name = "Bot"..i players[i] = bot end for i=1, #players do local distance = math.sqrt((player.x-players[i].x)^2+(player.y-players[i].y)^2) if (distance \< nearestDist)then nearestDist = distance nearestBot = players[i] end end print(nearestBot.name)
I would consider following Dave’s advice and dropping the square root calculation out. Since you don’t need the actual distance, just the relative distances apart, then the math.sqrt() is just adding overhead in a part of your app were efficiency is key. Also, if you do need the distance you would want to localize it by adding a
local sqrt = math.sqrt
at the top of the function, but since it’s really not needed here, I’d just git rid of it. I don’t know if the ^2 operator is more efficient than multiplying them together.
It’s fine to use it in an enterFrame listener, I imagine you have no other choice.
One thing you will need to do is reset the closestDistance variable after the for loop ends, otherwise it will get lower and lower until it is a value less than the distance to the nearest enemy.
use Pythagorean Theorem to calculate distance
Thanks @jstrahan for the reply but I don’t get it, plus i suck in maths
Can you please provide an example please?
If you’re enemies are in a table/array, then I would so something like this:
local function distBetween( point1, point2 ) local xFactor = point2.x-point1.x local yFactor = point2.y-point1.y local dist = math\_sqrt((xFactor\*xFactor) + (yFactor\*yFactor)) return dist end local closestEnemy = 1 local closestDistance = 1000000000000 for i = 1, #enemies do local d = distBetween(player, enemies[i]) if d \< closestDistance then closestDistance = d closestEnemy = i end end -- closest enemy is enemies[closestEnemy]
or something like that.
Thanks Rob for the method,
can i use it in a enterFrame event constantly? wouldn’t it affect the performance?
note that you don’t actually need the sqrt(), closest “distance squared” gives same result:
local dx = enemy.x - player.x
local dy = enemy.y - player.y
local distsq = dx*dx+dy*dy
if (distsq < closest_distsq_so_far) then…
This is my solution!!!
I have to run it in enterFrame so is it ok?
players = {} local nearestBot = nil local nearestDist = 10000 local player = display.newCircle( 250, 200, 30 ) for i=1, 4 do local bot = display.newCircle( 100\*i, 100, 30 ) bot.name = "Bot"..i players[i] = bot end for i=1, #players do local distance = math.sqrt((player.x-players[i].x)^2+(player.y-players[i].y)^2) if (distance \< nearestDist)then nearestDist = distance nearestBot = players[i] end end print(nearestBot.name)
I would consider following Dave’s advice and dropping the square root calculation out. Since you don’t need the actual distance, just the relative distances apart, then the math.sqrt() is just adding overhead in a part of your app were efficiency is key. Also, if you do need the distance you would want to localize it by adding a
local sqrt = math.sqrt
at the top of the function, but since it’s really not needed here, I’d just git rid of it. I don’t know if the ^2 operator is more efficient than multiplying them together.
It’s fine to use it in an enterFrame listener, I imagine you have no other choice.
One thing you will need to do is reset the closestDistance variable after the for loop ends, otherwise it will get lower and lower until it is a value less than the distance to the nearest enemy.