What does return really do?

I’ve been confused by what returning and object does or return true in functions and transition.to(s), can anyone give me an idea of what that does?  I’ve had a hard time coding without really knowing what this does and how its used.  

It would be real helpful if anyone helps me here :slight_smile:

thank you for reading!

This is really core to all programming, whichever language you’re using, so you might want to start off here:

http://www.lua.org/pil/contents.html

That site is simple and present clean examples.

Here’s the functions section: http://www.lua.org/pil/2.6.html

That may not help, if you’re new, so try to think of functions as “a series of instructions” which can be referred to by your code in just the same way that variables are referred to. The difference is that a function is not just a store for a value, it actually performs some logic, math, etc and gives you a value which is the result of that series of instructions.

And that’s what the return value is for. The value (or values) allows your function (series of logical instructions) to pass the final value of that logic back to your code.

You do not need to return a value from every function. For example, you might just have a function which prints something to the console display, so it does not need to return a value.

You can return more than one value, so if your function takes a table of integers and finds the highest and lowest numbers in the table, it can return both of those. I’ve written that below:

-- returns the highest and lowest values in the table passed in parameters local function getHighestAndLowest( numbers ) local lowest, highest = 0, 0 -- define the return values for i=1, #numbers do if (numbers[i] \< lowest) then lowest = numbers[i] end if (numbers[i] \> highest) then highest = numbers[i] end end return lowest, highest end -- call the function local lowest, highest = getHighestAndLowest( { 3,8,2,5,9 } ) print("Lowest: ",lowest) print("Highest: ",highest)

So, you can see that the function returns two values which you can then use later on. The reason the instructions in that function are in a function at all is because you might want to execute that code many times, so there is no point copying the instructions all over the place - this way you can just have the logic in one place and call it when you need it.

Now, returning true, false or any other value is just the same. There is no difference at all. They are values and return passes back values, that’s all.

Many people have trouble is knowing when to return true or false from touch listener functions, but that is a case-by-case specific situation. (For touch and tap functions you should return true if you have handled the user’s touch event, false if you haven’t.)

Let me know if you need a specific situation explaining - post some code and I’ll take a look.

im trying to make a cannon rotate through touch events

local cannon = display.newImage("cannon.png") local number = 5.3 cannon.rotation = rotate1 cannon.xScale = 0.2 cannon.yScale = 0.2 cannon.x = 250 cannon.y = 425 local rotate = function (event) if event.y \< 420 and event.y \> 50 then rotate1 = (event.y / number) print (rotate1) end end Runtime:addEventListener("touch", rotate) &nbsp;

the console is able to print the rotation angle, but it doesn’t change the angle of the cannon.

thanks for the explanation btw~ :slight_smile:

I can’t see where you define ‘rotate1’. You should define it somewhere just like you’ve defined ‘number’.

Also, the ‘rotation’ value of ‘cannon’ is ‘cannon.rotation’ so you need to assign a value to that if you want to turn the cannon. You are only assigning a value to ‘rotate1’.

Try this:

local cannon = display.newImage("cannon.png") local number = 5.3 cannon.rotation = rotate1 cannon.xScale = 0.2 cannon.yScale = 0.2 cannon.x = 250 cannon.y = 425 local rotate = function (event) if event.y \< 420 and event.y \> 50 then rotate1 = (event.y / number) cannon.rotation = rotate1 -- I ONLY ADDED THIS LINE print (rotate1) end end Runtime:addEventListener("touch", rotate) 

I have not tested this code, so let me know if it does what you want.

OMG :DDD

thank you~ 

:)  :)  :)  :)  :)  :slight_smile:

Don’t forget to mark the post which solved your problem.

now i have another problem

