Can you send an extra value to an touch eventlistener?

Hello,

is it possible to add extra values to an ecentListener?

I am using this at the moment:

[lua]

local function Oefening1(event)

end

oefening.i1:addEventListener(“touch”,Oefening1)

[/lua]

could I add a value and have something like this:

[lua]

local function Oefening1(event,x)

print(x)

end

oefening.i1:addEventListener(“touch”,Oefening1,5)

[/lua]

So it would print the value I am sending with it?

Thanks

Never seen it used like that before. I know of a way but it involves more writing

local PrintOef(num) print(num) end   local function Oefening1(event) PrintOef(5) end   oefening.addEventListener( "touch", Oefening1 )

or you can do something like

local function Oefening1(event) local x = event.num print(x) end oefening.num = 5 oefening.addEventListener( "touch", Oefening1 )

You can add custom value to the object:

oefening.i1.myCustomValue = 5 oefening.i1:addEventListener("touch",Oefening1)

Your Oefening1 function would be

local function Oefening1(event) print(event.target.myCustomValue) end

Another options is

oefening.i1:addEventListener("touch",function() someFunciton(something) end)

@hatethekingofnames

What I want to do is just use one function without a lot of code to navigate.

Now I have a touch event function for each button. Each has a

[lua]

storyboard.gotoScene(“sceneName”)

[/lua]

If I could change the scenename to a variable I send with the eventlistener. I could use just this one function. Now I have 9 each with its own gotoscene. Works well. But would be nice if I could do it in one function.

Having a lot of

[lua]if event.target==1 then scene1

[/lua]

is a solution I don’t wan’t still a lot of code and doesn’t look a lot better.

Thanks @jonjonsson Will try that.

Give each of your oefening’s a custom value like jonjonsson said

oefening1.scene = 1 oefening2.scene = 2 --etc ..or you can even give them the entire name directly like oefening1.scene = "Level1" oefening2.scene = "Level2" -- not sure if its supposed to be with quotes or not but probably quotes

then

local sceneName = event.scene storyboard.gotoScene( sceneName )

Thanks,

it worked. Code will look a lot cleaner now.

Hello Juf, take a look at the last post in this thread:

http://forums.coronalabs.com/topic/28413-is-there-a-way-to-pack-extra-info-in-the-touch-event-table/

It can be handy too if you need to pass several parameters; it’s not too easy to understand at first (at least it wasn’t for me) but when you get it it is kinda useful.

Thanks will look at it.

Never seen it used like that before. I know of a way but it involves more writing

local PrintOef(num) print(num) end   local function Oefening1(event) PrintOef(5) end   oefening.addEventListener( "touch", Oefening1 )

or you can do something like

local function Oefening1(event) local x = event.num print(x) end oefening.num = 5 oefening.addEventListener( "touch", Oefening1 )

You can add custom value to the object:

oefening.i1.myCustomValue = 5 oefening.i1:addEventListener("touch",Oefening1)

Your Oefening1 function would be

local function Oefening1(event) print(event.target.myCustomValue) end

Another options is

oefening.i1:addEventListener("touch",function() someFunciton(something) end)

@hatethekingofnames

What I want to do is just use one function without a lot of code to navigate.

Now I have a touch event function for each button. Each has a

[lua]

storyboard.gotoScene(“sceneName”)

[/lua]

If I could change the scenename to a variable I send with the eventlistener. I could use just this one function. Now I have 9 each with its own gotoscene. Works well. But would be nice if I could do it in one function.

Having a lot of

[lua]if event.target==1 then scene1

[/lua]

is a solution I don’t wan’t still a lot of code and doesn’t look a lot better.

Thanks @jonjonsson Will try that.

Give each of your oefening’s a custom value like jonjonsson said

oefening1.scene = 1 oefening2.scene = 2 --etc ..or you can even give them the entire name directly like oefening1.scene = "Level1" oefening2.scene = "Level2" -- not sure if its supposed to be with quotes or not but probably quotes

then

local sceneName = event.scene storyboard.gotoScene( sceneName )

Thanks,

it worked. Code will look a lot cleaner now.

Hello Juf, take a look at the last post in this thread:

http://forums.coronalabs.com/topic/28413-is-there-a-way-to-pack-extra-info-in-the-touch-event-table/

It can be handy too if you need to pass several parameters; it’s not too easy to understand at first (at least it wasn’t for me) but when you get it it is kinda useful.

Thanks will look at it.