Passing a parameter from an event listener to a function. HELP!

Newb needs help.

I am having trouble passing a parameter from an event listener to a function.  When I tap the button, I want it to pass the value of the button to the numberTapped function, so that I can display it on the screen, but it doesn’t work.  So now I am testing by printing the value of number to the terminal, but it only prints “nil”.

local function numberTapped(event, number) print(number) end oneButton = display.newImage( "/images/one.png" ) oneButton.x = display.contentWidth / 2 - 110 oneButton.y = display.contentHeight - 430 oneButton:addEventListener ( "tap", numberTapped, 1 )

Any help is greatly appreciated.

Dalbelo

Hi Dalbelo,

I think what you’re looking for is “event.target”. This represents the object (ID) that you tapped on, in this case the button.

You will, however, need to add a custom “number” property or similar to the button, like this:

[lua]

local function numberTapped( event )

   print(event.target.num)

end

local oneButton = display.newImage( “/images/one.png” )

oneButton.x = display.contentWidth / 2 - 110

oneButton.y = display.contentHeight - 430

oneButton.num = 1  --add this property!

oneButton:addEventListener ( “tap”, numberTapped )

[/lua]

Sweet, that did the trick.  Many thanks for the assistance.

Loren

Hi Dalbelo,

I think what you’re looking for is “event.target”. This represents the object (ID) that you tapped on, in this case the button.

You will, however, need to add a custom “number” property or similar to the button, like this:

[lua]

local function numberTapped( event )

   print(event.target.num)

end

local oneButton = display.newImage( “/images/one.png” )

oneButton.x = display.contentWidth / 2 - 110

oneButton.y = display.contentHeight - 430

oneButton.num = 1  --add this property!

oneButton:addEventListener ( “tap”, numberTapped )

[/lua]

Sweet, that did the trick.  Many thanks for the assistance.

Loren

I have trouble with ‘event’ parameter passed as nil.

I create a function in a lua file. Then call it as touch listener from main.lua.

There will be a lot of variables to process in the function, so its quite hard to use Brent’s solution above.

And in addition, i need to use the event.phase inside the function.

Why does it nil? How can i work this out?

Thanks before.

would need to see some code in order to help

Ok. Here goes

world.lua

[lua]

local world = {}

function world:new()

  . . .

end

function world:move(event)

    if (event.phase == “ended”) then  – this gave nil already

        . . .

    end

end

return world

[/lua]

main.lua

[lua]

local World = require “world”

local thisWorld = World.new()

local someButton = display.newImageRect( “images/button.png”, 30, 30 )

someButton:addEventListener(“touch”, thisWorld.move)

[/lua]

thisWorld has no reference to move function

basically it looks like the world module is setup wrong but without seeing entire code for it cant say how to fix other then try something like this

local world = {}   world.new = function( arg )     function arg:move(event)       ...     end     return arg   end return world

local world = require'world' local worldImage = display.newImageRect( ... local  thisWorld = world.new(worldImage) local someButton = ....   local function moveWorld(event)   ...   thisWorld:move(event)   ... end   someButton:addEventListener( "touch", moveWorld)

or something like that

So my options are

put :move inside :new function

or put :move function in main.lua.

Ok thanks, i’ll try.

move function is inside new function

calling it from main passing the event to move

@jstrahan

I think the structure of my world module is still acceptable by corona.

Because i add your segment below

[lua]

local function moveWorld(event)
  thisWorld:move(event)
end

someButton:addEventListener( “touch”, moveWorld)

[/lua] to main.lua. I left the structure as it is.

And then ‘event’ parameter passed fine already.

Im still not get the idea perfectly, but from now on i’ll just send external touch listener like this.

Thanks again…

I have trouble with ‘event’ parameter passed as nil.

I create a function in a lua file. Then call it as touch listener from main.lua.

There will be a lot of variables to process in the function, so its quite hard to use Brent’s solution above.

And in addition, i need to use the event.phase inside the function.

Why does it nil? How can i work this out?

Thanks before.

would need to see some code in order to help

Ok. Here goes

world.lua

[lua]

local world = {}

function world:new()

  . . .

end

function world:move(event)

    if (event.phase == “ended”) then  – this gave nil already

        . . .

    end

end

return world

[/lua]

main.lua

[lua]

local World = require “world”

local thisWorld = World.new()

local someButton = display.newImageRect( “images/button.png”, 30, 30 )

someButton:addEventListener(“touch”, thisWorld.move)

[/lua]

thisWorld has no reference to move function

basically it looks like the world module is setup wrong but without seeing entire code for it cant say how to fix other then try something like this

local world = {}   world.new = function( arg )     function arg:move(event)       ...     end     return arg   end return world

local world = require'world' local worldImage = display.newImageRect( ... local  thisWorld = world.new(worldImage) local someButton = ....   local function moveWorld(event)   ...   thisWorld:move(event)   ... end   someButton:addEventListener( "touch", moveWorld)

or something like that

So my options are

put :move inside :new function

or put :move function in main.lua.

Ok thanks, i’ll try.

move function is inside new function

calling it from main passing the event to move

@jstrahan

I think the structure of my world module is still acceptable by corona.

Because i add your segment below

[lua]

local function moveWorld(event)
  thisWorld:move(event)
end

someButton:addEventListener( “touch”, moveWorld)

[/lua] to main.lua. I left the structure as it is.

And then ‘event’ parameter passed fine already.

Im still not get the idea perfectly, but from now on i’ll just send external touch listener like this.

Thanks again…

I needed the same and I came up with code like this:

[lua]

     local handleTouch = function(event,fragmentId)

         if(event.phase == “ended”) then

            print(fragmentId);

         end

     end

     –

     local handleTouchGenerator = function(fragmentId)

         return function(event)

             handleTouch(event,fragmentId)

         end

     end

     – add evetns

     piece.fragments[1]:addEventListener(“touch”,handleTouchGenerator(1))

     piece.fragments[2]:addEventListener(“touch”,handleTouchGenerator(2))

[/lua]