Screen Transitions

In my game there are two paths or scenarios you can take via screens. Scene 1-3 are the same for both paths. When you get to the next screen its 4 or 4a. How do I select different screens without having to duplicate screens? Meaning screen 3 to screen 4 or 4a. [import]uid: 72372 topic_id: 13808 reply_id: 313808[/import]

[lua]if(condition to get to screen 4) then
code to switch to screen 4
else if(condition to get to screen 4a) then
code to switch to screen 4a
end[/lua]
[import]uid: 5317 topic_id: 13808 reply_id: 50694[/import]

Okay this is the code that triggers the choice on Screen 3. So if this is selected on Screen 3 how do I make that connection on screen 5? Meaning there will not be two buttons to press on Screen 5 just one.

Screen 3 Code for Choice

[lua]local optimistic = display.newImage (“optimistic.png”)
optimistic.x = 100
optimistic.y = 385
optimistic.xScale = .5
optimistic.yScale = .5
localGroup:insert(optimistic)
local function touchedOptimistic (event)
if (“ended” == event.phase) then
director:changeScene (“screen4a”)
media.playEventSound( “click_x.caf” )
end
end

optimistic:addEventListener (“touch”, touchedOptimistic)
–>This places the optimistic button

local skeptical = display.newImage (“skeptical.png”)
skeptical.x = 230
skeptical.y = 385
skeptical.xScale = .5
skeptical.yScale = .5
localGroup:insert(skeptical)

local function touchedSkeptical (event)
if (“ended” == event.phase) then
director:changeScene (“screen4”)
media.playEventSound( “click_x.caf” )
end
end

skeptical:addEventListener (“touch”, touchedSkeptical)
–>This places the skeptical button[/lua]

Screen 5 just has one button. How would I reference the buttons from Screen 3 on Screen 5?

[lua]local brain = display.newImage (“brain.png”)
brain.x = 165
brain.y = 390
brain.xScale = .4
brain.yScale = .4
localGroup:insert(brain)
–>This places the brain

local function touchedbrain (event)
if (“ended” == event.phase) then
director:changeScene (“screen6”)
media.playEventSound( “click_x.caf” )
_G.numberOne = numberFieldOne.text

end
end

brain:addEventListener (“touch”, touchedbrain)[/lua] [import]uid: 72372 topic_id: 13808 reply_id: 50723[/import]

Do you just want to keep track of the button that was pressed to get to screen 5?

so many ways to accomplish this, it is really up to you.

create a table that holds information about the game state…or pass the information along to screen5 as a parameter…or create a property of a displayObject…

It would really depend on what the information was and how I intended to use it that would help determine the method I would choose. [import]uid: 5317 topic_id: 13808 reply_id: 50735[/import]

I’m trying to transition to screens based on the choice made on screen 5. Like I said I have two paths. Some scenes are the same on the path and some are different. The paths intersect at different points during the game but it’s all based on the choice from earlier in the game. [import]uid: 72372 topic_id: 13808 reply_id: 50750[/import]

So you basically want to store the choices the user made to get to the current screen.

My immediate choice would be to create a global table that stored the entire path the user traversed. There will be others that will tell you to not use a global variable, but in this case, I think it is the easiest and most direct way to get what you need. If there are others who would like to outline a method of passing tables around between screens, I would like to see them, as well.

Here is some pseudo code to get you started.

First in your main.lua I would create a global variable as a table like so:
[lua] userPath = {};[/lua]
notice the lack of the local keyword, making the table global in scope.

Now, after every choice the user makes, set the appropriate value in the table, like this:
[lua]if(condition to get to screen 4) then
userPath[#userPath + 1] = value to store
code to switch to screen 4
else if(condition to get to screen 4a) then
userPath[#userPath + 1] = value to store
code to switch to screen 4a
end[/lua]
For this to work as I envision, on every screen, before you transition to the new screen, you need to store the appropriate information. This will mean, that for any screen you are on, you can get the previous screens information by accessing [lua]userPath[#userPath][/lua] That will give you the last piece of information you entered. If you want to go two screens back, use [lua]userPath[#userPath - 1][/lua]

Does that make sense?

The value you store can be as simple as a number, or as complex as another table.

For a more in depth explanation of tables in Corona see here: http://blog.anscamobile.com/2011/06/understanding-lua-tables-in-corona-sdk/ [import]uid: 5317 topic_id: 13808 reply_id: 50823[/import]