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)
In each loop iteration you are rewriting this table and its elements.
LISTADO_botonEditar={} . Move this before while statement.
You cannot add touch event to any object/table you create. Not every lua table has this method.
BTW, why LISTADO_botonEdita is global ???
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
In each loop iteration you are rewriting this table and its elements.
LISTADO_botonEditar={} . Move this before while statement.
You cannot add touch event to any object/table you create. Not every lua table has this method.
BTW, why LISTADO_botonEdita is global ???
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
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
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