Distance calculator

Right, I have 2 objects in a side scrolling game.

what I would like to do is add a counter on screen showing the distance between the two objects (player) and (enemy).

I currently have this code acting as the distance section:

– distance 

function distance(enemy,player)

return math.sqrt((enemy.x-player.x)^2)

end

– distance text

local distance_text = display.newText("", 0, 0, native.systemFont, 14)

distance_text.anchorX = 0,0

distance_text.anchorY = 0,0

distance_text:setTextColor(0,0,0)

end

however it only displays whatever is the “”

that code is probably completely wrong but… 

how do I get it to dynamically show the distance between the two objects?

would it be some sort of onEveryFrame thing?

thanks in advance  :slight_smile:

Firstly, pre-declare local distance_text near the top of your lua file to avoid scope problems later. Then add an enterFrame listener like this:

[lua]

local distance_text

local function distance(enemy,player)

    return math.sqrt((enemy.x-player.x)^2)

end

local gameLoop = function ()

  distance_text.text = distance(enemy, player)

  distance_text.anchorX = 0,0

  distance_text.anchorY = 0,0

  distance_text.x = 0

  distance_text.y = 0

end

distance_text = display.newText("", 0, 0, native.systemFont, 14)

distance_text.anchorX = 0,0

distance_text.anchorY = 0,0

distance_text:setTextColor(0,0,0)

Runtime:addEventListener(“enterFrame”,gameLoop)  – make sure player and enemy are ready before doing this

[/lua]

Thanks for the help, do you know how you would limit the amount of digits shown to only be whole numbers? also i’m guessing the distance is measured in pixels?

[lua]

distance_text.text = math.round(distance(enemy, player))

[/lua]

Yes the difference is the number of pixels, depending on the dimensions you specify in config.lua. For instance I always work with a 480 x 320 screen size as that’s what I’m used to, so objects on opposite ends of a landscape screen would be around 480 pixels apart. If you work with a 1024 x 768 screen size they’d report a distance of around 1024.

Firstly, pre-declare local distance_text near the top of your lua file to avoid scope problems later. Then add an enterFrame listener like this:

[lua]

local distance_text

local function distance(enemy,player)

    return math.sqrt((enemy.x-player.x)^2)

end

local gameLoop = function ()

  distance_text.text = distance(enemy, player)

  distance_text.anchorX = 0,0

  distance_text.anchorY = 0,0

  distance_text.x = 0

  distance_text.y = 0

end

distance_text = display.newText("", 0, 0, native.systemFont, 14)

distance_text.anchorX = 0,0

distance_text.anchorY = 0,0

distance_text:setTextColor(0,0,0)

Runtime:addEventListener(“enterFrame”,gameLoop)  – make sure player and enemy are ready before doing this

[/lua]

Thanks for the help, do you know how you would limit the amount of digits shown to only be whole numbers? also i’m guessing the distance is measured in pixels?

[lua]

distance_text.text = math.round(distance(enemy, player))

[/lua]

Yes the difference is the number of pixels, depending on the dimensions you specify in config.lua. For instance I always work with a 480 x 320 screen size as that’s what I’m used to, so objects on opposite ends of a landscape screen would be around 480 pixels apart. If you work with a 1024 x 768 screen size they’d report a distance of around 1024.