What does return really do?

Ok, so let’s say you have a gun turret which shoots and you have touches (let’s use ‘tap’) to aim at. When you tap, the gun turret fires in the direction you touched the screen.

Do you want the gun turret to turn to that direction before firing?

Do you want the ‘bullets’ to point in that direction?

Do you want to use the physics engine or not?

Let me know and I’ll write something for you…

I’d say yes for all 3 questions…

thank you~

FYI I’m working on this but I have to go to work now. Give me a couple of hours…

ok…

Ok, the turret angle is wrong and I’m working on that, but this should get you going:

https://dl.dropboxusercontent.com/u/10254959/Help/TargetPhys/TargetPhys.zip

Nice coding, where did you learn all this?  i need to learn more…

Persistence. In my experience, you simply need to love coding and do it on your own. Though I went to university to study programming, I spent years before doing it at home and learnt everything useful in my spare time. Learn from others, read as much as you can and keep asking questions in places like this forum and http://stackoverflow.com/questions/tagged/corona

Ok, I’ve done some research and come up with a good rotation routine. The version I provided earlier simply rotated from one angle to another - this isn’t good enough because it requires the turret to go through far larger revolutions than it should. It should not go through more than 180 degrees.

https://dl.dropboxusercontent.com/u/10254959/Help/TargetPhys/TargetPhys.zip

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…