Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Passing a parameter from an event listener to a function. HELP!
Started by lorendalbert May 06 2013 02:23 PM

- - - - -
11 replies to this topic
function parameter event listener
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

lorendalbert

[GLOBAL: userInfoPane.html]
lorendalbert
  • Observer

  • 3 posts
  • Corona SDK

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

 



[TOPIC: post.html]
#2

bjsorrentino

[GLOBAL: userInfoPane.html]
bjsorrentino
  • Veteran

  • 8,506 posts
  • Corona SDK

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:

 

 
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 )


[TOPIC: post.html]
#3

lorendalbert

[GLOBAL: userInfoPane.html]
lorendalbert
  • Observer

  • 3 posts
  • Corona SDK

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

 

Loren



[TOPIC: post.html]
#4

wilychristian

[GLOBAL: userInfoPane.html]
wilychristian
  • Enthusiast

  • 59 posts
  • Corona SDK

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.



[TOPIC: post.html]
#5

jstrahan

[GLOBAL: userInfoPane.html]
jstrahan
  • Corona Geek

  • 1,925 posts
  • Corona SDK

would need to see some code in order to help



[TOPIC: post.html]
#6

wilychristian

[GLOBAL: userInfoPane.html]
wilychristian
  • Enthusiast

  • 59 posts
  • Corona SDK

Ok. Here goes

world.lua

local world = {}
 
function world:new()
  . . .
end
 
function world:move(event)
    if (event.phase == "ended") then  -- this gave nil already
        . . .
    end
end
 
return world

main.lua

local World = require "world"
local thisWorld = World.new()
local someButton = display.newImageRect( "images/button.png", 30, 30 )
someButton:addEventListener("touch", thisWorld.move)



[TOPIC: post.html]
#7

jstrahan

[GLOBAL: userInfoPane.html]
jstrahan
  • Corona Geek

  • 1,925 posts
  • Corona SDK

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



[TOPIC: post.html]
#8

wilychristian

[GLOBAL: userInfoPane.html]
wilychristian
  • Enthusiast

  • 59 posts
  • Corona SDK

So my options are

put :move inside :new function

or put :move function in main.lua.

 

Ok thanks, i'll try.



[TOPIC: post.html]
#9

jstrahan

[GLOBAL: userInfoPane.html]
jstrahan
  • Corona Geek

  • 1,925 posts
  • Corona SDK

move function is inside new function

calling it from main passing the event to move



[TOPIC: post.html]
#10

wilychristian

[GLOBAL: userInfoPane.html]
wilychristian
  • Enthusiast

  • 59 posts
  • Corona SDK

@jstrahan

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

Because i add your segment below

local function moveWorld(event)  thisWorld:move(event)end
someButton:addEventListener( "touch", moveWorld)
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..



[TOPIC: post.html]
#11

matus

[GLOBAL: userInfoPane.html]
matus
  • Enthusiast

  • 34 posts
  • Corona SDK

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

 

     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))



[TOPIC: post.html]
#12

FearTec

[GLOBAL: userInfoPane.html]
FearTec
  • Contributor

  • 218 posts
  • Corona SDK

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

 

     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))

 

Thanks, you solved my problem here

http://forums.coronalabs.com/topic/56394-hundreds-of-dynamic-images-with-one-tap-listener-how-to-get-tagged-data/?p=291786




[topic_controls]
 
[/topic_controls]