Adding touch event listener to images using table as name

I am using this code to add several pictures and add touch listener to them. It works fine when there is no “][1]” but when using a table as image display it doesnt work anymore. As there are 30 pictures I dont want to add them one by one rather add or using myBall{} table.

  

myBall={}     myBall[1] = display.newImage( "letters/a.jpg", 150,150)     myBall[1].xScale = 0.5     myBall[1].yScale = 0.5     function myBall[1]:touch( event )         if event.phase == "began" then           if touched == false then             end                           -- set touch focus             display.getCurrentStage():setFocus( self )             self.isFocus = true         elseif self.isFocus then             if event.phase == "moved" then               myBall[1].x = event.x               myBall[1].y = event.y             elseif event.phase == "ended" or event.phase == "cancelled" then                 -- reset touch focus                 display.getCurrentStage():setFocus( nil )                 self.isFocus = nil             end         end         return true     end      myBall[1]:addEventListener( "touch" )

Try this:

 function ballTouch( event ) if event.phase == "began" then if touched == false then end -- set touch focus display.getCurrentStage():setFocus( self ) self.isFocus = true elseif self.isFocus then if event.phase == "moved" then self.x = event.x self.y = event.y elseif event.phase == "ended" or event.phase == "cancelled" then -- reset touch focus display.getCurrentStage():setFocus( nil ) self.isFocus = nil end end return true end myBall[1].touch = ballTouch myBall[1]:addEventListener("touch",myBall[1])

Also, don’t forget to use code <> tags!

EDIT: BTW this is an example of a table listener, which can be found in the docs below:

https://docs.coronalabs.com/api/event/touch/index.html#function-listener

When I touch the object then I get this error message:

main.lua:404: attempt to index global 'self' (a nil value)

which coresponds to this line:

&nbsp; &nbsp; &nbsp; &nbsp; elseif self.isFocus then

Yep, that’s my bad. I should have put:

function ballTouch( self, event )

Sorry about that!

Great! Thanks for the help.

Try this:

 function ballTouch( event ) if event.phase == "began" then if touched == false then end -- set touch focus display.getCurrentStage():setFocus( self ) self.isFocus = true elseif self.isFocus then if event.phase == "moved" then self.x = event.x self.y = event.y elseif event.phase == "ended" or event.phase == "cancelled" then -- reset touch focus display.getCurrentStage():setFocus( nil ) self.isFocus = nil end end return true end myBall[1].touch = ballTouch myBall[1]:addEventListener("touch",myBall[1])

Also, don’t forget to use code <> tags!

EDIT: BTW this is an example of a table listener, which can be found in the docs below:

https://docs.coronalabs.com/api/event/touch/index.html#function-listener

When I touch the object then I get this error message:

main.lua:404: attempt to index global 'self' (a nil value)

which coresponds to this line:

&nbsp; &nbsp; &nbsp; &nbsp; elseif self.isFocus then

Yep, that’s my bad. I should have put:

function ballTouch( self, event )

Sorry about that!

Great! Thanks for the help.