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

dude it will work have a look at this

1
2
3
4
5
6
7
8
9
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

Darn it.. you're right! Thanks =) The order of where the method gets defined is important. sigh..

welcome

:)

views:1530 update:2011/11/25 8:45:21
corona forums © 2003-2011