Accessing display objects by ID

Hi all,

New here so go easy :slight_smile:

I have the following code in my app that creates a list of artists and on each list item is a button. The button is either “Add” or “Remove” depending on whether the user has previously added them. onRelease I add or remove the item. What I’d then like to do is toggle the button, so the “Add” becomes “Remove” and vice versa. I’m just not sure how I access display objects by ID. If I just try removing “addToScheduleBtn” and creating a new “removeFromBtn” it only effects the last button on the list.

Any help is appreciated.

[lua]atozArtistList = tableView5.newList{
data=atozArtistData,
default=“listItemBg.png”,
over=“listItemBg_over.png”,
onRelease=atozArtistListButtonRelease,
top=topBoundary,
bottom=bottomBoundary,
callback=function(row)

local g = display.newGroup()

local title = display.newText( row.title, 0, 0, “Helvetica”, 16 )
title:setTextColor(0,0,0)
g:insert(title)
title.x = title.width*0.5 + 12
title.y = 28

local subtitle = display.newText( row.date … " - "…row.time, 0, 0, “Helvetica”, 14 )
subtitle:setTextColor(60,60,60)
g:insert(subtitle)
subtitle.x = subtitle.width*0.5 + 12
subtitle.y = title.y + title.height + 5

local subtitle2 = display.newText( row.venue_name, 0, 0, “Helvetica”, 14 )
subtitle2:setTextColor(60,60,60)
g:insert(subtitle2)
subtitle2.x = subtitle2.width*0.5 + 12
subtitle2.y = title.y + title.height + 25
print(row.scheduled)
if(row.scheduled == nil) then

addToScheduleBtn = ui.newButton{
default = “addButton.png”,
over = “addButton_over.png”,
onRelease =
function()
addToSchedule(row.id)
end,
id = “add”…row.id
}

addToScheduleBtn.x = display.contentWidth - 45
addToScheduleBtn.y = 50
addToScheduleBtn.alpha = 1

g:insert(addToScheduleBtn)

atozButtons[row.id] = addToScheduleBtn

else
removeFromScheduleBtn = ui.newButton{
default = “removeButton.png”,
over = “removeButton_over.png”,
onRelease =
function()
removeFromSchedule(row.id)
atozButtons[row.id] = nil
self:remove = nil
end
}

removeFromScheduleBtn.x = display.contentWidth - 45
removeFromScheduleBtn.y = 50
removeFromScheduleBtn.alpha = 1

g:insert(removeFromScheduleBtn)

atozButtons[row.id] = removeFromScheduleBtn

end

return g

end,
x=0
}

atozScreen:insert(atozArtistList)[/lua] [import]uid: 69057 topic_id: 11186 reply_id: 311186[/import]

I’m not sure what exactly you’re asking for, but I think you may want to use a technique like the one I demonstrate here to stick objects in a table:
http://developer.anscamobile.com/code/tables-lua-dictionaries-and-arrays

In my example I was using the objects as the key to refer to themselves, but you could use the button as the key to refer to a different display object. Something like this pseudo-code:

thingies = {} function onButton(button) thingies[button] = display.newImage() end [import]uid: 12108 topic_id: 11186 reply_id: 40591[/import]

Hi jhocking,

That looks useful. I think I’m just misunderstanding how display objects are manipulated in Lua. Looking at your example, does it mean that when you change the contents of a particular element in the associative array it will also change the display object?

Thanks for taking the time to reply. [import]uid: 69057 topic_id: 11186 reply_id: 40592[/import]

Just thought I’d update this. Basically I was misunderstanding how display objects are defined and manipulated in Lua. I was thinking along the JavaScript line that objects could be created, manipulated and assigned to a variable until needed. [import]uid: 69057 topic_id: 11186 reply_id: 41030[/import]