local cannon = display.newImage("cannon.png") local number = 1 local knife = display.newImage("knife.png") local knifex = 250 local knifey = 425 local knifexs = 0.1 local knifeys = 0.1 local balloonx = 50 local balloony = 300 local rotate2 = 0 knife.x = knifex knife.y = knifey knife.xScale = knifexs knife.yScale = knifeys knife.rotation = 250 --knife:toBack() knife:toFront() cannon.xScale = 0.2 cannon.yScale = 0.2 cannon.x = 250 cannon.y = 425 local rotate = function (event) if event.y \< 420 and event.y \> 340 then rotate1 = (event.y / number) cannon.rotation = rotate1 knife.rotation = rotate1 + 250 --print(rotate1) print(event.x,event.y) end end Runtime:addEventListener("touch", rotate) local rotate1 = cannon.rotation local physics = require "physics" physics.start() local balloon = display.newImage("balloon.png") balloon.x = balloonx balloon.y = balloony balloon.xScale = 0.2 balloon.yScale = 0.2 physics.addBody(balloon, "static") ----------WHERE I HAVE PROBLEMS!-------------- local throw = function(event) rotate2 = (event.y / number) angle = rotate2 -341 angle1 = angle / 180 radian = angle1 / math.pi rad = math.sin(radian) shootpos = rad \* 100 print (rad) if event.phase == "press" then transition.to(knife,{time = 1000,x=0, y=shootpos, rotation = 720,}) end end local ui = require "ui" local firebtn = ui.newButton{ defaultSrc = "button.png", defaultX = 100 , defaultY = 100 , overSrc = "button.png", overX = 100, overY = 100, onEvent = throw, } firebtn.x = 20 firebtn.y = 450

i’m creating an equation that calculates the coordinates in which the object will shoot towards.

whenever i press the firebtn, i get the error

What error?

You might like to take a look at the rotation functions I wrote in my mathlib here:

http://springboardpillow.blogspot.co.uk/2012/04/sample-code.html

Specifically:

-- returns the distance between points a and b math.lengthOf = function( a, b ) local width, height = b.x-a.x, b.y-a.y return (width\*width + height\*height)^0.5 -- math.sqrt(width\*width + height\*height) -- nothing wrong with math.sqrt, but I believe the ^.5 is faster end --[[DEPRECATED -- converts degree value to radian value, useful for angle calculations function convertDegreesToRadians( degrees ) -- return (math.pi \* degrees) / 180 return math.rad(degrees) end]]-- --[[DEPRECATED function convertRadiansToDegrees( radians ) return math.deg(radians) end]]-- -- rotates point around the centre by degrees -- rounds the returned coordinates using math.round() if round == true -- returns new coordinates object local function rotateAboutPoint( point, degrees, center ) local pt = { x=point.x - centre.x, y=point.y - centre.y } pt = math.rotateTo( pt, degrees ) pt.x, pt.y = pt.x + centre.x, pt.y + centre.y return pt end -- rotates a point around the (0,0) point by degrees -- returns new point object -- center: optional math.rotateTo = function( point, degrees, center ) if (center ~= nil) then return rotateAboutPoint( point, degrees, center ) else local x, y = point.x, point.y local theta = math.rad( degrees ) local pt = { x = x \* math.cos(theta) - y \* math.sin(theta), y = x \* math.sin(theta) + y \* math.cos(theta) } return pt end end local PI = (4\*math.atan(1)) local quickPI = 180 / PI -- returns the degrees between two points -- note: 0 degrees is 'east' local function angleBetweenPoints( a, b ) local x, y = b.x - a.x, b.y - a.y return math.angleOf( { x=x, y=y } ) end -- returns the degrees between (0,0) and pt -- note: 0 degrees is 'east' -- center: optional math.angleOf = function( center, pt ) if (pt == nil) then pt = center local angle = math.atan2( pt.y, pt.x ) \* quickPI -- 180 / PI -- math.pi if angle \< 0 then angle = 360 + angle end return angle else return angleBetweenPoints( center, pt ) end end -- Brent Sorrentino -- Returns the angle between the objects local function angleBetween ( srcObj, dstObj ) local xDist = dstObj.x - srcObj.x local yDist = dstObj.y - srcObj.y local angleBetween = math.deg( math.atan( yDist / xDist ) ) if ( srcObj.x \< dstObj.x ) then angleBetween = angleBetween + 90 else angleBetween = angleBetween - 90 end return angleBetween end

