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!