function return

hi, (I’m sorry this is really a beginner question)

i have again a problem to return a value from a function. what’s the good syntax ? thanks for your help.

in my case baba.x=timer*10 doesn’t works…why?

local baba=display.newImage( "my5.png" ) baba.x=340 baba.y=100 local function scaleOut () transition.scaleBy(bulle, { time=1500, xScale=-0.6, yScale=-0.6 }) local timer=0.6 return timer end scaleOut () baba.x=timer\*10

You have to store the return of the function somewhere like this:

[lua]

local scaleOutResult = scaleOut()

[/lua]

So now whatever timer is when the scaleOut function finishes, will now be stored in scaleOutResult.

You can return multiple values like this:

[lua]

local test = function (in)

      local out5 = in * 5

      local out50 = in * 50

      local out150 = in * 150

      return out5, out50, out150

end

local result1, result2, result3 = test( 3.8 )

print (result1. ." " …r esult2. ." " …r esult3)

[/lua]

Here, the variables out5, out50 and out150 are returned by the function and stored in result1, result2 and result3. The function takes a number as a parameter (in) and uses it to calculate the values.

A quick way of fixing your code above is to do:

[lua]

baba.x = scaleOut() * 10

[/lua]

hello and thank you for the reply.
I thought I understood but completing my code with your information it does not work … and I’m looking but I do not freelance, where is the error

local function myTapListener(event)   local positionPersonnagex=event.x   local positionPersonnagey=event.y   transition.from( animationPersonnage, { tag="animationPersonnagedeplacement", time=1000, x=positionPersonnagex, y=positionPersonnagey } )   return positionPersonnagex, positionPersonnagey end background:addEventListener("tap", myTapListener ) local result1, result2 = myTapListener () animationPersonnage.x = result1 animationPersonnage.y = result2

When you call ‘result1, result 2 = myListener ()’ , you have not provided myTapListener with the parameter (event) that it requires. So the results will come back as nil.

What is it you are trying to achieve? When you tap the screen, your object moves to where your finger tapped?

Sorry, i have already an error but I informed that you specified.

yes i would move my object (a sprite animation) to the tap location… i know that there is an example code in corona but i want understand by myself and with the generous help of the forum members :slight_smile:

local function myTapListener(event)   local positionPersonnagex=event.x   local positionPersonnagey=event.y   transition.from( animationPersonnage, { tag="animationPersonnagedeplacement", time=1000, x=positionPersonnagex, y=positionPersonnagey } )   return positionPersonnagex, positionPersonnagey end background:addEventListener("tap", myTapListener ) local result1, result2 = myTapListener(event) animationPersonnage.x = result1 animationPersonnage.y = result2

You have to store the return of the function somewhere like this:

[lua]

local scaleOutResult = scaleOut()

[/lua]

So now whatever timer is when the scaleOut function finishes, will now be stored in scaleOutResult.

You can return multiple values like this:

[lua]

local test = function (in)

      local out5 = in * 5

      local out50 = in * 50

      local out150 = in * 150

      return out5, out50, out150

end

local result1, result2, result3 = test( 3.8 )

print (result1. ." " …r esult2. ." " …r esult3)

[/lua]

Here, the variables out5, out50 and out150 are returned by the function and stored in result1, result2 and result3. The function takes a number as a parameter (in) and uses it to calculate the values.

A quick way of fixing your code above is to do:

[lua]

baba.x = scaleOut() * 10

[/lua]

hello and thank you for the reply.
I thought I understood but completing my code with your information it does not work … and I’m looking but I do not freelance, where is the error

local function myTapListener(event)   local positionPersonnagex=event.x   local positionPersonnagey=event.y   transition.from( animationPersonnage, { tag="animationPersonnagedeplacement", time=1000, x=positionPersonnagex, y=positionPersonnagey } )   return positionPersonnagex, positionPersonnagey end background:addEventListener("tap", myTapListener ) local result1, result2 = myTapListener () animationPersonnage.x = result1 animationPersonnage.y = result2

When you call ‘result1, result 2 = myListener ()’ , you have not provided myTapListener with the parameter (event) that it requires. So the results will come back as nil.

What is it you are trying to achieve? When you tap the screen, your object moves to where your finger tapped?

Sorry, i have already an error but I informed that you specified.

yes i would move my object (a sprite animation) to the tap location… i know that there is an example code in corona but i want understand by myself and with the generous help of the forum members :slight_smile:

local function myTapListener(event)   local positionPersonnagex=event.x   local positionPersonnagey=event.y   transition.from( animationPersonnage, { tag="animationPersonnagedeplacement", time=1000, x=positionPersonnagex, y=positionPersonnagey } )   return positionPersonnagex, positionPersonnagey end background:addEventListener("tap", myTapListener ) local result1, result2 = myTapListener(event) animationPersonnage.x = result1 animationPersonnage.y = result2