How to change scenes

How to go from the main.lua to another file, like game.lua

I want to write a book. I can do a lot of things in the main.lu file

now I want to go somewhere else, to a scene 2 or page 2 of the book for that matter.

How do I do that?

Victor

---------------------------------------------TIME FOR ME TO GIVE BACK A LITTLE-----------------------------------

About one month ago I post this and I did not know how…thanks to the corona community, to Rob, Saerothir, mourdos,ksan, Brent and many more now I know how to go to other scene, and I will tell you how it works for me in a very simple way. In a way I would have wished someone would have told me the first time I ask about a month ago,

and instead of taking me a month to learn something, it would have taken me no more than 10 minutes.

If you are reading this the first time, here is the answer. It works for me, I hope it works for you


STEP – 1

in the main.lua file you need this

display.setStatusBar( display.HiddenStatusBar )                 – hide the status bar
local storyboard = require “storyboard”                               – you need to have this storyboard thing
storyboard.gotoScene( “working2” )                                    – Here you are telling the program to go to the scene

                                                                                                 you want to go, or the page, or the view. Here I want

                                                                                                 to go to a scene called “working2”

that’s it 3 lines of code in the main.lua. If you want to go to a scene call “level1” you put “level1” inside the parenthesis, like this: – storyboard.gotoScene( “level1” )

and the program will go to level1.

It works like a link on a web-site. Now remember that you have to have in a folder on your desktop the two files, main.lua and level1.lua, if you don’t have that file, the program will not run.


STEP – 2

Now, when the program goes to (“working2”) then it will create the “first page” of your book, or the first scene, or the first view, it’s like when you turn on the TV is the first thing you see.

To do this it needs this code

local storyboard = require( “storyboard” )                – it needs the soryboard all the time
local scene = storyboard.newScene()                      – and it needs this line to create a new scene or new page

now, you need to have 4 functions in here.

1 to create the scene

2 to enter the scene

3 to exit the scene

4 to destroy the scene

each function is like this:

– --==**************************[CREATE SCENE]**********************************+±- –

function scene:createScene( event )
    local group = self.view
    
    local background = display.newImage( “backgroundLevel1Q4.png” )
    
    group:insert ( background )
    
end

– --==***************************[ENTER SCENE]**********************************+±- –

function scene:enterScene( event )
    local group = self.view
    
end

– --==***************************[EXIT SCENE]**********************************+±- –

function scene:exitScene( event )
    local group = self.view
    
end

– --==**************************[DESTROY SCENE]*********************************+±- –

function scene:destroyScene( event )
    local group = self.view    
        
end


STEP – 3

In the createScene you need to place the background image, the buttons, or things that you wnat to see in your page, or view. If you have a button or a sprite or something, you add this in here. I just add a background so we can see something. I use this line, and the background image is there.

local background = display.newImage( “backgroundLevel1Q4.png” )

Now I have to insert that image into a group, so when I go to another scene the group, and everything inside will hide from the view. It’s like an eraser.

– so I created a group like this –

local group = self.view

– and then I inserted the background in the group like this –

group:insert ( background )

– you do all this in the createScene.


STEP – 4

Now you need to add the event listeners, like this –

scene:addEventListener( “createScene”, scene )
scene:addEventListener( “enterScene”, scene )
scene:addEventListener( “exitScene”, scene )
scene:addEventListener( “destroyScene”, scene )

– you add one for each scene, and at the end of the program you type this –

return scene

------------------------------------------THAT’S IT--------------------------------------------

Here’s all the code you need in a very simple way.

--------------------------------------main.lua-------------------3 lines of code------------------

display.setStatusBar( display.HiddenStatusBar )
local storyboard = require “storyboard”
storyboard.gotoScene( “working2” )

-------------------------------------------------working2-------------------21 lines of code------------------

local storyboard = require( “storyboard” )
local scene = storyboard.newScene()
function scene:createScene( event )
    local group = self.view
    local background = display.newImage( “backgroundLevel1Q4.png” )    
    group:insert ( background )    
end
function scene:enterScene( event )
    local group = self.view    
end
function scene:exitScene( event )
    local group = self.view    
end
function scene:destroyScene( event )
    local group = self.view            
end
scene:addEventListener( “createScene”, scene )
scene:addEventListener( “enterScene”, scene )
scene:addEventListener( “exitScene”, scene )
scene:addEventListener( “destroyScene”, scene )
return scene

------------------------------------------------------------------------------------END----------------------------

