Call a function from another function

Hi
Can I call a function or what the function return from another function ?
For example :

local dragItem=function(event)
t= event.target


From another function :
local rotate = function(event)



object.rotation = dragItem(t).rotation or dragItem(event.target).rotation or something like this.

I’am a beginner please forgive me if it’s stupid.
A link or a any trace or example will be great :slight_smile:
Thank you [import]uid: 13156 topic_id: 5753 reply_id: 305753[/import]

You can call a function from another function with something like this =)

[code]

function startFunction2()
print(“from rotation”)
end

function rotate()
startFunction2()
end

[/code] [import]uid: 9577 topic_id: 5753 reply_id: 19713[/import]

Thanks slimboyfat but what when both depends of events. Drag event move the object and showing a gear (circle) around but rotate rotating the object that you touch before via touching a gear.

[code]
local rotate = function(event)
local xOffset = gear.x
local yOffset = gear.y

local angleBetween = mCeil(mAtan2( (event.y - yOffset), (event.x - xOffset) ) * 180 / mPi) + 90
gear.rotation = angleBetween + 180
dragItem(event.target).rotation = gear.rotation----- I want compare gear rotation to event.target from dragItem function
end
gear:addEventListener(“touch”, rotate)
local dragItem = function(event)

local t = event.target
local phase = event.phase

if “began” == phase and ball.isActive == false then

display.getCurrentStage():setFocus( t )
t.isFocus = true
t.x0 = event.x - t.x
t.y0 = event.y - t.y
event.target.bodyType = “kinetic”
gear.isVisible = true
gear.alpha = 0.3
gear.x = event.target.x
gear.y = event.target.y
levelOneGroup:insert(t)
transition.to(box, {time = 400, alpha =0 , onComplete = function() box.isVisible = false; end })
elseif t.isFocus then
if “moved” == phase and ball.isActive == false then
t.x = event.x - t.x0
t.y = event.y - t.y0
gear.x = event.target.x
gear.y = event.target.y

elseif “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus( nil)
t.isFocus = false

if (not event.target.isPlatform ) then
t.isFocus = false
event.target.bodyType = “static”
physics.addBody(t, “static” ,{density = 2.0, friction = 1.3, bounce = 0.2} )
end
end
end
return true

end
[/code] [import]uid: 13156 topic_id: 5753 reply_id: 19716[/import]

I’m guessing that you want an object to start rotating and start being draged when you touch it?

this should be outside the function, it’s not activated inside the function it’s going to call unless you call the function from elsewhere.

gear:addEventListener("touch", dragItem)   
  
local rotate = function(event)  
 local xOffset = gear.x  
 local yOffset = gear.y  
 local angleBetween = mCeil(mAtan2( (event.y - yOffset), (event.x - xOffset) ) \* 180 / math.pi) + 90  
 gear.rotation = angleBetween + 180  
end  
  
   
   
 local dragItem = function(event)  
 if(event.phase=="began") then  
 Runtime:addEventListener("enterFrame", rotate)   
 elseif (event.phase=="ended" or event.phase="cancelled") then  
 Runtime:removeEventListener("enterFrame", rotate)   
 elseif(event.phase=="moving") then  
 event.target.x = event.x  
 event.target.y = event.y  
 end   
 end  
  
gear:addEventListener("touch", dragItem)   
  

Can’t test the code now but something like this should work, I’m a novice to so sorry if it doesn’t work :stuck_out_tongue: [import]uid: 9577 topic_id: 5753 reply_id: 19724[/import]

not exactly … when you touching the gear the object is rotating exactly the same but it depends of touching the gear…

  1. You touch the object , the gear show up. You can drag an object around the screen.The object is in the gear.
  2. When you touch the gear you can rotate the object.

Thanks for trying help I appreciate this :slight_smile: [import]uid: 13156 topic_id: 5753 reply_id: 19728[/import]