Removing buttons and disabling event listeners

Hi, i have been staring at this for days, please help!

I am using ghosts vs monsters code as my basis,

I wish to have four button options at the start of each round of my game (there are 4 rounds in a level)

when a button is pressed it needs to transition to alpha 0.5, capture its variable and disable the event listeners on other buttons.

i am new to lua and corona and have placed my code after the ‘activateRound’ call in ghosts vs monsters. The code below captures the variable i need but does not remove event listeners and reloads with each new round (replacing the images with new versions)

Please advise how i can progress this, thank you!

[code]

– FOOD SELECT

local selectFood = function()

local foodBtn1 = display.newImageRect(“levelimg/sun.png”, 40, 40)
foodBtn1.x = _W/2; foodBtn1.y = _H/4
foodBtn1.alpha = 1

local foodBtn2 = display.newImageRect(“levelimg/sun.png”, 40, 40)
foodBtn2.x = _W/2 + 40; foodBtn2.y = _H/4
foodBtn2.alpha = 1

local foodBtn3 = display.newImageRect(“levelimg/sun.png”, 40, 40)
foodBtn3.x = _W/2 + 80; foodBtn3.y = _H/4
foodBtn3.alpha = 1

local foodBtn4 = display.newImageRect(“levelimg/sun.png”, 40, 40)
foodBtn4.x = _W/2 + 120; foodBtn4.y = _H/4
foodBtn4.alpha = 1

feedGroup:insert(foodBtn1)
feedGroup:insert(foodBtn2)
feedGroup:insert(foodBtn3)
feedGroup:insert(foodBtn4)

function chooseFood1 (e)
if e.phase==“ended” then
foodSelection1 = “Chilli”
transition.to( foodBtn1, { time=3000, alpha=0.5 } )
print(“food:” … foodSelection );

end
end

local function chooseFood2 (e)
if e.phase==“ended” then
foodSelection = “Chips”
transition.to( foodBtn2, { time=3000, alpha=0.5 } )
print(“food:” … foodSelection );

end
end

function chooseFood3 (e)
if e.phase==“ended” then
foodSelection = “Curry”
transition.to( foodBtn3, { time=3000, alpha=0.5 } )
print(“food:” … foodSelection );
end
end

function chooseFood4 (e)
if e.phase==“ended” then
foodSelection = “Burgers”
transition.to( foodBtn4, { time=3000, alpha=0.5 } )
print(“food:” … foodSelection );
end
end

foodBtn1:addEventListener(“touch”, chooseFood1);
foodBtn2:addEventListener(“touch”, chooseFood2);
foodBtn3:addEventListener(“touch”, chooseFood3);
foodBtn4:addEventListener(“touch”, chooseFood4);

if foodSelection == “Chilli” then
print(“food:” … foodSelection );
foodBtn1:removeEventListener( “touch”, chooseFood1 );
foodBtn2:removeEventListener( “touch”, chooseFood2 );
foodBtn3:removeEventListener( “touch”, chooseFood3 );
foodBtn4:removeEventListener( “touch”, chooseFood4 );
end
end

– END FOOD SELECT
selectFood()
[/code] [import]uid: 113162 topic_id: 22358 reply_id: 322358[/import]

Use a on complete function instead:

  
 function chooseFood1 (e)  
 if e.phase=="ended" then  
 foodSelection1 = "Chilli"  
 transition.to( foodBtn1, { time=3000, alpha=0.5 onComplete=killListener} )  
 print("food:" .. foodSelection );  
  
 end  
 end  
  
local killListener = function( )  
 foodBtn1:removeEventListener( "touch", chooseFood1 );  
end  

Let me know how it goes. Untested above. [import]uid: 118379 topic_id: 22358 reply_id: 89140[/import]

Thank you for looking at this, i can’t seem to get this to work (even just trying to return a print statement, anything else you could suggest?

Thanks again, [import]uid: 113162 topic_id: 22358 reply_id: 89152[/import]