Question on AddEventListener "Touch"

Hi Everyone…

I have 1 image

    bgOff = display.newImageRect( sceneGroup, "images/sBgOff.png", 430, 430 )     bgOff.x = display.contentCenterX - separacionX     bgOff.y = display.contentCenterY     bgOff:scale (.7, .7)     bgOff.anchorX = 1     bgOff.anchorY = 1     bgOff.isVisible = true

I add 1 function “touch”

    function bgOff:touch( event )         if event.phase == "began" then             quitaFlechita()             bgOn.isVisible = true             bgOff.isVisible = false             bringBgs()             return true         end     end     bgOff:addEventListener( "touch", bgOff )

WORKS FINE!

the problem I have is when I want to use the “loop”  – for i …

Like this I have 7 images in 1 loop, they work great

    local numerito = 1     for i = 1, 7 do         nail[i] = display.newImageRect( sceneGroup, "images/nail" .. numerito .. ".png", 250, 188 )         nail[i].x = - 100         nail[i].y = -30 + i\*120         nail[i]:scale (.6, .6)         nail[i].isVisible = true         numerito = numerito + 1     end

Now… How do I add an event listener to all 7 of them, so I can do something different on each of them

I did this and it won’t work

    function nail[i]:touch( event )         if event.phase == "began" then             transition.to(negrito, {time=100, x=100, y=nail[1].y})             return true         end     end     nail[i]:addEventListener( "touch", nail[i] )

even if I change … nail[i]    for  nail[1]

Please I need your help, thank you very much for everything you do to help me

and all of us in the forum, thanks.

Victor

So far 13 views and nothing…

So far 16 views and nothing…

Please be patient and give the community time to respond.

Rob

It’s okay Rob, I am sorry.

My intention was not to rush anybody.

While I was working on the code, I was just checking the forum

and I thought it would be nice to keep track of how many views

before someone will answer…

I will wait

thanks

You could try something like… function doSomething ( event ) local obj = event.target if event.phase == “began” then If obj.name == 1 then ---- elseif obj.name == 2 then ---- continue for 7 conditions end return true end end local numerito = 1 for i = 1, 7 do nail[i] = display.newImageRect( sceneGroup, “images/nail” … numerito … “.png”, 250, 188 ) nail[i].name = i nail[i].x = - 100 nail[i].y = -30 + i*120 nail[i]:scale (.6, .6) nail[i].isVisible = true numerito = numerito + 1 nail[i]:addEventListener( “touch”, doSomething) end

One thing that is very confusing about your question is in each example you give you seem to be referring to different objects: bgOn, bgOff, nail, negrito.

(You didn’t get an answer because its hard to understand what you’re trying to do other than re-use the code for a touch listener.)

However, to make one object and re-use the same code for a touch listener do this ( note use of ‘self’ ):

local function touch( self, event ) if event.phase == "began" then self.isVisible = true return true end return false end local numerito = 1 for i = 1, 7 do nail[i] = display.newImageRect( sceneGroup, "images/nail" .. numerito .. ".png", 250, 188 ) nail[i].x = - 100 nail[i].y = -30 + i\*120 nail[i]:scale (.6, .6) nail[i].isVisible = true nail[i].touch = touch nail[i]:addEventListener("touch") numerito = numerito + 1 end

Hi “kilopop” and “roaminggamer” thanks for your answer…

I use a little bit of each to make this

local function touch( self, event )         local obj = event.target            if event.phase == "began" then               self.isVisible = true                      if obj.name == 1 then                   print("el numero 1")               elseif obj.name == 2 then                   print("el numero 2")               elseif obj.name == 3 then                   print("el numero 3")               elseif obj.name == 4 then                   print("el numero 4")               elseif obj.name == 5 then                   print("el numero 5")               elseif obj.name == 6 then                   print("el numero 6")               else                   print("el numero 7")             end            end            return false     end local numerito = 1 for i = 1, 7 do    nail[i] = display.newImageRect( sceneGroup, "images/nail" .. numerito .. ".png", 250, 188 )    nail[i].x = display.contentCenterX    nail[i].y = display.contentCenterY + i\*120    nail[i]:scale (.6, .6)    nail[i].isVisible = true    nail[i].name = i    nail[i].touch = touch    nail[i]:addEventListener("touch")    numerito = numerito + 1 end

I use the obj.name from top

and in the self, example I didn’t know how to detect each object

and in the example above I didn’t use the “self” inside the (event) function

is kind of confusing, but now it works the way I was expecting…

But just for me learning more… could you please explain a little bit more… thanks

Victor

So far 13 views and nothing…

So far 16 views and nothing…

Please be patient and give the community time to respond.

Rob

It’s okay Rob, I am sorry.

My intention was not to rush anybody.

While I was working on the code, I was just checking the forum

and I thought it would be nice to keep track of how many views

before someone will answer…

I will wait

thanks

You could try something like… function doSomething ( event ) local obj = event.target if event.phase == “began” then If obj.name == 1 then ---- elseif obj.name == 2 then ---- continue for 7 conditions end return true end end local numerito = 1 for i = 1, 7 do nail[i] = display.newImageRect( sceneGroup, “images/nail” … numerito … “.png”, 250, 188 ) nail[i].name = i nail[i].x = - 100 nail[i].y = -30 + i*120 nail[i]:scale (.6, .6) nail[i].isVisible = true numerito = numerito + 1 nail[i]:addEventListener( “touch”, doSomething) end

One thing that is very confusing about your question is in each example you give you seem to be referring to different objects: bgOn, bgOff, nail, negrito.

(You didn’t get an answer because its hard to understand what you’re trying to do other than re-use the code for a touch listener.)

However, to make one object and re-use the same code for a touch listener do this ( note use of ‘self’ ):

local function touch( self, event ) if event.phase == "began" then self.isVisible = true return true end return false end local numerito = 1 for i = 1, 7 do nail[i] = display.newImageRect( sceneGroup, "images/nail" .. numerito .. ".png", 250, 188 ) nail[i].x = - 100 nail[i].y = -30 + i\*120 nail[i]:scale (.6, .6) nail[i].isVisible = true nail[i].touch = touch nail[i]:addEventListener("touch") numerito = numerito + 1 end

Hi “kilopop” and “roaminggamer” thanks for your answer…

I use a little bit of each to make this

local function touch( self, event )         local obj = event.target            if event.phase == "began" then               self.isVisible = true                      if obj.name == 1 then                   print("el numero 1")               elseif obj.name == 2 then                   print("el numero 2")               elseif obj.name == 3 then                   print("el numero 3")               elseif obj.name == 4 then                   print("el numero 4")               elseif obj.name == 5 then                   print("el numero 5")               elseif obj.name == 6 then                   print("el numero 6")               else                   print("el numero 7")             end            end            return false     end local numerito = 1 for i = 1, 7 do    nail[i] = display.newImageRect( sceneGroup, "images/nail" .. numerito .. ".png", 250, 188 )    nail[i].x = display.contentCenterX    nail[i].y = display.contentCenterY + i\*120    nail[i]:scale (.6, .6)    nail[i].isVisible = true    nail[i].name = i    nail[i].touch = touch    nail[i]:addEventListener("touch")    numerito = numerito + 1 end

I use the obj.name from top

and in the self, example I didn’t know how to detect each object

and in the example above I didn’t use the “self” inside the (event) function

is kind of confusing, but now it works the way I was expecting…

But just for me learning more… could you please explain a little bit more… thanks

Victor