Reusing objects and EventListeners in multiple displayGroups

I’m working on an eBook, presently implementing each page as a separate displayGroup(). Two issues I’m encountering:

  1. I want to share some display objects on multiple pages - but sometimes they refuse to appear on some pages. I can insert, say one image object into the display group for several pages, but sometimes it will be missing on some pages. Things ‘compile’ and run fine, but some objects, known to be inserted are just absent on some pages. It’s not universal, seems a little random 'where’the absences are – but whatever is missing or visible on launch stays that way as other things go on at runtime

  2. I insert the same two display object hotspots (let’s say ‘back’ and ‘next’) on each page - each one with an eventListener that triggers the flip. each one This works in some cases - but seems ‘absent’ here and there, too. No crashes or runtime complaints - just ‘dead spots’

I can post code to give more detail if necessary - but I wonder at the global level for now … is it OK to have one eventListener attached to a display object that is inserted in multiple displayGroups? I have all the code I need to sort out ‘who called’ and make everything else work fine, but there seems to be a weak spot in my approach with inserting copies across groups.
Thanks for any tips…
[import]uid: 34130 topic_id: 7442 reply_id: 307442[/import]

is it OK to have one eventListener attached to a display object that is inserted in multiple displayGroups?

Yes, this is fine. Can you share some code? It’s not clear what the problem may be.

Tim [import]uid: 8196 topic_id: 7442 reply_id: 26362[/import]

Here’s a simplified piece of code.
What happens with this is – page2 shows up without any ‘back’ or ‘next’ markers. The logic with the eventListeners and the page-changing seems to be OK – but I’m a bit stuck on what’s going on.
Thanks for any help.

–[[
testing a strategy for multiple pages as displayGroups

  • sets up 3 simple page backgrounds, each with
    the same ‘back’ and ‘ahead’ text markers that
    have event Listeners to trigger code that transitions to
    the prior or next page in the sequence.

–]]

local page1 = display.newImage(“page1.png”)
local page2 = display.newImage(“page2.png”)
local page3 = display.newImage(“page3.png”)
local ahead = display.newText(“Ahead”,200,400,“Arial”,30)
local back = display.newText(“Back”,0,400,“Arial”,30)

pageCount =3

– setup the list data structure
– so the eventListeners can readily figure out which page is ‘next’ and ‘prior’

list = {}

list[1] = display.newGroup()
list[1].prior = nil

for i = 2 , pageCount do

list[i] = display.newGroup()
list[i].prior = list[i-1]

end

for i=1,pageCount-1 do
list[i].next = list[i+1]
end

list[pageCount].next = nil

– populate the display Groups

list[1]:insert(page1)
list[1]:insert(ahead)
list[1]:insert(back)

list[2]:insert(page2)
list[2]:insert(ahead)
list[2]:insert(back)

list[3]:insert(page3)
list[3]:insert(ahead)
list[3]:insert(back)
local function priorPage(event)
if event.target.parent.prior == nil then
print(“Start of list”)
else
transition.dissolve(event.target.parent,event.target.parent.prior,10)
end
end

local function nextPage(event)
if event.target.parent.next == nil then
print(“End of list”)
else
transition.dissolve(event.target.parent,event.target.parent.next,10)
end

end

ahead:addEventListener(“touch”,nextPage)
back:addEventListener(“touch”,priorPage) [import]uid: 34130 topic_id: 7442 reply_id: 26389[/import]

I think I’m beginning to see that the problem is connected with the way I’m assigning values the .prior and .next elements in the table. Of course Lua lets me do that… but in the documentation for next() I see that it can create unpredictable behavior.

I can declare those fields, but it’d be even cleaner if I didn’t need to have them. They exist so I can tell, from a touch listener function, which display group is prior and next in the list. Looks like the built-in next() could give me the forward link, but I can’t see how to get the equivalent of ‘prior’ without doing it myself in the data structure.

This is all in the name of starting to learn Lua for me – so I may be missing something that’s obvious to others.

thanks
[import]uid: 34130 topic_id: 7442 reply_id: 26585[/import]

when you inserted “ahead” and “back” into list[2] it took it out of list[1], then you inserted it into list[3] taking it out of list[2]. corona does not duplicate images.

you should insert the buttons into the relevant page *when* you change page
[import]uid: 6645 topic_id: 7442 reply_id: 26652[/import]