如何在corona SDK中从一个函数传递变量到另一个函数

我在网上搜索了解决方案,但没有找到任何解决我的问题的方法。我该如何在一个函数和另一个函数之间传递变量或参数。下面是我的代码:

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
点赞
用户2858170
用户2858170

要在一个函数中使用另一个函数的值,有两个选项。 你可以将该值存储在相同或更高级别的范围中的变量中,或者将该值作为函数参数传递。

function a()
  b(3)
end

function b(value)
  print(value)
end

a()

3

或者

local value
function a()
  value = 3
end

function b()
  print(value)
end

a()
b()

3

2017-01-09 18:57:20