Modifying function closure as argument

Thanks in advance for any help.  Still figuring out how Lua works…

I made a function that uses transition.to.  That function can accept a function closure as an argument, which it then passes to the transition.to’s onComplete.  Is there a way to add a line of code to that function enclosure before it is passed to transition.to? 

Stripped-down example:

[lua]local myVariable = false
local myObject = display.newRect(50,50,50,50)

   function testFunction( object, args )
   myVariable = true

   transition.to(object, {time= 1000, x = args.x, onComplete = args.complete})
   --I need to add this to onComplete:

   --myVariable = false

end

testFunction(myObject, {x = 200, complete = function() print(“transition completed”) end})[/lua]

I could just add it to the closure, but I’d end up having to add it a few hundred times…

Any info appreciated.

I answered, but then realized the question didn’t make sense to me.

Based on what you said, you need to define the ‘onComplete function’ just prior to the transition call.

Unfortunately, the example you gave didn’t make sense in that context because of scoping issues for ‘myVariable’

Here is the best answer I can give:

local myObject = display.newRect(50,50,50,50) local function testFunction( object, args ) local myVariable = false local function onComplete() print("transition completed") end transition.to(object, {time= 1000, x = args.x, onComplete = onComplete}) end testFunction(myObject, { x = 200 } )

You may not find that answer helpful, but the problem is I don’t really know what you’re trying to do here.

Can you give us a more ‘concrete’ example of the problem you’re trying to solve.  

  • What is your motivation for modifying a variable?
  • Is that variable shared or meant to be visible only in the context of the object be transitioned?
  • What does the variable do?

Another possible answer is this:

local function onComplete( self ) print("transition completed") self.myVariable = true end local myObject = display.newRect(50,50,50,50) myObject.myVariable = true myObject.onComplete = onComplete local function testFunction( object, args ) object.myVariable = false transition.to(object, {time= 1000, x = args.x, onComplete = object }) end testFunction(myObject, { x = 200 } )

I answered, but then realized the question didn’t make sense to me.

Based on what you said, you need to define the ‘onComplete function’ just prior to the transition call.

Unfortunately, the example you gave didn’t make sense in that context because of scoping issues for ‘myVariable’

Here is the best answer I can give:

local myObject = display.newRect(50,50,50,50) local function testFunction( object, args ) local myVariable = false local function onComplete() print("transition completed") end transition.to(object, {time= 1000, x = args.x, onComplete = onComplete}) end testFunction(myObject, { x = 200 } )

You may not find that answer helpful, but the problem is I don’t really know what you’re trying to do here.

Can you give us a more ‘concrete’ example of the problem you’re trying to solve.  

  • What is your motivation for modifying a variable?
  • Is that variable shared or meant to be visible only in the context of the object be transitioned?
  • What does the variable do?

Another possible answer is this:

local function onComplete( self ) print("transition completed") self.myVariable = true end local myObject = display.newRect(50,50,50,50) myObject.myVariable = true myObject.onComplete = onComplete local function testFunction( object, args ) object.myVariable = false transition.to(object, {time= 1000, x = args.x, onComplete = object }) end testFunction(myObject, { x = 200 } )