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