Now that you are in the new scene, actually is the first one that you see, you want to go to another page, or another view right?, to do that you need to have a “button” to touch that button and it will take you to another view, or will change the page or the channel on your TV.

First you have to have a widget to create a button – Remember there are many ways to do the same thing, I’m telling you how I do it, I don’t know if it’s the “Correct” way, but it works.

you need 1 line of code to “require” a widget – local widget = require “widget” – you have to add this right after the line – local storyboard = require( “storyboard” ) –

So like this

local storyboard = require( “storyboard” )
local widget = require “widget”   -------------------this is the new line to create the button.
local scene = storyboard.newScene()


STEP – 5 – Now create the button. this is the code you need --------------6 lines--------------

buttonHome = widget.newButton{
        defaultFile=“buttonHome.png”,
        onRelease = buttonHome
    }
    buttonHome.x = 522
    buttonHome.y = 655

– now you have to put this code inside the “createScene” function. Right after this line of code

local background = display.newImage( “backgroundLevel1Q4.png” )
    buttonHome = widget.newButton{
        defaultFile=“buttonHome.png”,                                             --------here I just have an image for the button
        onRelease = returnHome                                                       --------and here is when they release the finger
    }                                                                                                             it will perform the function “returnHome”
    buttonHome.x = 522
    buttonHome.y = 655
    group:insert ( background ) 

    group:insert ( buttonHome )                                                       ---------- here I’m inserting the button in the group

– and you have to insert the new button in the group also.

– when people release the finger it will run the program of a function called “returnHome”

You need to have the function at the beginning of the program, so the function is there first, so you write the function like this:

local function returnHome()                                                               ------- this is the name of the function
        storyboard.gotoScene( “level1Q3”, “crossFade”, 1000 )      -----Here you’re telling the storyboard to go to

                                                                                                             level1Q3.lua file, to go there it will make a

                                                                                                             special effect like “crossFade” and it will

                                                                                                             take 1 second to go to the new scene
    return true
end

---------------------------------------------------------------------------This code looks like this

local storyboard = require( “storyboard” )

local widget = require “widget”
local scene = storyboard.newScene()
local function returnHome()
        storyboard.gotoScene( “level1Q3”, “crossFade”, 1000 )    
    return true
end
function scene:createScene( event )
    local group = self.view
    local background = display.newImage( “backgroundLevel1Q4.png” )
    buttonHome = widget.newButton{
        defaultFile=“buttonHome.png”,
        onRelease = returnHome
    }
    buttonHome.x = 522
    buttonHome.y = 655
    group:insert ( background )
    group:insert ( buttonHome )
end
function scene:enterScene( event )
    local group = self.view    
end
function scene:exitScene( event )
    local group = self.view    
end
function scene:destroyScene( event )
    local group = self.view            
end
scene:addEventListener( “createScene”, scene )
scene:addEventListener( “enterScene”, scene )
scene:addEventListener( “exitScene”, scene )
scene:addEventListener( “destroyScene”, scene )
return scene

---------------------------------------------------------------------We’re almost there------------------------

Now because you created a button, you have to destroy that button, before you go to a new scene, if you don’t the button will be there forever.

In the destroyScene you add this code–

if buttonHome then                                  ------- if you use the button, of course you did
        buttonHome:removeSelf()                ------- then button, remove your self
        buttonHome = nil                               ------- and you add this line, that’s it button, we’re done.
    end

 so the complete code looks like this

local storyboard = require( “storyboard” )
local widget = require “widget”
local scene = storyboard.newScene()
local function returnHome()
        storyboard.gotoScene( “level1Q3”, “crossFade”, 1000 )    
    return true
end
function scene:createScene( event )
    local group = self.view
    local background = display.newImage( “backgroundLevel1Q4.png” )
    buttonHome = widget.newButton{
        defaultFile=“buttonHome.png”,
        onRelease = returnHome
    }
    buttonHome.x = 522
    buttonHome.y = 655
    group:insert ( background )
    group:insert ( buttonHome )
end
function scene:enterScene( event )
    local group = self.view    
end
function scene:exitScene( event )
    local group = self.view    
end
function scene:destroyScene( event )
    local group = self.view
    if buttonHome then
        buttonHome:removeSelf()
        buttonHome = nil
    end        
end
scene:addEventListener( “createScene”, scene )
scene:addEventListener( “enterScene”, scene )
scene:addEventListener( “exitScene”, scene )
scene:addEventListener( “destroyScene”, scene )
return scene


