Possible to Pass Parameters in Event Function?

I am trying to pass parameters in an event function.  I have a native input function and would like to be able to use it to input to different tables if possible.  

I know this is not correct, but conceptually I would like to do something like this:

local function textListener( event, table )     if event.phase == "began" then          event.target.text = ''     elseif event.phase == "submitted" then         text1 = event.target.text         native.setKeyboardFocus(nil)         table.insert(table, text1)         print(table.concat(table, ", "))     end end local function callAhead()     textListener(table1) end  defaultField = native.newTextField( 10, 50, 180, 23 ) defaultField:addEventListener( "userInput", callAhead)  

Am I able to do something like this or am I going to have to create a new textListener function every time I want to input something into a different table?

local function textListener( event, table ) if event.phase == "began" then event.target.text = '' elseif event.phase == "submitted" then text1 = event.target.text native.setKeyboardFocus(nil) table.insert(table, text1) print(table.concat(table, ", ")) end end local function callAhead(event) textListener(event, table1) end defaultField = native.newTextField( 10, 50, 180, 23 ) defaultField:addEventListener( "userInput", callAhead)

Not sure if I got it correctly, but basically like this callAhead will get the event, that you then pass to your textListener function, with everything else you’d like to pass as well.

Not exactly sure what you want to do, but here are something to consider if you had not realized:

[lua]local function textListener( event )

    if event.phase == “began” then 

        local target = event.target – event.target is the object the touch listener is on

        local myValue = target.someValueIWantToUseInTouchListener

    end

end

defaultField = native.newTextField( 10, 50, 180, 23 )

defaultField.someValueIWantToUseInTouchListener = “foo”

defaultField:addEventListener( “userInput”, textListener)

[/lua]

Hach, I guess I was going for something like this that you need to use to pass a parameter in a transition onComplete function: 

local square = display.newRect(50,50,50,50) stretch = function(obj)     obj.xScale = 2 end       callAhead = function()     stretch(square)  end  transition.to(square, {x = 200, y = 200, time = 2000, onComplete = callAhead})  

Jon, that I haven’t tried anything like that, will check it out and let you know.  

Here is another way of doing the same thing just little bit more contained

[lua]

local square = display.newRect(50,50,50,50)

local stretch = function(obj)

    obj.xScale = 2

end 

    

transition.to(square, {x = 200, y = 200, time = 1000, onComplete = function() stretch(square) end})

[/lua]

but in that example the square is still in scope inside stretch function so you can also just do

[lua]

local square = display.newRect(50,50,50,50)

local stretch = function()

    square.xScale = 2

end 

    

transition.to(square, {x = 200, y = 200, time = 1000, onComplete = stretch})

[/lua]

Or you could use an anonymous funciton:

[lua]

defaultField:addEventListener( “userInput”,

function(event, table1)

textListener(event, table1)

end)

[/lua]

I’m not sure where table1 is however so you’ll need to check on your scoping…

Nailed it.  You’re the man, thanks.  

local function textListener( event, table ) if event.phase == "began" then event.target.text = '' elseif event.phase == "submitted" then text1 = event.target.text native.setKeyboardFocus(nil) table.insert(table, text1) print(table.concat(table, ", ")) end end local function callAhead(event) textListener(event, table1) end defaultField = native.newTextField( 10, 50, 180, 23 ) defaultField:addEventListener( "userInput", callAhead)

Not sure if I got it correctly, but basically like this callAhead will get the event, that you then pass to your textListener function, with everything else you’d like to pass as well.

Not exactly sure what you want to do, but here are something to consider if you had not realized:

[lua]local function textListener( event )

    if event.phase == “began” then 

        local target = event.target – event.target is the object the touch listener is on

        local myValue = target.someValueIWantToUseInTouchListener

    end

end

defaultField = native.newTextField( 10, 50, 180, 23 )

defaultField.someValueIWantToUseInTouchListener = “foo”

defaultField:addEventListener( “userInput”, textListener)

[/lua]

Hach, I guess I was going for something like this that you need to use to pass a parameter in a transition onComplete function: 

local square = display.newRect(50,50,50,50) stretch = function(obj)     obj.xScale = 2 end       callAhead = function()     stretch(square)  end  transition.to(square, {x = 200, y = 200, time = 2000, onComplete = callAhead})  

Jon, that I haven’t tried anything like that, will check it out and let you know.  

Here is another way of doing the same thing just little bit more contained

[lua]

local square = display.newRect(50,50,50,50)

local stretch = function(obj)

    obj.xScale = 2

end 

    

transition.to(square, {x = 200, y = 200, time = 1000, onComplete = function() stretch(square) end})

[/lua]

but in that example the square is still in scope inside stretch function so you can also just do

[lua]

local square = display.newRect(50,50,50,50)

local stretch = function()

    square.xScale = 2

end 

    

transition.to(square, {x = 200, y = 200, time = 1000, onComplete = stretch})

[/lua]

Or you could use an anonymous funciton:

[lua]

defaultField:addEventListener( “userInput”,

function(event, table1)

textListener(event, table1)

end)

[/lua]

I’m not sure where table1 is however so you’ll need to check on your scoping…

Nailed it.  You’re the man, thanks.