adding event listeners to texts in an array

Hi All,

I want to enable clicking on some texts and have them change color once they’re touched. I also want to keep the text display in an array. Can you see what’s wrong with this? I’m having a hard time figuring why this will not work. When I’m not using the array, I’m able to add the event and trigger the color change. I’m also open to other ideas.

local wordsText = {}
for i = 1, 3, 1 do
wordsText[i] = display.newText( “hi”, 0, i * 100, “Helvetica”, 40 )
wordsText[i]:setTextColor(255, 0, 255)
wordsText[i]:addEventListener(“touch”, changeColor)
end
This is the stack trace that I’m getting when I run the above code:

Runtime error: assertion failed!
stack traceback:
[C]: ?
[C]: in function ‘assert’
?: in function ‘getOrCreateTable’
?: in function ‘addEventListener’
Thanks in advance!!

Jen
[import]uid: 84674 topic_id: 18070 reply_id: 318070[/import]

dude it will work have a look at this

[lua]local function changeColor(e)
e.target:setTextColor(255,0,0)
end
wordsText = {}
for i = 1, 3 do
wordsText[i] = display.newText( “hi”, 100, i * 100, “Helvetica”, 40 )
wordsText[i]:setTextColor(255, 0, 255)
wordsText[i]:addEventListener(“touch”, changeColor)
end[/lua]

:slight_smile: [import]uid: 12482 topic_id: 18070 reply_id: 69057[/import]

Darn it… you’re right! Thanks =) The order of where the method gets defined is important. sigh… [import]uid: 84674 topic_id: 18070 reply_id: 69281[/import]

welcome
:slight_smile: [import]uid: 12482 topic_id: 18070 reply_id: 69295[/import]