adding a listener to text displayObject

How can I access a text object contained in a group by name and later manipulate x,y position? 

Seems like a basic question but I must be missing something obvious and easy.

I started by creating this function in a class. pGroup is the group that the text object will reside in. pText is the actual text. pName is the reference name of the text object. The other parameters are obvious.

[lua]

insertText = function(pGroup,pText,pName,pFont,pFontSize,pX,pY,pWidth,pColor,pRef)

    local pText     = display.newText( pGroup, pText, pX, pY, pFont, pFontSize )

    pText.name      = pName

    pText:setReferencePoint(pRef)

    pText:setTextColor(pColor[1], pColor[2], pColor[3])

    pGroup:insert(pText)

   return pGroup  

   

end

– END CODE -------------------------------------------------------------------------------------------

I called the function in main.lua like this:

– CODE --------------------------------------------------------------------------------------------------

    gCardGroup = uiObj.insertText(gCardGroup,gRecord.title,“title”,“Papyrus”,16,

                                  (display.contentWidth*.5),82,31,{0, 0, 0},display.CenterReferencePoint)

– END CODE -------------------------------------------------------------------------------------------

[/lua]

gCardGroup is the group object

This all works so far but I want to move the text object named “title” by adding a listener. Basically, a scrolling text field in the object. How can I reference the specific text object to alter its y position. What does not appear to work 

gCardGroup[“title”].x

gCardGroup.title.x

Any help would be GREATLY appreciated.

Hi @junkmail81,

Objects within a display group aren’t inherently “named”, so you’ll have to look up its reference another way. One method is to loop through the children of the display group and determine which one is the “title” object. Then, you can assign that internal ID to a variable and manipulate it however you need. Like this:

[lua]

local myGroup = display.newGroup()

local textObject1 = display.newText( myGroup, “Title”, 50, 50, “Arial”, 60 )

textObject1.name = “title”

local textObject2 = display.newText( myGroup, “Subtitle”, 50, 150, “Arial”, 60 )

textObject2.name = “subtitle”

local titleObject

for i = 1,myGroup.numChildren do

    if ( myGroup[i].name == “title” ) then

        titleObject = myGroup[i]

    end

end

titleObject.y = 400

[/lua]

Hope this helps,

Brent Sorrentino

junmail, can you replace line 4 with something like the following, and then access the object?:

[lua]

   pGroup.pName = pText

[/lua]

I already have:  pText.name      = pName in the function. Can I not use “name” for groups?

That was my work around… I guess I was hoping that I could reference an object by attribute value; foregoing the for loop. Thank you though for you assistance.  

@junkmail… Another syntax that might work to replace line 4 is:

[lua]

insertText = function(pGroup,pText,pName,pFont,pFontSize,pX,pY,pWidth,pColor,pRef)

    local pText     = display.newText( pGroup, pText, pX, pY, pFont, pFontSize )

    pGroup[pName] = {}

    pGroup[pName] = pText

–     pText.name      = pName   – Not accessible this way, no way to gt to it

    pText:setReferencePoint(pRef)

    pText:setTextColor(pColor[1], pColor[2], pColor[3])

    pGroup:insert(pText)

   return pGroup  

   

end

– END CODE -------------------------------------------------------------------------------------------

–MAIN CODE --------------------------------------------------------------------------------------------------

    gCardGroup = uiObj.insertText(gCardGroup,gRecord.title,“title”,“Papyrus”,16,

                                  (display.contentWidth*.5),82,31,{0, 0, 0},display.CenterReferencePoint)

    gCardGroup[“title”].x = 100    – Access the object by its “pName” parameter now

– END CODE -------------------------------------------------------------------------------------------

[/lua]

My replacement  line 4 stuff should allow your access code later to work, using the original group and pName as the index ( gCardGroup[“title”].x in this case)

There’s probably ten ways to write the syntax, but it’s the same idea, store the pointer to the text as an element in the group…

This is EXACTLY what I was looking for. Thank you!

Hi @junkmail81,

Objects within a display group aren’t inherently “named”, so you’ll have to look up its reference another way. One method is to loop through the children of the display group and determine which one is the “title” object. Then, you can assign that internal ID to a variable and manipulate it however you need. Like this:

[lua]

local myGroup = display.newGroup()

local textObject1 = display.newText( myGroup, “Title”, 50, 50, “Arial”, 60 )

textObject1.name = “title”

local textObject2 = display.newText( myGroup, “Subtitle”, 50, 150, “Arial”, 60 )

textObject2.name = “subtitle”

local titleObject

for i = 1,myGroup.numChildren do

    if ( myGroup[i].name == “title” ) then

        titleObject = myGroup[i]

    end

end

titleObject.y = 400

[/lua]

Hope this helps,

Brent Sorrentino

junmail, can you replace line 4 with something like the following, and then access the object?:

[lua]

   pGroup.pName = pText

[/lua]

I already have:  pText.name      = pName in the function. Can I not use “name” for groups?

That was my work around… I guess I was hoping that I could reference an object by attribute value; foregoing the for loop. Thank you though for you assistance.  

@junkmail… Another syntax that might work to replace line 4 is:

[lua]

insertText = function(pGroup,pText,pName,pFont,pFontSize,pX,pY,pWidth,pColor,pRef)

    local pText     = display.newText( pGroup, pText, pX, pY, pFont, pFontSize )

    pGroup[pName] = {}

    pGroup[pName] = pText

–     pText.name      = pName   – Not accessible this way, no way to gt to it

    pText:setReferencePoint(pRef)

    pText:setTextColor(pColor[1], pColor[2], pColor[3])

    pGroup:insert(pText)

   return pGroup  

   

end

– END CODE -------------------------------------------------------------------------------------------

–MAIN CODE --------------------------------------------------------------------------------------------------

    gCardGroup = uiObj.insertText(gCardGroup,gRecord.title,“title”,“Papyrus”,16,

                                  (display.contentWidth*.5),82,31,{0, 0, 0},display.CenterReferencePoint)

    gCardGroup[“title”].x = 100    – Access the object by its “pName” parameter now

– END CODE -------------------------------------------------------------------------------------------

[/lua]

My replacement  line 4 stuff should allow your access code later to work, using the original group and pName as the index ( gCardGroup[“title”].x in this case)

There’s probably ten ways to write the syntax, but it’s the same idea, store the pointer to the text as an element in the group…

This is EXACTLY what I was looking for. Thank you!