question about display object naming (naming a table?)

I have a display object called with

local background = display.newImage("ccbg.png")

Later in my code, I want to reference this object with something along the line of

if activeChar.target == background then

I noticed this does not work, corona wont recognize the name “background”

when I use this to find out what exactly is happening 

print(activeChar.target)

This is what is printed in the console “table: 0x7fd5034d6d60”

I remember reading that display objects are basically table, but I was wondering if it was possible to name it instead of having that random sting of characters for the name

This code works:

local background = display.newImage( "Icon.png", 100, 100) local background\_copy = background print(" \*\*\* Table Names \*\*\* ") print(background, background\_copy) print() print(" \*\*\* Conditional \*\*\* ") print(background == background\_copy) local function onClick( event ) local target = event.target if event.phase == "began" then print(" \*\*\* target == background == background\_copy \*\*\* ") print( (( target == background ) and ( target == background\_copy )) ) -- All three of these works and moves the same display object --target.x = 200 background.x = 200 --background\_copy.x = 200 end end background\_copy:addEventListener( "touch", onClick )

I would assume that activeChar.target is not the same as background. You mention that Corona doesn’t recognize the variable background, could you explain that a little further?

If the objects does exists print them out, like I did in my example, and make sure that the ID’s are the same for both activeChar.target and background.

Best regards,

Tomas

“You mention that Corona doesn’t recognize the variable background, could you explain that a little further?”

all I mean by this is that it wont run the block of code within that conditional function. I assume its because the name of target is “table: 0x7fd5034d6d60” and not background. 

I’ll look at your stuff and see if I can get it to work.

Hi Ethan,

Yes, you can name any object - you can actually add any properties to anything - so you can add a .name property to the background after you create it;

local background = display.newImage("ccbg.png") background.name = "background"

And then you can check if the .name of the object which triggered the event is the same as the .name property for the background;

if activeChar.target.name == "background" then

Cheers,

Simon Murphy

Dixon Court Entertainment

The variable background contains a pointer to a display object (which is a lua table). It will actually contain a number like:  0x93AB013D. Depending on the rest of your logic, it’s possible that activeChar.target, which is likely also a display object (and another pointer) could point to the same memory address, which would cause your condition to be true. If they should be true and you’re getting an error that you can’t see background, you could have a “scope” issue.

But the other advice about giving your objects a .name variable would address your need to give it a text name.

Rob

This code works:

local background = display.newImage( "Icon.png", 100, 100) local background\_copy = background print(" \*\*\* Table Names \*\*\* ") print(background, background\_copy) print() print(" \*\*\* Conditional \*\*\* ") print(background == background\_copy) local function onClick( event ) local target = event.target if event.phase == "began" then print(" \*\*\* target == background == background\_copy \*\*\* ") print( (( target == background ) and ( target == background\_copy )) ) -- All three of these works and moves the same display object --target.x = 200 background.x = 200 --background\_copy.x = 200 end end background\_copy:addEventListener( "touch", onClick )

I would assume that activeChar.target is not the same as background. You mention that Corona doesn’t recognize the variable background, could you explain that a little further?

If the objects does exists print them out, like I did in my example, and make sure that the ID’s are the same for both activeChar.target and background.

Best regards,

Tomas

“You mention that Corona doesn’t recognize the variable background, could you explain that a little further?”

all I mean by this is that it wont run the block of code within that conditional function. I assume its because the name of target is “table: 0x7fd5034d6d60” and not background. 

I’ll look at your stuff and see if I can get it to work.

Hi Ethan,

Yes, you can name any object - you can actually add any properties to anything - so you can add a .name property to the background after you create it;

local background = display.newImage("ccbg.png") background.name = "background"

And then you can check if the .name of the object which triggered the event is the same as the .name property for the background;

if activeChar.target.name == "background" then

Cheers,

Simon Murphy

Dixon Court Entertainment

The variable background contains a pointer to a display object (which is a lua table). It will actually contain a number like:  0x93AB013D. Depending on the rest of your logic, it’s possible that activeChar.target, which is likely also a display object (and another pointer) could point to the same memory address, which would cause your condition to be true. If they should be true and you’re getting an error that you can’t see background, you could have a “scope” issue.

But the other advice about giving your objects a .name variable would address your need to give it a text name.

Rob