Dynamics buttons

Hello, I have problems to create many buttons and call functions in a while loop … I have an error that is not how to solve: “attempt to call method ‘addEventListener’ (a nil value)”:

How should successfully create these dynamic buttons?
Thanks for the help.

[code]

local i=0
while i<10 do
–BOTON EDITAR
LISTADO_botonEditar={}
LISTADO_botonEditar[i+1]={}

LISTADO_botonEditar[i+1].accion = function(event)
if event.phase == “began” then
LISTADO_botonEditar[i+1].rectangulo.alpha=0.3
LISTADO_botonEditar[i+1].texto.alpha=0.3
end
if event.phase == “ended” then
print ("Boton presionado: "…LISTADO_botonEditar[i+1].ID)

end
if event.phase == “moved” then
–Nada
end
end


–Rectangulo
LISTADO_botonEditar[i+1].ID=i+1
LISTADO_botonEditar[i+1].rectangulo = display.newImageRect(“imagenes/btn_editar.png”, 37,14)
LISTADO_botonEditar[i+1].rectangulo.x = 105
LISTADO_botonEditar[i+1].rectangulo.y = (25*i)
LISTADO_botonEditar[i+1].rectangulo.alpha=1
LISTADO_botonEditar[i+1]:addEventListener (“touch”, LISTADO_botonEditar[i+1].accion)
LISTADO_MC_hoja_items:insert(LISTADO_botonEditar[i+1].rectangulo)

–Nombre
LISTADO_botonEditar[i+1].texto = display.newText("", 0, 0, “OriyaMN”, 7)
LISTADO_botonEditar[i+1].texto:setTextColor(102, 102, 102)
LISTADO_botonEditar[i+1].texto.x=105
LISTADO_botonEditar[i+1].texto.y=25*i
LISTADO_botonEditar[i+1].texto.text = “EDITAR”…LISTADO_botonEditar[i+1].ID
LISTADO_MC_hoja_items:insert(LISTADO_botonEditar[i+1].texto)

i=i+1
end
[/code] [import]uid: 121547 topic_id: 30225 reply_id: 330225[/import]

1)Why dont you use already created and tested components like ui and to create buttons using ui.newButton ???
here is refenerence http://developer.coronalabs.com/code/ui-library

  1. In each loop iteration you are rewriting this table and its elements.
    LISTADO_botonEditar={} . Move this before while statement.
  2. You cannot add touch event to any object/table you create. Not every lua table has this method.

BTW, why LISTADO_botonEdita is global ???

  1. You code should look more or less like this (but pls use ui.newButton method instead!!!)

[code]
local i=0
local LISTADO_botonEditar={}

while i<10 do

–BOTON EDITAR

— myButton={}
local myButton = display.newGroup()
function myButton:touch (event)
if event.phase == “began” then
self[1].alpha=0.3 – rectangulo child element,
self[2].alpha=0.3 – texto child element
elseif event.phase == “ended” then
print ("Boton presionado: "… tostring(self.ID)) – to string in case of nil

elseif event.phase == “moved” then
–Nada
end
return true – stops processing touch event
end


–Rectangulo
myButton.ID=i+1

local rectangulo = display.newImageRect(“imagenes/btn_editar.png”, 37,14)
rectangulo.x = 105
rectangulo.y = (25*i)
rectangulo.alpha=1

– LISTADO_MC_hoja_items:insert(myButton.rectangulo)

–Nombre
local texto = display.newText("", 0, 0, “OriyaMN”, 7)
texto:setTextColor(102, 102, 102)
texto.x=105
texto.y=25*i
texto.text = “EDITAR”…myButton.ID
myButton:insert(rectangulo)
myButton:insert(texto)
myButton:addEventListener (“touch”, myButton)
LISTADO_botonEditar[i+1] = myButton
–LISTADO_MC_hoja_items:insert(myButton)

i=i+1
end
[/code] [import]uid: 145499 topic_id: 30225 reply_id: 121135[/import]

Thank you veeeeery much!!! [import]uid: 121547 topic_id: 30225 reply_id: 121353[/import]

1)Why dont you use already created and tested components like ui and to create buttons using ui.newButton ???
here is refenerence http://developer.coronalabs.com/code/ui-library

  1. In each loop iteration you are rewriting this table and its elements.
    LISTADO_botonEditar={} . Move this before while statement.
  2. You cannot add touch event to any object/table you create. Not every lua table has this method.

BTW, why LISTADO_botonEdita is global ???

  1. You code should look more or less like this (but pls use ui.newButton method instead!!!)

[code]
local i=0
local LISTADO_botonEditar={}