With this code you can go from one scene to another, as many as you want.

This works for me really good, I hope I made this very simple. It’s long but you read it and copy the paste nad that’s it.

The answer I was looking for one month ago, now it’s here.

I know it’s really good, if you did not know how to do this and now this help you…

you’re welcome.

Victor

Is anybody there? Hello?

Checkout Corona’s sample code.
Everything you need to get started can be found there.

  • It’s all included in the file you get when you download the SDK -

Would you please help me out, I don’t really understand the concept, if you know how, would you teach me please?

Victor

[lua]

local storyboard = require “storyboard”

storyboard.gotoScene( “game”, options )

[/lua]

Above is the most basic of scene changes. You would probably want to wrap that goToScene call in a listener attached to something like a button, I suspect. Go and have a look at the storyboard tutorial and try to write a bit of code and do some trial and error.

If you don’t put in any effort and try to do it yourself, nobody else will. If you end up getting stuck, post the code your having problems with and people will probably be more inclined to help. A general “tell me how to do everything so I don’t have to” isn’t going to get many responses.

Especially since your in the wrong subforum for storyboard :slight_smile:

Thank you Mourdos: For the time you took to post this…

I AM VERY VERY VERY NEW AT ALL THIS! So

– local storyboard = require “storyboard”

– storyboard.gotoScene (“game”, options)

it doesn’t tell me anything, I just don’t get it, and I don’t know how to use it or put it in my code.

I just one to change the page, like a book.

YOU: “You would probably want to wrap that goToScene call in a listener attached to something like a button, I suspect”

ME: If you mean “goToScene” go to another “Page” yes that it’s what I want, BUT I DON’T KNOW HOW.

       When you said “attached to a button”, Yes!, but I don’t know how.

YOU: “Go and have a look at the storyboard tutorial and try to write a bit of code and do some trial and error.”

ME: I don’t know where it is…And even If I find it, I go and just read and click, isn’t it another way? I would not know what

        I’m looking for, so I don’t know what to click.

YOU: “If you don’t put in any effort”

ME: Yes I AM putting a lot of effeort, I bought a book from Dr. Burton, I still don’t find that answer ( and I read all the book

        already). I download about 20 sample code from github, and nothing, I watch about 100 videos on youtube, and nothing

        all I have is this forum it’s the only one I have found that somebody actually talks to me back, so I’m lost. But I AM 

        PUTTING A LOT OF EFFORT, I just don’t know what to do.

YOU: “post the code your having problems with”

ME: The problem is that I don’t have a code yet! I know how to do a few things, but all of them are in just one page, or one

       view, or one file main.lua, So all those things I can do, and I understand it, but I don’t know how to turn a page (gotoScene)

YOU: “A general “tell me how to do everything so I don’t have to” isn’t going to get many responses”

ME: And I don’t want people to tell me how, so I don’t have to… I WANT TO LEARN, so I know there is a way to change a view

       or goToScene, I KNOW THAT, but…

       1 .- Either I have not found someone who knows how to do it

       2.- Or they know, and they just don’t want to share it, or teach me

       3.- Or because they had SO MUCH trouble learning before, just like me today, now they want me to suffer, jut like they did

YOU: “Especially since your in the wrong subforum for storyboard”

ME: If you KNOW that I am in the “WRONG” place, would you be so kind to tell me how to get to the “RIGHT” place. And it took

       me 3 or 4 days to answer this, because I wanted to find out for myself, but I JUST CAN’T.

       Even if I AM in the WRONG place, this is the ONLY PLACE that at least I get people like you to tell me things, and I’m

       learning like that.

By the way my name it’s Victor M. Barba, if you Google my name you’ll see that I have a lot of books published already by the major music companies in the world. SO I WILL LEARN HOW TO MAKE AN APP, what ever it takes.

I hope you still can and want to help me and if you know how to gotoScene or change the page of my book, would you please tell me how in a very EXPLICIT WAY, I would really appreciated it.

Thank you Mourdos, and God bless you!

Victor

Hi Victor, have you had a chance to review the sample called Storyboard? It has 4 scenes and transitions between them. You can take this and tweak it to meet your needs. Alternatively, I think I recall seeing an eBook template for sale on the commercial section of this forum. I would think that the template developer would help you get up to speed if you wish to go that way. 

PS. No connection to the template developer mentioned and have no clue on the quality of his/her work. Was just an idea. 

