如何在 Corona SDK 中将变量从一个函数传递到另一个函数
How to pass variables from one function to another function in corona SDK
我在 Internet 上搜索了解决方案,但找不到任何解决我问题的方法。如何将变量或参数从一个函数传递到另一个函数。这是我的代码:
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
要在另一个函数中使用一个函数的值,您有两种选择。
您要么将该值存储在相同或更高范围内的变量中,要么将该值作为函数参数传递给。
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
我在 Internet 上搜索了解决方案,但找不到任何解决我问题的方法。如何将变量或参数从一个函数传递到另一个函数。这是我的代码:
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
要在另一个函数中使用一个函数的值,您有两种选择。
您要么将该值存储在相同或更高范围内的变量中,要么将该值作为函数参数传递给。
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