while i<10 do

–BOTON EDITAR

— myButton={}
local myButton = display.newGroup()
function myButton:touch (event)
if event.phase == “began” then
self[1].alpha=0.3 – rectangulo child element,
self[2].alpha=0.3 – texto child element
elseif event.phase == “ended” then
print ("Boton presionado: "… tostring(self.ID)) – to string in case of nil

elseif event.phase == “moved” then
–Nada
end
return true – stops processing touch event
end


–Rectangulo
myButton.ID=i+1

local rectangulo = display.newImageRect(“imagenes/btn_editar.png”, 37,14)
rectangulo.x = 105
rectangulo.y = (25*i)
rectangulo.alpha=1

– LISTADO_MC_hoja_items:insert(myButton.rectangulo)

–Nombre
local texto = display.newText("", 0, 0, “OriyaMN”, 7)
texto:setTextColor(102, 102, 102)
texto.x=105
texto.y=25*i
texto.text = “EDITAR”…myButton.ID
myButton:insert(rectangulo)
myButton:insert(texto)
myButton:addEventListener (“touch”, myButton)
LISTADO_botonEditar[i+1] = myButton
–LISTADO_MC_hoja_items:insert(myButton)

i=i+1
end
[/code] [import]uid: 145499 topic_id: 30225 reply_id: 121135[/import]

Thank you veeeeery much!!! [import]uid: 121547 topic_id: 30225 reply_id: 121353[/import]

kcharubin, and… how change to frame 2 in dynamic movieclip?

[code]

local myButton = display.newGroup()

function myButton:touch (event)
if event.phase == “began” then

print ("Boton presionado: "… tostring(self.ID))
self[1].alpha=0.3
botonEstrellas[self.ID].myButton.botonEstrella:stopAtFrame(2) ------ NOT WORK IT!!!
— How change to “botonEstrella:stopAtFrame(2)” when is pressed?

elseif event.phase == “ended” then
self[1].alpha=1

elseif event.phase == “moved” then

end
return true – stops processing touch event
end

myButton.ID=i+1

local botonEstrella = movieclip.newAnim({“imagenes/btn_estrella_off.png”, “imagenes/btn_estrella_on.png”},30,29)
botonEstrella.x = 292
botonEstrella.y = (desfaceVertical*i)+14
botonEstrella.alpha=1
botonEstrella:stopAtFrame(1) – off button
–botonEstrella:stopAtFrame(2) – on button

myButton:insert(botonEstrella)
myButton:addEventListener (“touch”, myButton)
botonEstrellas[i+1] = myButton
Layer_Listado_Contenido:insert(myButton)

[/code] [import]uid: 121547 topic_id: 30225 reply_id: 122755[/import]

any help? please!!! :smiley: [import]uid: 121547 topic_id: 30225 reply_id: 122907[/import]

kcharubin, and… how change to frame 2 in dynamic movieclip?

[code]

local myButton = display.newGroup()

function myButton:touch (event)
if event.phase == “began” then

print ("Boton presionado: "… tostring(self.ID))
self[1].alpha=0.3
botonEstrellas[self.ID].myButton.botonEstrella:stopAtFrame(2) ------ NOT WORK IT!!!
— How change to “botonEstrella:stopAtFrame(2)” when is pressed?

elseif event.phase == “ended” then
self[1].alpha=1

elseif event.phase == “moved” then

end
return true – stops processing touch event
end

myButton.ID=i+1

local botonEstrella = movieclip.newAnim({“imagenes/btn_estrella_off.png”, “imagenes/btn_estrella_on.png”},30,29)
botonEstrella.x = 292
botonEstrella.y = (desfaceVertical*i)+14
botonEstrella.alpha=1
botonEstrella:stopAtFrame(1) – off button
–botonEstrella:stopAtFrame(2) – on button

myButton:insert(botonEstrella)
myButton:addEventListener (“touch”, myButton)
botonEstrellas[i+1] = myButton
Layer_Listado_Contenido:insert(myButton)

[/code] [import]uid: 121547 topic_id: 30225 reply_id: 122755[/import]

any help? please!!! :smiley: [import]uid: 121547 topic_id: 30225 reply_id: 122907[/import]

Peach any Help? please… [import]uid: 121547 topic_id: 30225 reply_id: 123579[/import]

Peach any Help? please… [import]uid: 121547 topic_id: 30225 reply_id: 123579[/import]

@peachpellen any help? pleaseee [import]uid: 121547 topic_id: 30225 reply_id: 125119[/import]

@peachpellen any help? pleaseee [import]uid: 121547 topic_id: 30225 reply_id: 125119[/import]