nil value from a table value but not sure why

Hi,

In a function I run this code:

    for i = 1, #word do

        letters[i] = word:sub(i,i)

        – letterTable[letters[i]]

        local letterImage = display.newImage("/images/letters/a.png", 30 * i, 380)

        letterImage.param1 = “A”

        letterImage:addEventListener(“tap”, touchLetter)

        lettersGroup:insert(letterImage)

        print(letterImage.param1)

    end

I create an event listener for each image that is produces on screen then I want to able, from touchLetter() to find that letter (in this case always “A”) but I only get a nil value:

function touchLetter(event)

    print(event)

    print(event.letter)

    print(event.other)

    print(event.other.param1)

end

I want event.other.letter to print out “A” but I only get two nil values (

Runtime error

…ecito\documents\corona projects\languagepic\main.lua:98: attempt to index fie

ld ‘other’ (a nil value)

stack traceback:

        [C]: ?

        …ecito\documents\corona projects\languagepic\main.lua:98: in function

<…ecito\documents\corona projects\la

Can someone please help me understand why?

Thanks in advance,

Best regards,

Tomas

Note: The subject say table but I changed it so it is an image instead. It didn’t work with an table either.

Hi Tomas,

You’re getting that error because ‘other’ is, well, nil.  I don’t see where in your code you define ‘other’ to be anything.  What is it you want ‘other’ to even mean?

(Note: If you’re looking at sample code, you might see event.other used in collision events in the physics library.  But that’s the only place I’ve seen it.  Are you using physics?)

  • Andrew

Ahh, ok! That’s the thing, in another project I used physics so in this project I though I could use event.other.param1 again just as I did. Thanks for explaining that to me.

But how do I do then if I have an image (names letterImage) and I set letterImage.param1 = “A” how do I get that value from the event?

I print event.param1 and I get a nil value back.

Best regards

Tomas

If I do this it works:

        letterImage:addEventListener(“tap”, function(event)

            print(letterImage.letter)

            end)

For tap events, I think the syntax you want to access the fields is this

[lua]

function touchLetter(event)

    print(event)

    print(event.target.letter)

    print(event.target.other)

    print(event.target.other.param1)

end

[/lua]

Note: The subject say table but I changed it so it is an image instead. It didn’t work with an table either.

Hi Tomas,

You’re getting that error because ‘other’ is, well, nil.  I don’t see where in your code you define ‘other’ to be anything.  What is it you want ‘other’ to even mean?

(Note: If you’re looking at sample code, you might see event.other used in collision events in the physics library.  But that’s the only place I’ve seen it.  Are you using physics?)

  • Andrew

Ahh, ok! That’s the thing, in another project I used physics so in this project I though I could use event.other.param1 again just as I did. Thanks for explaining that to me.

But how do I do then if I have an image (names letterImage) and I set letterImage.param1 = “A” how do I get that value from the event?

I print event.param1 and I get a nil value back.

Best regards

Tomas

If I do this it works:

        letterImage:addEventListener(“tap”, function(event)

            print(letterImage.letter)

            end)

For tap events, I think the syntax you want to access the fields is this

[lua]

function touchLetter(event)

    print(event)

    print(event.target.letter)

    print(event.target.other)

    print(event.target.other.param1)

end

[/lua]