How do i write this function correctly?

Hello again, now how do i make an event listener work? i have the drag body function here…

 function dragBody( self, event, params ) local body = event.target local phase = event.phase local stage = display.getCurrentStage() if "began" == phase then stage:setFocus( body, event.id ) body.isFocus = true if params and params.center then body.tempJoint = physics.newJoint( "touch", body, body.x, body.y ) else body.tempJoint = physics.newJoint( "touch", body, event.x, event.y ) end if params then local maxForce, frequency, dampingRatio if params.maxForce then body.tempJoint.maxForce = params.maxForce end if params.frequency then body.tempJoint.frequency = params.frequency end if params.dampingRatio then body.tempJoint.dampingRatio = params.dampingRatio end end elseif body.isFocus then if "moved" == phase then body.tempJoint:setTarget( event.x, event.y ) elseif "ended" == phase or "cancelled" == phase then stage:setFocus( body, nil ) body.isFocus = false body.tempJoint:removeSelf() end end return true end 

i added the table  to the drag body as you can see above… now in my spawnCircle function when i try adding the event listener it gives an error… here the listener

ball[i]:addEventListener( "touch", dragBody )

And yes im listening to you on that i should remove the table and all but i have to fix this first and then ill make a video on what im trying to do and accomplish so you understand better and could maybe help me out further 

  1. Fix this:

    local function dragBody( self, event, params )  

  2. Be sure you’re creating the ‘balls’ after you’ve defined dragBody

  3. Make this change:

    ball[i].touch = dragBody ball[i]:addEventListener( “touch” )

Note

There is nothing wrong with storing references to objects in tables.  

I don’t mean stop using tables.  I was saying, don’t use them like magic scope solvers.  

Instead understand scope and visibility, and you’ll be in a much better position to code.  Just trying to take the mystery out of stuff is all.

Hey, Another question… so i have this…

 function movePlayBtn1() transition.to( playBtn,{time=1000, x = 60, y = 350, onComplete=movePlayBtn} ) end function movePlayBtn() transition.to( playBtn,{time=1000, x = 60, y = 355, onComplete=movePlayBtn1} ) end movePlayBtn()

how do i remove the 

movePlayBtn()

like how do i stop it when the scene changes? Thanks!

You don’t actually remove functions.  You can re-define them (override original), or nill them, but you’re thinking about it sideways.

You need to make it a local function instead of a global one.

This is why I told you global functions are bad, unless they must be global for a specific reason.

Wait, I missed it.  You’ve got two functions calling each other.
 
Try something like this:

-- At top of scene file local isRunning = false -- Forward declare your functions (Stop it with the globals) local movePlayBtn1 local movePlayBtn

Then, in the will or did phase of scene ‘hide()’ method: 

isRunning = false

Then, modify your functions:

movePlayBtn1 = function() if( not isRunning ) then return end transition.to( playBtn,{time=1000, x = 60, y = 350, onComplete=movePlayBtn} ) end movePlayBtn = function() if( not isRunning ) then return end transition.to( playBtn,{time=1000, x = 60, y = 355, onComplete=movePlayBtn1} ) end

Then, when you first start the ‘cycle’ do this:

isRunning = true movePlayBtn()