attempt to perform arithmetic  on field ‘y’ (a nil value)

still error

i dont think my equation is the problem, i imported ur API(real useful!).

i dont get the problem,  my shootpos is a nil value, but it is stated in the function…

local cannon = display.newImage("cannon.png") local number = 1 local knife = display.newImage("knife.png") local knifex = 250 local knifey = 425 local knifexs = 0.1 local knifeys = 0.1 local balloonx = 50 local balloony = 300 local radnum = 100 local mathlib = require ("mathlib") knife.x = knifex knife.y = knifey knife.xScale = knifexs knife.yScale = knifeys knife.rotation = 250 --knife:toBack() knife:toFront() cannon.xScale = 0.2 cannon.yScale = 0.2 cannon.x = 250 cannon.y = 425 local rotate = function (event) if event.y \< 420 and event.y \> 340 then rotate1 = (event.y / number) cannon.rotation = rotate1 knife.rotation = rotate1 + 250 --print(rotate1) print(event.x,event.y) end end Runtime:addEventListener("touch", rotate) local rotate1 = cannon.rotation local physics = require "physics" physics.start() local balloon = display.newImage("balloon.png") balloon.x = balloonx balloon.y = balloony balloon.xScale = 0.2 balloon.yScale = 0.2 physics.addBody(balloon, "static") --local rotate2 = (event.y / number) --local angle = rotate2 -341 --local angle1 = angle / 180 --local radian = angle1 / math.pi --local rad = math.sin(radian) --local shootpos = rad \* 100 --print (rad) local throw = function(event) radian = math.rad(rotate2) rad = math.sin(radian) shoot = rad \* radnum print (rad) if event.phase == "press" then transition.to(knife,{time = 1000,x=-25,y = shoot, rotation = 720}) end end Runtime:addEventListener("touch", throw) local remove = function() knife:removeSelf() knife=nil if knife == nil then print ("knife removed!") end end local ui = require "ui" local firebtn = ui.newButton{ defaultSrc = "button.png", defaultX = 100 , defaultY = 100 , overSrc = "button.png", overX = 100, overY = 100, onEvent = throw, } firebtn.x = 20 firebtn.y = 450

my new code

You have not defined shootpos.

You are not making this very easy. You need to post complete error messages, including line numbers and when you do, you need to post the code which goes with it - not wait a while and post code which might be something to do with the problem.

I simply don’t know what to look at. The last code I can see with ‘shootpos’ has the declaration commented out.

Ok, ill make sure i post full error messages…

this is where i have problems,  I have fixed the code a little bit to a point which i get no errors, but the knife is not shooting at the angle my cannon is pointing at…

local throw = function(event) if event.phase == "press" then radian = math.rad(cannon.rotation) rad = math.sin(radian) shoot = rad \* radnum print (rad) transition.to(knife,{time = 1000,x=-25,y = shoot, rotation = 720}) end end Runtime:addEventListener("touch", throw)

I don’t think you understand ‘transition.to’.  Putting ‘x=-25’ will always ensure that, at the end of the 1000 milliseconds, knife.x == -25. So, you have not set the destination properly.

You need to calculate the angle to fire the knife in. Then you need to get an x,y value for that angle. My functions can help you with that. The you need to multiply the x,y values by some other value to make the knife go further.

Do you really want to do this in raw form or would you prefer to do this with the physics engine? Both cases have challenges, but if you like I can write the code for you - you just need to choose a particular solution.

Btw, really think you should take a look at my blog page: http://springboardpillow.blogspot.co.uk/2012/04/sample-code.html

Specifically, this entry: http://developer.coronalabs.com/code/predictive-aiming-tower-defense

I’m afraid that’s posted on the old code exchange, so it needs updating, but the logic is sound (probably the code is fine too.)

By the way, you should know that im actually 13 years old LOL… I havent learned many calculations physics and stuff like that, i had to ask my dad for that. thanks for the link!

yeah i probably need u to write a piece of code for me…  PLEASE