How to add an eventListenrt "touch" function

Hello.

I would like to have 1 “touch” function for many objects.

I create 20 images in a loop – for i = 1 …

each object has a unique name, like a1, a2, a3 because i use a[i]

as far as I understand a touch function you put the name of the variable 3 times, like

    function a1:touch( event )     if event.phase == "began" then         audio.play(n44)         -- set touch focus         display.getCurrentStage():setFocus( self )         self.isFocus = true     elseif self.isFocus then         if event.phase == "moved" then         elseif event.phase == "ended" or event.phase == "cancelled" then         audio.stop (  )             -- reset touch focus             display.getCurrentStage():setFocus( nil )             self.isFocus = nil         end     end     return true     end     a1:addEventListener( "touch", a1 )

it works perfect, I touch a1 and plays the audio.

but how can I get the same function and make

a1 play audio 1

a2 play audio 2

a3 play audio 3 and so on…

Thanks for all your help, and I hope you can understand my question

Victor

Hi Victor,

If you want to use a common “touch” function for many objects, I suggest that you create the function separately of the objects and just refer to that function from each object. For example:

[lua]

local function playAudio( event )

   print( event.phase )

   print( event.target )

end

[/lua]

Then in the loop:

[lua]

for i=1,10 do

   local object = display.newImageRect( … )  --your code here

   object:addEventListener( “touch”, playAudio )

end

[/lua]

Hope this helps,

Brent

Hi Brent, thanks for responding.

as far as I know, and the way I do it.

to create a loop for i = 1

I have to declare a table

local object = { }

then I have to do the loop, but I do

object [i] = display…

and i get 10 objects

 object [1]

 object [2] … and so on

in your code I don’t see the [i]

so I don’t get 10 objects, and it’s not really working I get errors

Also…

if it is only 1 function… object:addEventListener( “touch”, playAudio )

how do the program “knows” if it is object [4] or object [2]

and how do I get INSIDE the function

if object[2] == “red” then ---- do this

if object[6] == “yellow” then ---- do something else

because each object is doing something different

Thanks Brent

Hi Victor,

OK, it’s easy enough to create those objects as items in a table, like this:

[lua]

local objects = {}  --containing table for objects

for i=1,10 do

    objects[i] = display.newImageRect( … )

    objects[i]:addEventListener( “touch”, playAudio )

end

[/lua]

Then in the common touch handling function, “event.target” is a reference to the object that was touched. This is especially important to remember. Once you have that reference, you can read/set any number of properties from/to that object, because after all, it’s a direct reference to the display object you’re concerned with (the one that was touched).

This guide may help you further:

http://docs.coronalabs.com/guide/events/touchMultitouch/index.html

Brent

Hi Brent… I think I am very close… very close…

I get numbers like this –   Unique touch ID: userdata: 0x10daf0f30

or like this

Apr 10 16:41:16.157: began
Apr 10 16:41:16.157: table: 0x61000226c380
Apr 10 16:41:16.157: 150
Apr 10 16:41:16.157: 558
Apr 10 16:41:16.258: ended

I am expecting to get ONE number like 1, 2, 3, 4, 5…

so I can say: if it’s 5 do this and so on

my code is this

    function playAudio( event )        print( event.phase )        print( event.target )        print( event.x )        print( event.y )        print( "Unique touch ID: "..tostring(event.id) )     end

      local number = 1       for i = 1, 10 do           object[i] = display.newText( sceneGroup, "A", 0, 0, native.systemFont, 30 )         object[i].x = 100 + i\*50         object[i].y = display.contentCenterY + 100         object[i]:setFillColor( 1, 0, 0 )         object[i].id = number         object[i]:addEventListener( "touch", playAudio )         number = number + 1     end     object[5].text = "B"

I try with the … id

or maybe the ;;;; name

I think I need just a little bit more…

Thanks you for everything Brent

this is a Hugh thing for my app right now

thanks!

Hi Victor,

Yes, you are very close. Just one little thing:

“event.id” is an internal Corona refernce to the touch on the screen, for use with multi-touch generally. You may not even need to worry about this.

“event .target.id” is the number (id) that you assigned to the object… remember that “event.target” is the object, so any additional property you assign to the object needs to be referenced as “event.target.(propertyName)”

Brent

Hi Brent… You made my YEAR!!! not my day… my YEAR!!!

this was the key –  “event .target.id”

Thank you very much, this will help a lot on my app…

I had a code of 3429 lines I will reduce that at least by 1000 lines

Thank you, thank you, thank you…

Victor

Hi Brent… one extra little thing about this post.

I want to play the audio if it’s id 5 on “began”

and on “ended” I want to stop audio

so where exactly I have to put the ended or moved phases?

The function I have is like this

    function playAudio( event )            if event.phase == "began" and ( event.target.id ) == 1 then               audio.play(n49)            elseif event.phase == "began" and ( event.target.id ) == 3 then                audio.play(n46)            elseif event.phase == "began" and ( event.target.id ) == 15 then                audio.play(n46)         else         return true                     end     end

and works perfect, but where exactly I have to add the extra touch

“moved” and “ended”?

and do I need all the extra stuff like

        – set touch focus
        display.getCurrentStage():setFocus( self )
        self.isFocus = true

    elseif self.isFocus then
        if event.phase == “moved” then

        elseif event.phase == “ended” or event.phase == “cancelled” then
        audio.stop( )
            – reset touch focus
            display.getCurrentStage():setFocus( nil )
            self.isFocus = nil

because I have so many – elseif…

I don’t get where I have to put  [end] for each one

thanks

Hi Victor,

You should probably wrap/combine the conditional blocks more cleanly, like this:

