Preferences screen sample code

Is there a forum post or sample code dealing with how to setup a preferences screen that can be swapped in/out with the main screen of an app/ And how to save/load the preference settings?

I searched the forum but could find nothing. [import]uid: 295 topic_id: 1448 reply_id: 301448[/import]

I fear you will not find a “shortcut” to those functionality …

It may be enough for you to know that you do this best using groups (display.newGroup()) for every thing you call “screen”. You may then swap (using transition.to()) them easily.

Loading and saving preferences can be done in different ways. You may look up one in my example code for the playlist here in the forum. There are other json based examples around here to. [import]uid: 6928 topic_id: 1448 reply_id: 4044[/import]

Well I found out how to do what I want with groups and transition.to(). I made 1 group for the main screen ( playfieldScreenGroup ), and 1 group for the preferences screen ( settingsScreenGroup ).

To hide the main (current) screen I do:
transition.to(playfieldScreenGroup, {time=1500, alpha=0})

Then to display the preferences screen I do:

display.getCurrentStage():insert(settingsScreenGroup),
transition.to(settingsScreenGroup, {time=1500, alpha=1.0})

I just reverse the process to redisplay the main screen ( playfieldScreenGroup )

Both screens will always be there so I’m wondering (to prevent the CurrentStage group from growing with numerous insertions of the same two screens) if it is necessary to also remove the opposite screen before insertion of the screen that will be displayed, as in this code (notice the commented out line):

 transition.to(playfieldScreenGroup, {time=1500, alpha=0})  
-- display.getCurrentStage():remove(playfieldScreenGroup)  
 display.getCurrentStage():insert(settingsScreenGroup)  
 transition.to(settingsScreenGroup, {time=1500, alpha=1.0})  
 currentScreen = 2  

[import]uid: 295 topic_id: 1448 reply_id: 4101[/import]

You do not need to fear about “growing”…

“Insert” is a bad term for what happens! Actually an insert “moves” the object to a new place!

As every object is created inside the stage … and from then on only “moved” to another place it would be a much better term to name it “move” … I dunno why Ansca named it insert. Maybe because you “insert” stuff in tables… but there you really “insert” it and create a duplicated reference to the original table.

So… If you “insert” something into the same group where it already is, you move it to the end of that group which is the “top” of the display hierarchy (stuff is rendered from bottom to top).

Try this:

local dsp=display.newGroup()  
local g1=display.newGroup()  
local g2=display.newGroup()  
  
print("g1: "..tostring(g1))  
print("g2: "..tostring(g2))  
  
dsp:insert(g1)  
dsp:insert(g2)  
  
print("idx 1: "..tostring(dsp[1]))  
print("idx 2: "..tostring(dsp[2]))  
  
dsp:insert(g1)  
  
print("idx 1: "..tostring(dsp[1]))  
print("idx 2: "..tostring(dsp[2]))  

which prints:

[code]
g1: table: 0x2928f0
g2: table: 0x292eb0

idx 1: table: 0x2928f0 idx 2: table: 0x292eb0
– here insert g1 is called again

idx 1: table: 0x292eb0 idx 2: table: 0x2928f0 [/code]

I think Ansca should deprecate the term “insert” for display objects. I saw it multiple times that users are confused about that! [import]uid: 6928 topic_id: 1448 reply_id: 4118[/import]

Thanks again. That makes it clear. [import]uid: 295 topic_id: 1448 reply_id: 4142[/import]

@OderWat,

I think the term “insert” does describe what you’re doing with the objects and using the term “move” would be more confusing.

For example: If I want to buy a candy bar from a vending machine, I take the change out of my pocket and “insert” it in the coin slot. The coins were already in my pocket before inserted into the machine. When Corona objects are first created, they exist on the “Stage” (i.e. my pocket). You then insert them into groups (i.e. coin slot of the vending machine).

Your point about having a duplicate reference to an object may only be true while the chunk is active if it was defined as Local.

I think the current Corona documentation could do a better job of describing what is happening to avoid the confusion. I also think the term “stage,” may be confusing for non-flash programmers.

Just my two-cents. [import]uid: 7559 topic_id: 1448 reply_id: 4143[/import]

I am still sure that having another term which does not imply duplication of the “table.insert()” behavior could help new users.

And my point about having a duplicate reference to an object is always true!?

When you use Lua table.insert() the object does not get removed() somewhere else…

local txt='Hello'  
local t1={}  
local t2={}  
  
table.insert(t1,txt)  
table.insert(t2,txt)  
  
print(t1[1]) -- prints Hello  
print(t2[1]) -- prints Hello  
local txt=display.newText('Hello',0,0)  
local g1=display.newGroup()  
local g2=display.newGroup()  
  
g1:insert(txt)  
g2:insert(txt)  
  
print(g1[1]) -- prints nil  
print(g2[1]) -- prints textObject  

I KNOW that those are different methods … and I think I know what happens and I can distinguish those stuff.

I am talking about new users and the general concept of having unambiguous names for functions with different behavior… thats all :slight_smile:

BTW: Changing the documentation about that is a good thing in any case! [import]uid: 6928 topic_id: 1448 reply_id: 4151[/import]

Yes, there is a different between table “insert” and group “insert”, which can be confusing to new users.

We will look at the documentation and try to make it more understandable.

Thanks for the input.
[import]uid: 7559 topic_id: 1448 reply_id: 4156[/import]