display group question

I have a display group made up of 1 png and 2 text objects. (A word tile object)

I have a function that create the display group by adding the png and 2 text objects and then returns the display group itself.

[lua]local function display_letter_tile( letter_character, position_x, position_y, size, letter_value)

local tilegroup = display.newGroup()

local c = letter_character
local x = position_x
local y = position_y
local s = size
local v = letter_value

local tile = display.newImageRect(tilegroup,“circle.png”, s, s)
tile.x = x
tile.y = y

local letter = display.newEmbossedText( tilegroup, c, 0, 0, native.systemFontBold, 32)
letter.x = x
letter.y = y + 5
letter:setTextColor(0, 0, 0)
local value = display.newEmbossedText( tilegroup, v , 0 ,0, native.systemFont, 8)
value.x = x - (size/4)
value.y = y - (size/4)
value:setTextColor(0, 0, 0)
tilegroup:setReferencePoint(display.CenterReferencePoint)
return tilegroup

end[/lua]

I’d like to be able to change the text color of one of the objects.

Should I able to change it this way when the display group’s touch function.

[lua]local function on_tile_touch(event)

if event.phase == “began” then

display.getCurrentStage():setFocus(event.target)

local x = event.x
local y = event.y

test = event.target – display group gets passed here
test[2]:setTextColor(255) – assuming that the second child is the text object[/lua]
[import]uid: 9035 topic_id: 33162 reply_id: 333162[/import]

try changing

test[2]:setTextColor(255)

to

test[2]:setTextColor(255, 255, 255)
Not sure if this is the problem, but I think you have to specify all three of RGB parameters.

The rest of the code looks fine to me [import]uid: 130035 topic_id: 33162 reply_id: 131689[/import]

You need to carefully confirm that event.target is, in fact, the display group and not the actual object. It depends what you attached your touch listener to. Use print commands to compare what is being created and what event.target is… if they match, your function should work fine.

Also, the “one parameter” color method is fine… ( 255 ) is the same as ( 255, 255, 255 )

Hope this helps somewhat,
Brent [import]uid: 9747 topic_id: 33162 reply_id: 131699[/import]

add this:

tilegroup.tile = tile  
tilegroup.letter = letter  
tilegroup.value = value  

and you can access all the properties from there (color, x, y or anything you set up) [import]uid: 31508 topic_id: 33162 reply_id: 131731[/import]

I didn’t know you could access display group member by name that way. I’ll give it a try when I have a chance.

[import]uid: 9035 topic_id: 33162 reply_id: 131743[/import]

Hello @ WideAwakeGames,
In truth, you can’t access (point to) the group object “by name” unless you specifically set up that linkage in code.

For example, the following will not allow you to access “ball” by name:

local testGroup = display.newGroup()  
local ball = display.newCircle( testGroup, 150, 700, 30 )  
print( testGroup.ball ) --it will print 'nil'  

This example, however, will allow it because you manually linked up “ball” as a property of the table “testGroup”.

local testGroup = display.newGroup()  
local ball = display.newCircle( testGroup, 150, 700, 30 )  
testGroup.ball = ball  
print( testGroup.ball ) --now it will print the reference of 'ball'  

I don’t particularly recommend this method however, because if you want to remove the “ball” somewhere else during the app, you need to remember to remove its property pointer from the parent group as well. It’s a step that would be easy to forget, especially if you get dozens or hundreds of objects… and if you forget, the object will never be properly cleaned up from memory until (in theory) the display group is also destroyed, in which case all of its properties would be too.

Long story short, I suggest you find a very reliable, easy-to-manage way to identify your objects within a group. :slight_smile:

Brent Sorrentino
[import]uid: 9747 topic_id: 33162 reply_id: 131768[/import]

try changing

test[2]:setTextColor(255)

to

test[2]:setTextColor(255, 255, 255)
Not sure if this is the problem, but I think you have to specify all three of RGB parameters.

The rest of the code looks fine to me [import]uid: 130035 topic_id: 33162 reply_id: 131689[/import]

You need to carefully confirm that event.target is, in fact, the display group and not the actual object. It depends what you attached your touch listener to. Use print commands to compare what is being created and what event.target is… if they match, your function should work fine.

Also, the “one parameter” color method is fine… ( 255 ) is the same as ( 255, 255, 255 )

Hope this helps somewhat,
Brent [import]uid: 9747 topic_id: 33162 reply_id: 131699[/import]

add this:

tilegroup.tile = tile  
tilegroup.letter = letter  
tilegroup.value = value  

and you can access all the properties from there (color, x, y or anything you set up) [import]uid: 31508 topic_id: 33162 reply_id: 131731[/import]

I didn’t know you could access display group member by name that way. I’ll give it a try when I have a chance.

[import]uid: 9035 topic_id: 33162 reply_id: 131743[/import]

@brent thanks for the warning. In this case the groups are tiles in a word game and as such I won’t be removing any of the children object.
I’ve tested this and it works as described. Always nice to learn something new. [import]uid: 9035 topic_id: 33162 reply_id: 131838[/import]

Hello @ WideAwakeGames,
In truth, you can’t access (point to) the group object “by name” unless you specifically set up that linkage in code.

For example, the following will not allow you to access “ball” by name:

local testGroup = display.newGroup()  
local ball = display.newCircle( testGroup, 150, 700, 30 )  
print( testGroup.ball ) --it will print 'nil'  

This example, however, will allow it because you manually linked up “ball” as a property of the table “testGroup”.

local testGroup = display.newGroup()  
local ball = display.newCircle( testGroup, 150, 700, 30 )  
testGroup.ball = ball  
print( testGroup.ball ) --now it will print the reference of 'ball'  

I don’t particularly recommend this method however, because if you want to remove the “ball” somewhere else during the app, you need to remember to remove its property pointer from the parent group as well. It’s a step that would be easy to forget, especially if you get dozens or hundreds of objects… and if you forget, the object will never be properly cleaned up from memory until (in theory) the display group is also destroyed, in which case all of its properties would be too.

Long story short, I suggest you find a very reliable, easy-to-manage way to identify your objects within a group. :slight_smile:

Brent Sorrentino
[import]uid: 9747 topic_id: 33162 reply_id: 131768[/import]

@brent thanks for the warning. In this case the groups are tiles in a word game and as such I won’t be removing any of the children object.
I’ve tested this and it works as described. Always nice to learn something new. [import]uid: 9035 topic_id: 33162 reply_id: 131838[/import]