Best of luck in your endeavor.

Thank you Ksan:

Where do I get that "Sample called Storyboard?

It should be in the following subfolder of your Corona installation : 

/CoronaSDK/SampleCode/Interface/Storyboard

"1 .- Either I have not found someone who knows how to do it

  2.- Or they know, and they just don’t want to share it, or teach me

  3.- Or because they had SO MUCH trouble learning before, just
like me today, now they want me to suffer, jut like they did"

I can guarantee nobody here wants you to suffer. :slight_smile:

We just don’t want to up and give you the code, not because we don’t want to help, but because we would actually be doing you a disservice - I think so anyway.
When it comes to learning to program, I’ve found that while studying and reading books on the subject can certainly help, actually creating a blank project and typing even just a few lines of code to display an image is far more valuable.

Have you tried looking at/experimenting with the various templates?
Corona Simulator > New Project >  Choose a template:
Blank
App
Game
E-book
Scene

If you did in fact read Burton’s book, then you should at least have a
understanding of various basic concepts and be able to experiment with the code included in Corona’s templates, and if not then I’d suggest re-reading the book and having a go at implementing some of the concepts he covers.

While it’s a good goal to try and challenge yourself, I’d suggest you try and make something very basic so you can get a firm understanding of the code&concepts that go into creating an app.

You can do it!! :smiley:

I’m going to move this to the storyboard forum.

OK helloworld2013,

You sound a little lost.  Did you download Corona SDK?  If so, that will contain a whole bunch of sample code, along with a Corona Simulator, which will run the codes.  It will not contain a text editor, which you will have to look for and download from the web.  I’m on a Mac and I use TextWrangler, but if your on a pc, just look for a good free code editor.  

Now, open the Corona SDK folder and look for Sample Code, then Interface, then Storybook.  Open the main.lua file in the simulator, and ALL of the .lua files in the text editor.  

Now start playing with the code.  The best way to learn what the code does and how it works is to mess with it.  Change numbers, save it, and see what it changed in the simulator.  Study every aspect of the code, and learn what it does.  Remember, whenever you change something in the code, save it, and see what it did in the simulator.  You can always undo anything that breaks the app.

Hope this is basic enough to get you started.

Thank you everyone for helping me, THANK YOU VERY MUCH!

I’m learning little by little, but I’m almost there to start making my books.

One more question – I’m adding a video to try to explaing the question better

http://www.youtube.com/watch?v=waa_Troorf4&feature=youtu.be

Thank you again for all your help.

I got the sound –

the storyboard –

the animation –

the background –

Good!

Victor

Great to hear of your progress!!! 

ksan, do you know how to answer the question I have in the video?

Awesome! glad you’re making progress! :slight_smile:

One question though -
On scene one, is that one entire background image or are those icons (what you are wanting to be buttons) individual images?

OK, it’s pretty simple.  Before you do anything, you’ll need to create images for your buttons as .png files.  Then, here’s how I do it.

--here you create the function to go to the new scene, my FUNCTION is called "gotobg" local function gotobg(e) storyboard.gotoScene( "bluegrass" ) end --then you need to create the button, and here is how i do it... "bgme" is my object name --and notice what it points to for the "onRelease" local bgme = ui.newButton{ default="bgbutton.png", over="bgbuttonover.png", onRelease=gotobg } bgme.x = display.contentWidth / 2 bgme.y = display.contentHeight / 2

That’s it.  Notice my image is called “bgbutton.png” and it’s .x and .y are set to be the middle of the screen. The scene I’m going to is called “bluegrass.lua”

*Everything* you need for creating a button is right here:
http://docs.coronalabs.com/api/library/widget/newButton.html

If you have a solid background and not individual images for buttons, then you’ll just need to create an invisible circle/rectangle and position it over the area where you want the button.
For invisible touch-sensitive buttons/whatever:

.isVisible = false;
.isHitTestable = true;

Also, check out this page: http://docs.coronalabs.com/api/event/touch/phase.html

 

I will post a video with all this answers, but first let me ask you this:

http://www.youtube.com/watch?v=ieli_AXLGwU&feature=youtu.be

Hi Saerothir, eieiosoftware, ksan…

I have this video trying the code that eieiosoftware, told me, I couldn’t make it work WHY?

http://www.youtube.com/watch?v=yxxuX9gpnPs&feature=youtu.be

programming is hard, I guess you must be really inteligent, to do this. (I think I am)… or I though I was!