How to pass additional arguments / parameters to an event listener

Is it possible to pass other parameters, in addition to the event, to an event listener?
May I please have an example?

Thanks!

you can if you do for example

1
timer.performWithDelay(200, listenerFunction);

I appreciate your reply. I am still confused though.

Here is my specific example where I need to apply this.

I'd like to pass an argument to the buttonHandlerScreen1 event handler in the onPress line of the ui.newButton code block.

Thanks!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
local buttonHandlerScreen1 = function ( event )
        if not screen1.isVisible then
                screen1.isVisible = true
                screen2.isVisible = false
                screen3.isVisible = false
                transition.from(screen1, {time=500, alpha=0})
        end
end
 
local buttonImageScreen1 = ui.newButton{
        default = "up.png",
        over = "over.png",
        onPress = buttonHandlerScreen1,
        id = "buttonImageScreen1"
}

You can't pass any parameters but you could call a specific listener that could call another function with a parameter. Not sure if that helps.

Without specifically passing a parameter, in the case where multiple objects use the same listener, is there any way to check which object triggered the listener?

I thought "target" could take care of that, but for target always returns a different value, which pretty much rules out the idea that "target" is the object which triggered the listener.

How do you create the eventlistener (code)? I use a lot of transition.to commands with an OnComplete listener and there target is the object the event is dealing with.

You can always add your own parameter to the button object and read it in your Listener routine.

1
buttonImageScreen1.parm1 = "my own value"

Oh, didn't know you could add parameters to display objects. That's brilliant. thanks!

in answer to the subject, just as another self contained example for reference

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
local function spawnBall(event)
 
 print("spawnBall: ".. event.source.something)
 
end
 
local function newBall(someVal)
                
    local randomPosition = 100 + math.random(200)
        print("newBall: "..randomPosition)
                
    local t2  =  timer.performWithDelay(2000, spawnBall, 1)
 
   -- note t2 will be equal to event.source in the call back
    t2.something = someVal..", "..randomPosition
    
end
 
newBall("blah")
newBall("whatever")

It seems like source is nil! Is there any other way to access additional parameters I added to the displayObject? So that I can tell which object calls the listener.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
local function listener(event) 
        print ("event.source.caller",event.source.caller) -- *********always get nil*********
        if( phase == "moved" ) then
                -- do something
        end
        return true
end
 
-- put image in place
for i, names in ipairs(image_names) do
        i = i-1
        local img = display.newImageRect( names, img_size, img_size ) 
        
        img:setReferencePoint(display.TopLeftReferencePoint)
        img.x = side_space + img_space*(i%4) + img_size*(i%4)
        img.y = side_space + img_space*math.floor(i/4) + img_size*math.floor(i/4)
        scrollView:insert(img)
 
        img.caller = i -- pass a parameter to the listener via "caller"
        img:addEventListener( "touch", listener )
end

in that situation isnt it?

local function listener(self,event)

can't remember, but worth a try

You can access the image object with event.target.

1
2
3
4
5
6
7
local function listener(event) 
        print ("event.target.caller",event.target.caller) 
        if( event.phase == "moved" ) then
                -- do something
        end
        return true
end

timers: listener(event) => event.source = the timer
transitions: listener(obj) => obj = the transitioned object
touch: listener(event) => event.target = the object touched

so..

for timers:

1
2
3
4
5
6
7
8
9
 
local function timerComplete(event)
 
  print(event.source.something) -- "whatever"
 
end
 
local t1=timer.performWithDelay(100, timerComplete)
t1.something="whatever"

@jmp909,

You are correct and we should document the event properties.

One note on touch. The event.target will be nil if there is no object associated with the touch event. Having only a global touch listener will return nil when touching a display object.

Ok, i got it now :) Thanks for your time Tom.

BTW, the OP's question was answered by jwwtaker.

@MarkHenry, If you are talking about passing other parameters to the Listener function from timer, transition, or touch calls, it won't work unless you attach the parameters to object itself (as I mentioned previously). Any parameters within the Listener call (as in jwwtaker's example) is ignored.

@Tom. You're right, of course. The example was incomplete, and I assumed the syntax:

Runtime:addEventListener("touch", function() return myFunc(params) end)

Handy for some callbacks, but not appropriate for the OP's purpose.

And while adding a property to the event target is usually simpler, this way can be handy sometimes (at least it was for me):

local function listener(a, b)
        return function(event)
                print(event.name, event.phase, a, b)
        end
end
 
Runtime:addEventListener("touch", listener(1, 3))
Runtime:addEventListener("tap", listener(5, 7))

i don't know if that's a good idea. (I read anonymous functions trash the garbage colletor to some extent). I can't say for sure though. i guess if it works and doesnt cause problems then its ok for a workaround

will check it out anyway. hadn't thought of doing it like that

thanks
j

@MarkHenryC, you get extra points for your clever design. :)

For those following at home your solution is not passing parameters to the Listener function, but creating multiple versions of the Listener with local variables containing different values (1,3 and 5,7).

The reason the Runtime:addEventListener uses the listener function without the parenthesizes is because it needs the address of the function and not executing the function. If you add the parenthesizes it will call the listener at the time it sees the Runtime code and use the return value as the code address to be called when the event is triggered. In your example the listener gets called but it returns the address of an anonymous function and the use of Lua "closure" generates two local variables that hold the listener parameters (a and b).

Here is how the code would look if it written without the anonymous function generation:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
local function listener1(event)
   local a = 1
   local b = 3
   print(event.name, event.phase, a, b)
end
 
local function listener2(event)
   local a = 5
   local b = 7
   print(event.name, event.phase, a, b)
end
 
Runtime:addEventListener("touch", listener1)
Runtime:addEventListener("tap", listener2)

I am trying to use a listener for indicating when a sound has finished playing and in the main process after I invoke the playsound and pass the listener I go into a loop waiting for a variable to be set by the listener. The loop locks up the simulator because the variable never gets set by the listener.

Does anyone know how to do this? Basically, go into a holding pattern waiting for a listener to be fired?

Does Corona even support something like this with listeners?

@cminshall

In an event driven engine like Corona you can't use infinite loops. They run forever and never give the control back to Corona. That is why it hangs up.

Does anyone know how to do this? Basically, go into a holding pattern waiting for a listener to be fired?

Use a finite state machine setup for your enterFrame event.

Are there any code samples around regarding your suggestion (finite state machine)? I am new to Corona and Lua so any points in the right direction would be appreciated!

Thanks,
Chris

One clarification for my actual requirement is that I need to play a series of sounds one right after the other and so I need to kick off the next sound when the previous sound finishes playing.

hi,
how to convert the java script code to lua script code can any one tell me????

views:3205 update:2011/9/26 8:07:09
corona forums © 2003-2011