swap image the events not work

Hello,

I have a photo gallery with a big picture to other small below. I wish that when you touch one of the small images, this appears in the big picture.
my code is as follows and it works properly.

    photo:removeSelf()
    photo = nil
    
    photo = display.newImage(  “product/or”  … tostring(storyboard.state.idPhoto) … “_a.png”,  0,0 )
       
    photo:addEventListener( “touch”, photo )
  

My problem is that: after having destroyed and re-created the image, I again added the event touch, but this does not work anymore

the event before changing the image works, after changing the image no longer works

how can I fix it?

I apologize for my English
thanks

Your code is incorrect - you are adding an event listener called ‘photo’ to an object named ‘photo’. You need to give your listener a different name - i.e. photoListener.

the first time is called work fine.

The function is:

function photo:touch( event )

         local phase = event.phase
        local eventTime = event.time
        local previousTouches = self.previousTouches
        
        if not self.xScaleStart then
                self.xScaleStart, self.yScaleStart = self.xScale, self.yScale
        end
 …

In this case you’ll need to re-add the listener each time, when you nil photo you also nil the touch listener as it is a child of photo.

Instead you can define and add the touch listener like this and it will stay in place as it is not a child of photo.

[lua]

local photoListener = function (event)

photo:addEventListener(“touch”,photoListener)

[/lua]

Your code is incorrect - you are adding an event listener called ‘photo’ to an object named ‘photo’. You need to give your listener a different name - i.e. photoListener.

the first time is called work fine.

The function is:

function photo:touch( event )

         local phase = event.phase
        local eventTime = event.time
        local previousTouches = self.previousTouches
        
        if not self.xScaleStart then
                self.xScaleStart, self.yScaleStart = self.xScale, self.yScale
        end
 …

In this case you’ll need to re-add the listener each time, when you nil photo you also nil the touch listener as it is a child of photo.

Instead you can define and add the touch listener like this and it will stay in place as it is not a child of photo.

[lua]

local photoListener = function (event)

photo:addEventListener(“touch”,photoListener)

[/lua]