QuestionEverything
QuestionEverything

Reputation: 65

How to pass variables from one function to another function in corona SDK

I've searched on the internet for a solution but can't find anything that solves my problem. How do I pass a variable or parameter from one function to another. Here is my code:

local move
local distanceBetween
local ball
local finishX
local finishY

function move()
  ball.x = display.contentWidth/2
  ball.y = display.contentWidth-display.contentWidth-ball.contentWidth*2
  finishX = display.contentWidth/2
  finishY = display.contentHeight+ball.contentWidth/2
transition.to(ball, {x=finishX, y=finishY, time=travTime,onComplete=move5})
  end

function distanceBetween() 
factor = { x = finishX - ball.x, y = finishY - ball.y }
distanceBetween =math.sqrt( ( factor.x * factor.x ) + ( factor.y * factor.y ) )
return distanceBetween
end

Upvotes: 0

Views: 1235

Answers (1)

Piglet
Piglet

Reputation: 28950

To use a value from one function in another you have two options.

You either store that value in a variable that is in the same or in a superior scope or you pass the value to as a function argument.

function a()
  b(3)
end

function b(value)
  print(value)
end

a()

3

or

local value
function a()
  value = 3
end

function b()
  print(value)
end

a()
b()

3

Upvotes: 1

Related Questions