remove event listener and add again

Hello, I have a grow and pick function below. I am trying place a limit on how many “straws” can be picked, I have created a function basketFull() to remove the straw “tap” event listener, but they keep adding up. Here is the code I am working with:

 

[lua]

 

local m = require(“myvars”)

local basketFull()

 

local function strawTap(e)

print(e.target.name)

m.addTobasketItems(1)

transition.to(e.target, { xScale = .01, yScale = .01})

basketFull()

return true

end

 

local maxStraws = 5

 

local straws = {“strawberry.png”, “strawberry2.png”, “strawberry3.png”}

local allStraws = {}

 

 

local function makeAStraw(id)

local strawidx = rnd(1, 3)

local randX = rnd(400, 700)

local randY = rnd(670, 720)

local randRot = rnd(-10, 10)

local strawsize = rnd(1, 4) / 100

straw = display.newImage(“images/” … straws[strawidx]) 

straw.x = randX

straw.y = randY

straw.rotation = randRot

straw:scale ( strawsize, strawsize )

straw.name = “straw” … tostring(id)

straw:addEventListener ( “tap”, strawTap )

allStraws[#allStraws+1] = straw

scene.view:insert( straw ) 

end

 

local function turnOnStraws()

for i = 1, #allStraws do

TurnOnStrawsTrans = transition.to(allStraws[i], {time = 3000, xScale = .15, alpha = 1, yScale = .15,})

end

end

 

function basketFull()

if m.basketItems >= 3 then

straw:removeEventListener ( “tap”, strawTap )

elseif m.basketItems <= 3 then

straw:addEventListener ( “tap”, strawTap )

end

return true

end

[/lua]

 

Would love to hear from anyone who can tell me how to add and remove the event listener on “straw”, so I can create some limitations on picking.

 

Many thanks!

Hi Corona Community, What I’m trying to figure out in the above code with failed tags… sorry, is how to remove a tap listener when the score m.basketItems <= 3. functionBasketFull() at the bottom of the code is where it is called. straw:removeEventListener ( “tap”, strawTap ) is just being ignored upon reaching 3 and keeps allowing the tap.

Any help on this would be greatly appreciated. I plan on using this over and over in my code. thanks!

Hi @jleap,

This is basically a scoping/syntax issue. You’re assigning (and re-assigning) new display objects (images) to what is presumably a global variable “straw”. Then later, you’re trying to reference that object and remove the tap listener, but since you’ve re-assigned new display objects to the same variable, you’re not targeting the previous ones, so the tap listener remains on them. Basically, you need to target the specific object that you want to remove the listener on, or if you want to remove the tap listeners on all strawberries after the basket contains a certain number, you should loop through the table (“allStraws”?) which contains a reference to each one, and remove the listener on each as you loop through.

Hope this helps,

Brent

P.S. - If you’re using any, note that global variables are bad news and should be avoided unless absolutely required for some reason.

Brent, thank you so much for taking a look at this and offering a solution for my code prob.

In code that I previously posted, as an effort to simplify, I left out “local straw” at the top. Per your solution above, I am not trying to call straw as a global. 

Please, if you will or anyone else in the Corona Community, try to run new code offered here. I have also attached the “straw” assets. Kindly help me figure out why I am not able to remove strawTap event when Score >=3? The basketFull() function is where I am trying to call for the event listener to be removed. Here is new, clean code:

[lua]

local Score = 0

local strawScoreTxt

local straw

function addToScore(num)

Score = Score + num

strawScoreTxt.text = Score

end

local w = display.contentWidth

local h = display.contentHeight

local rnd = math.random

local maxStraws = 10

local straws = {“strawberry.png”, “strawberry2.png”, “strawberry3.png”}

local allStraws = {}

local function strawTap(e)

e.target.alpha = 0

addToScore(1)

return true

end

local function basketFull()

if Score >= 3 then

straw:removeEventListener ( “tap”, strawTap )

elseif Score <= 3 then

straw:addEventListener ( “tap”, strawTap )

end

end

local function makeAStraw(id)

local strawidx = rnd(1, #straws)

local randX = rnd(20, w-20)

local randY = rnd(20, h-20)

local strawsize = rnd(25, 75) / 100

straw = display.newImage(straws[strawidx])

straw.x = randX

straw.y = randY

straw:scale ( strawsize, strawsize )

straw.name = “straw” … tostring(id)

straw:addEventListener ( “tap”, strawTap )

allStraws[#allStraws+1] = straw

end

for v = 1, maxStraws do

makeAStraw(v)

end

local function turnOnStraws()

for i = 1, #allStraws do

allStraws[i].alpha = 1

end

end

Runtime:addEventListener ( “tap”, turnOnStraws )

strawScoreTxt = display.newText(tostring(Score), 0, 0, native.systemFont, 18)

strawScoreTxt.x = 400

strawScoreTxt.y  = 10

strawScoreTxt.isVisible = true

[/lua]

Hi Corona Community, What I’m trying to figure out in the above code with failed tags… sorry, is how to remove a tap listener when the score m.basketItems <= 3. functionBasketFull() at the bottom of the code is where it is called. straw:removeEventListener ( “tap”, strawTap ) is just being ignored upon reaching 3 and keeps allowing the tap.

Any help on this would be greatly appreciated. I plan on using this over and over in my code. thanks!

Hi @jleap,

This is basically a scoping/syntax issue. You’re assigning (and re-assigning) new display objects (images) to what is presumably a global variable “straw”. Then later, you’re trying to reference that object and remove the tap listener, but since you’ve re-assigned new display objects to the same variable, you’re not targeting the previous ones, so the tap listener remains on them. Basically, you need to target the specific object that you want to remove the listener on, or if you want to remove the tap listeners on all strawberries after the basket contains a certain number, you should loop through the table (“allStraws”?) which contains a reference to each one, and remove the listener on each as you loop through.

Hope this helps,

Brent

P.S. - If you’re using any, note that global variables are bad news and should be avoided unless absolutely required for some reason.

Brent, thank you so much for taking a look at this and offering a solution for my code prob.

In code that I previously posted, as an effort to simplify, I left out “local straw” at the top. Per your solution above, I am not trying to call straw as a global. 

Please, if you will or anyone else in the Corona Community, try to run new code offered here. I have also attached the “straw” assets. Kindly help me figure out why I am not able to remove strawTap event when Score >=3? The basketFull() function is where I am trying to call for the event listener to be removed. Here is new, clean code:

[lua]

local Score = 0

local strawScoreTxt

local straw

function addToScore(num)

Score = Score + num

strawScoreTxt.text = Score

end

local w = display.contentWidth

local h = display.contentHeight

local rnd = math.random

local maxStraws = 10

local straws = {“strawberry.png”, “strawberry2.png”, “strawberry3.png”}

local allStraws = {}

local function strawTap(e)

e.target.alpha = 0

addToScore(1)

return true

end

local function basketFull()

if Score >= 3 then

straw:removeEventListener ( “tap”, strawTap )

elseif Score <= 3 then

straw:addEventListener ( “tap”, strawTap )

end

end

local function makeAStraw(id)

local strawidx = rnd(1, #straws)

local randX = rnd(20, w-20)

local randY = rnd(20, h-20)

local strawsize = rnd(25, 75) / 100

straw = display.newImage(straws[strawidx])

straw.x = randX

straw.y = randY

straw:scale ( strawsize, strawsize )

straw.name = “straw” … tostring(id)

straw:addEventListener ( “tap”, strawTap )

allStraws[#allStraws+1] = straw

end

for v = 1, maxStraws do

makeAStraw(v)

end

local function turnOnStraws()

for i = 1, #allStraws do

allStraws[i].alpha = 1

end

end

Runtime:addEventListener ( “tap”, turnOnStraws )

strawScoreTxt = display.newText(tostring(Score), 0, 0, native.systemFont, 18)

strawScoreTxt.x = 400

strawScoreTxt.y  = 10

strawScoreTxt.isVisible = true

[/lua]