What does return really do?

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 

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