[lua]

function playAudio( event )

    if event.phase == “began”

        if ( event.target.id ) == 1 then

            audio.play(n49)

        --etc.

        end

    elseif event.phase == “ended” then

        audio.stop()

    end

end

[/lua]

Of course, that will stop all audio on any ended phase. If you only want to stop the audio for that object, then it gets a little more complicated.

Brent

This works really good. For now it’s the same action in the ended phase

so that works great…

there is so much to learn…

Thanks for all your help

Victor

Hi Victor,

If you want to use a common “touch” function for many objects, I suggest that you create the function separately of the objects and just refer to that function from each object. For example:

[lua]

local function playAudio( event )

   print( event.phase )

   print( event.target )

end

[/lua]

Then in the loop:

[lua]

for i=1,10 do

   local object = display.newImageRect( … )  --your code here

   object:addEventListener( “touch”, playAudio )

end

[/lua]

Hope this helps,

Brent

Hi Brent, thanks for responding.

as far as I know, and the way I do it.

to create a loop for i = 1

I have to declare a table

local object = { }

then I have to do the loop, but I do

object [i] = display…

and i get 10 objects

 object [1]

 object [2] … and so on

in your code I don’t see the [i]

so I don’t get 10 objects, and it’s not really working I get errors

Also…

if it is only 1 function… object:addEventListener( “touch”, playAudio )

how do the program “knows” if it is object [4] or object [2]

and how do I get INSIDE the function

if object[2] == “red” then ---- do this

if object[6] == “yellow” then ---- do something else

because each object is doing something different

Thanks Brent

Hi Victor,

OK, it’s easy enough to create those objects as items in a table, like this:

[lua]

local objects = {}  --containing table for objects

for i=1,10 do

    objects[i] = display.newImageRect( … )

    objects[i]:addEventListener( “touch”, playAudio )

end

[/lua]

Then in the common touch handling function, “event.target” is a reference to the object that was touched. This is especially important to remember. Once you have that reference, you can read/set any number of properties from/to that object, because after all, it’s a direct reference to the display object you’re concerned with (the one that was touched).

This guide may help you further:

http://docs.coronalabs.com/guide/events/touchMultitouch/index.html

Brent

Hi Brent… I think I am very close… very close…

I get numbers like this –   Unique touch ID: userdata: 0x10daf0f30

or like this

Apr 10 16:41:16.157: began
Apr 10 16:41:16.157: table: 0x61000226c380
Apr 10 16:41:16.157: 150
Apr 10 16:41:16.157: 558
Apr 10 16:41:16.258: ended

I am expecting to get ONE number like 1, 2, 3, 4, 5…

so I can say: if it’s 5 do this and so on

my code is this

    function playAudio( event )        print( event.phase )        print( event.target )        print( event.x )        print( event.y )        print( "Unique touch ID: "..tostring(event.id) )     end

      local number = 1       for i = 1, 10 do           object[i] = display.newText( sceneGroup, "A", 0, 0, native.systemFont, 30 )         object[i].x = 100 + i\*50         object[i].y = display.contentCenterY + 100         object[i]:setFillColor( 1, 0, 0 )         object[i].id = number         object[i]:addEventListener( "touch", playAudio )         number = number + 1     end     object[5].text = "B"

I try with the … id

or maybe the ;;;; name

I think I need just a little bit more…

Thanks you for everything Brent

this is a Hugh thing for my app right now

thanks!

Hi Victor,

Yes, you are very close. Just one little thing:

“event.id” is an internal Corona refernce to the touch on the screen, for use with multi-touch generally. You may not even need to worry about this.

“event .target.id” is the number (id) that you assigned to the object… remember that “event.target” is the object, so any additional property you assign to the object needs to be referenced as “event.target.(propertyName)”

Brent

Hi Brent… You made my YEAR!!! not my day… my YEAR!!!

this was the key –  “event .target.id”

Thank you very much, this will help a lot on my app…

I had a code of 3429 lines I will reduce that at least by 1000 lines

Thank you, thank you, thank you…

Victor

Hi Brent… one extra little thing about this post.

I want to play the audio if it’s id 5 on “began”

and on “ended” I want to stop audio

so where exactly I have to put the ended or moved phases?

The function I have is like this

    function playAudio( event )            if event.phase == "began" and ( event.target.id ) == 1 then               audio.play(n49)            elseif event.phase == "began" and ( event.target.id ) == 3 then                audio.play(n46)            elseif event.phase == "began" and ( event.target.id ) == 15 then                audio.play(n46)         else         return true                     end     end

and works perfect, but where exactly I have to add the extra touch

“moved” and “ended”?

and do I need all the extra stuff like

        – set touch focus
        display.getCurrentStage():setFocus( self )
        self.isFocus = true

    elseif self.isFocus then
        if event.phase == “moved” then

        elseif event.phase == “ended” or event.phase == “cancelled” then
        audio.stop( )
            – reset touch focus
            display.getCurrentStage():setFocus( nil )
            self.isFocus = nil

because I have so many – elseif…

I don’t get where I have to put  [end] for each one

thanks

Hi Victor,

You should probably wrap/combine the conditional blocks more cleanly, like this:

[lua]

function playAudio( event )

    if event.phase == “began”

        if ( event.target.id ) == 1 then

            audio.play(n49)

        --etc.

        end

    elseif event.phase == “ended” then

        audio.stop()

    end

end

[/lua]

Of course, that will stop all audio on any ended phase. If you only want to stop the audio for that object, then it gets a little more complicated.

Brent

This works really good. For now it’s the same action in the ended phase

so that works great…

there is so much to learn…

Thanks for all your help

Victor