Passing More Information into a Function with an Event Argument

Hello!

I have a question about listener functions and how they relate with their child functions.  For example, if you have…

[lua]local function printK ( event )
local k = 0 ;
print ( k ) ;
end

myImage:addEventListener ( “touch” , k ) ;[/lua]

Would there be a way to include another variable in the function printK?  Like if I wanted to update the variable k to some other value, could I pass it into that function?  And if so, how?

Thanks!

Except for adding event listeners (i.e. onComplete’s for transition.to’s, the function for timers, etc.) you can use an anonymous function sometimes called a Closure to do so:

timer.performWithDelay(1000, function()  printK( event, k ); end)

But because you may want to remove the event listener later, you have to pass the same function address to removeEventListener that you do to addEventListener so this concept of anonymous function won’t work.

Instead you have to pre-define a wrapper function:

local function SendK( event )

     printK( event, k )

end

myImage:addEventListener(  “touch”, SendK )

But ask yourself when someone touches an object, where is the value “k” coming from?  A touch normally doesn’t generate this sort of thing.  The usual way of doing this is to make sure myImage knows about k by adding it as an attribute:

myImage.k = k

Then in your touch handler, event.target.k will have the value of k.

Rob

Thanks!  That was really helpful!

But now I have another question.  What happens if I’m working on a function that moves a sprite to a location on a background?  The event listener is declared on the background, but the sprite has to move there.  There’s no value that the background can give to the sprite, right?

For example:

[lua]function getCoords ( event )
     return event.x , event.y ;
end

function stopHim ()
     Currin:setSequence ( “currinStand” ) ;
     Currin:play() ;
end

function updateCoords ( event )
     local newx , newy = getCoords ( event )
     if ( Currin.x < newx ) then
          Currin:setSequence ( “currinWalk” ) ;
          Currin:play() ;
          transition.to ( Currin , { time = 1500 , x = newx , y = newy , onComplete = stopHim } ) ;
     end
end

background:addEventListener ( “touch” , updateCoords ) ;[/lua]

Thanks!

The touch event has the .x and .y that the touch occurred at.  You an then move your sprite to that location.

Rob

Thanks, Rob!  One more question:

How would you recommend I move the sprite without declaring global variables?  I’m using the composer library and having trouble understanding what is and what is not local (i.e. is what I declare local in createScene still considered local in subsequent functions?).

Or would I set-up a function where the return values are the x and y coordinates of my event (touching the background)?  But then how would I pass that into a function where the sprite could use them?

Thanks!

http://coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/

is one of my preferred ways.

Rob

Except for adding event listeners (i.e. onComplete’s for transition.to’s, the function for timers, etc.) you can use an anonymous function sometimes called a Closure to do so:

timer.performWithDelay(1000, function()  printK( event, k ); end)

But because you may want to remove the event listener later, you have to pass the same function address to removeEventListener that you do to addEventListener so this concept of anonymous function won’t work.

Instead you have to pre-define a wrapper function:

local function SendK( event )

     printK( event, k )

end

myImage:addEventListener(  “touch”, SendK )

But ask yourself when someone touches an object, where is the value “k” coming from?  A touch normally doesn’t generate this sort of thing.  The usual way of doing this is to make sure myImage knows about k by adding it as an attribute:

myImage.k = k

Then in your touch handler, event.target.k will have the value of k.

Rob

Thanks!  That was really helpful!

But now I have another question.  What happens if I’m working on a function that moves a sprite to a location on a background?  The event listener is declared on the background, but the sprite has to move there.  There’s no value that the background can give to the sprite, right?

For example:

[lua]function getCoords ( event )
     return event.x , event.y ;
end

function stopHim ()
     Currin:setSequence ( “currinStand” ) ;
     Currin:play() ;
end

function updateCoords ( event )
     local newx , newy = getCoords ( event )
     if ( Currin.x < newx ) then
          Currin:setSequence ( “currinWalk” ) ;
          Currin:play() ;
          transition.to ( Currin , { time = 1500 , x = newx , y = newy , onComplete = stopHim } ) ;
     end
end

background:addEventListener ( “touch” , updateCoords ) ;[/lua]

Thanks!

The touch event has the .x and .y that the touch occurred at.  You an then move your sprite to that location.

Rob

Thanks, Rob!  One more question:

How would you recommend I move the sprite without declaring global variables?  I’m using the composer library and having trouble understanding what is and what is not local (i.e. is what I declare local in createScene still considered local in subsequent functions?).

Or would I set-up a function where the return values are the x and y coordinates of my event (touching the background)?  But then how would I pass that into a function where the sprite could use them?

Thanks!

http://coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/

is one of my preferred ways.

Rob