Buttons Stacking Exponentially

Alright, so I have a function that generates buttons from tables. Each table contains a list of 5 words, which are then loaded into 5 buttons. The issue I am having is that when you press one of the buttons, and it loads 5 new buttons, it loads them on top of the old ones, and when you press one, it presses all of the buttons underneath. The easy solution would seem to be to remove the old buttons, but I can’t seem to do it in a way that doesn’t crash the simulator. I was trying to use display.remove(button), but if I use it at the beginning of the function, it crashes. If I use it at the end, the buttons are erased instantly. If I use it at the beginning under an a boolean if statement, which is set to true after the first set of buttons are displayed, it crashes. Originally I assumed it was crashing because at first it was attempting to erase objects that had not yet been created, however the boolean statement should have resolved that, but hasn’t :/, any ideas? Am I using the wrong command or something? I was also thought it might be because the buttons were local within each execution of the function, but I tried making them global, and it didn’t seem to make a difference…

Here’s the code without any button erasing using local variables:

[code]buttonHandler = function ( event )
lastTable = curTable
curWord = event.id
curTable = masterTable[curWord]

bWord1 = ui.newButton{
default = “button.png”,
over = “buttonGrayOver.png”,
onEvent = buttonHandler,
text = curTable[1],
id = curTable[1],
size = 20,
emboss = true
}
bWord1.x = display.contentWidth / 2
bWord1.y = display.contentHeight / 5 * 1

bWord2 = ui.newButton{
default = “button.png”,
over = “buttonGrayOver.png”,
onEvent = buttonHandler,
text = curTable[2],
id = curTable[2],
size = 20,
emboss = true
}
bWord2.x = display.contentWidth / 2
bWord2.y = display.contentHeight / 5 * 2

bWord3 = ui.newButton{
default = “button.png”,
over = “buttonGrayOver.png”,
onEvent = buttonHandler,
text = curTable[3],
id = curTable[3],
size = 20,
emboss = true
}
bWord3.x = display.contentWidth / 2
bWord3.y = display.contentHeight / 5 * 3

bWord4 = ui.newButton{
default = “button.png”,
over = “buttonGrayOver.png”,
onEvent = buttonHandler,
text = curTable[4],
id = curTable[4],
size = 20,
emboss = true
}
bWord4.x = display.contentWidth / 2
bWord4.y = display.contentHeight / 5 * 4

bWord5 = ui.newButton{
default = “button.png”,
over = “buttonGrayOver.png”,
onEvent = buttonHandler,
text = curTable[5],
id = curTable[5],
size = 20,
emboss = true
}
bWord5.x = display.contentWidth / 2
bWord5.y = display.contentHeight

end
[/code] [import]uid: 197365 topic_id: 37535 reply_id: 67535[/import]

What error are you getting when it crashes? The problem is probably somewhere else in your code, but it’s tough to tell without the error.
If you want to just make sure the buttons below don’t receive events, just add the line

return true

to the end of your function. However, I would recommend that you remove the old buttons, to save memory.

P.S. - It looks like you’re only changing the text of the buttons, in which case it’d be better to just use the “setLabel” function, like so:

bWord1:setLabel(curTable[1])

What error are you getting when it crashes? The problem is probably somewhere else in your code, but it’s tough to tell without the error.
If you want to just make sure the buttons below don’t receive events, just add the line

return true

to the end of your function. However, I would recommend that you remove the old buttons, to save memory.

P.S. - It looks like you’re only changing the text of the buttons, in which case it’d be better to just use the “setLabel” function, like so:

bWord1:setLabel(curTable[1])