Storyboard problem in my game

Hi guys,

I’m working on a game with Corona, and I have a problem and I can’t find any solutions to solved it.

local storyboard = require( "storyboard" ) local scene = storyboard.newScene() local widget = require "widget" local game = require("game") local function onbtn\_back() storyboard.gotoScene( "bg\_home", "fade", 500 ) return true end --[[local function onbtn\_startlevel1() storyboard.gotoScene( "bg\_level", "fade", 500 ) return true local Game Game = game.new("images/backgrounds/bg\_newyork.jpg") Game:startGame() storyboard.removeScene( "bg\_menulvl" ) end]] local function onbtn\_startlevel2() local Game Game = game.new("images/backgrounds/bg\_paris.jpg") Game:startGame() storyboard.removeScene( "bg\_menulvl" ) end local function onbtn\_startlevel3() local Game Game = game.new("images/backgrounds/bg\_lecaire.jpg") Game:startGame() storyboard.removeScene( "bg\_menulvl" ) end local function onbtn\_startlevel4() local Game Game = game.new("images/backgrounds/bg\_tokyo.jpg") Game:startGame() storyboard.removeScene( "bg\_menulvl" ) end local function onbtn\_startlevel5() local Game Game = game.new("images/backgrounds/bg\_moscou.jpg") Game:startGame() storyboard.removeScene( "bg\_menulvl" ) end local function onbtn\_startlevel6() local Game Game = game.new("images/backgrounds/bg\_rio.jpg") Game:startGame() storyboard.removeScene( "bg\_menulvl" ) end local function onbtn\_startlevelarcade() storyboard.gotoScene( "bg\_arcade", "fade", 500 ) end local function onbtn\_startlevelchallenge() storyboard.gotoScene( "bg\_challenge", "fade", 500 ) end function scene:createScene( event ) local group = self.view local background = display.newImageRect( "images/backgrounds/bg\_menulvl.jpg", display.contentWidth, display.contentHeight ) background.x, background.y = display.contentCenterX, display.contentCenterY btn\_back = widget.newButton { defaultFile = "images/boutons/b\_back.png", width = 200, height = 200, left = 150, top = 80, onRelease = onbtn\_back } options = { effect = "fade", params = { sample\_var = "anything", custom = "you want", data = "here", level1 = "new-york"} } btn\_newyork = widget.newButton { defaultFile = "images/boutons/b\_newyork.png", width = 200, height = 200, left = 350, top = 250, --onRelease = onbtn\_startlevel1 onRelease = function () storyboard.gotoScene( "bg\_level", options ) end } btn\_paris = widget.newButton { defaultFile = "images/boutons/b\_paris.png", width = 200, height = 200, left = 800, top = 180, onRelease = onbtn\_startlevel2 } btn\_lecaire = widget.newButton { defaultFile = "images/boutons/b\_lecaire.png", width = 200, height = 200, left = 890, top = 400, onRelease = onbtn\_startlevel3 } btn\_tokyo = widget.newButton { defaultFile = "images/boutons/b\_tokyo.png", width = 200, height = 200, left = 1550, top = 300, onRelease = onbtn\_startlevel4 } btn\_moscou = widget.newButton { defaultFile = "images/boutons/b\_moscou.png", width = 200, height = 200, left = 1100, top = 170, onRelease = onbtn\_startlevel5 } btn\_rio = widget.newButton { defaultFile = "images/boutons/b\_rio.png", width = 200, height = 200, left = 500, top = 650, onRelease = onbtn\_startlevel6 } btn\_arcade = widget.newButton { defaultFile = "images/boutons/b\_arcade.png", width = 360, height = 128, left = 800, top = 600, onRelease = onbtn\_startlevelarcade } btn\_challenge = widget.newButton { defaultFile = "images/boutons/b\_challenge.png", width = 481, height = 128, left = 740, top = 750, onRelease = onbtn\_startlevelchallenge } group:insert( background ) group:insert( btn\_back ) group:insert( btn\_newyork ) group:insert( btn\_paris ) group:insert( btn\_lecaire ) group:insert( btn\_tokyo ) group:insert( btn\_moscou ) group:insert( btn\_rio ) group:insert( btn\_arcade ) group:insert( btn\_challenge ) end scene:addEventListener( "createScene", scene ) return scene

This is normally the file how to choose my level, 

After this, I have one other file :

local game = {} local widget = require "widget" local storyboard = require( "storyboard" ) local scene = storyboard.newScene() physics = require("physics") physics.start() physics.setGravity(0, 0) local function onbtn\_pause() local Game Game = game.new("images/backgrounds/bg\_pause.jpg") Game:pauseGame() end local function onbtn\_home() storyboard.loadScene( "bg\_home", "fade", 500 ) end local function onbtn\_map() storyboard.loadScene( "bg\_menulvl", "fade", 500 ) end local function onbtn\_score() storyboard.gotoScene( "bg\_score", "fade", 500 ) end function game.new(levelN) local game = display.newGroup() game.id = "game" local Missile = require("missile") local missiles function initGame() local background = display.newImageRect( levelN, display.contentWidth, display.contentHeight ) background.x, background.y = display.contentCenterX, display.contentCenterY btn\_pause = widget.newButton { defaultFile = "images/boutons/b\_pause.png", width = 150, height = 150, left = 1700, top = 40, onRelease = onbtn\_pause } end function initPause() local background = display.newImageRect( levelN, display.contentWidth, display.contentHeight ) background.x, background.y = display.contentCenterX, display.contentCenterY btn\_son = widget.newButton { defaultFile = "images/boutons/b\_son\_on.png", width = 200, height = 200, left = 150, top = 80, --onRelease = onbtn\_faq } btn\_fx = widget.newButton { defaultFile = "images/boutons/b\_fx\_on.png", width = 200, height = 200, left = 150, top = 300, --onRelease = onbtn\_faq } btn\_reprendre = widget.newButton { defaultFile = "images/boutons/b\_play.png", width = 200, height = 200, left = 1650, top = 80, onRelease = onbtn\_reprendre } btn\_recommencer = widget.newButton { defaultFile = "images/boutons/b\_replay.png", width = 200, height = 200, left = 1650, top = 300, onRelease = onbtn\_recommencer } btn\_home = widget.newButton { defaultFile = "images/boutons/b\_home.png", width = 200, height = 200, left = 150, top = 800, onRelease = onbtn\_home } btn\_map = widget.newButton { defaultFile = "images/boutons/b\_map.png", width = 200, height = 200, left = 400, top = 800, onRelease = onbtn\_map } btn\_score = widget.newButton { defaultFile = "images/boutons/b\_score.png", width = 200, height = 200, left = 1650, top = 820, onRelease = onbtn\_score } end function game:startGame() initGame() end function game:pauseGame() initPause() end return game end return game

The problem is right here ! 

When I begin to implement the Game: in the first file, I CAN’T USE the storyboard after ! And i really don’t know how I can use the function onbtn_Home or function who used storyboard in the second file.

Someone have a idea ? 

I’m not sure which file is which, but the second one is not a storyboard scene.  It never returns scene and doesn’t have the event handlers for createScene, enterScene, etc. that storyboard expects. 

I can’t tell if you intend the last item to be a storyboard scene, or an object that you would use in a storyboard scene. 

Yes it’s not,

Because if you look the first file, you have a new instance of Game, where i draw the background, it’s after this phase I want to recreate the storyboard but it doesn’t work =/

is the first file above “bg_menulvl”?

If so, it’s perhaps because you’re removing the scene.  Let me try to explain that clearer.  You’re drawing your “game” on top of your storyboard scene then nuking your storyboard scene, so storyboard doesn’t have a current scene to goto.   Is there a reason you are removing it considering you drawing your game level on top?  It would seem that when your game was over, you could just get rid of the game and your menu would be sitting there waiting for you.  Most people would make your game a scene and just go to it.

Rob

No, i didn’t remove my scene, but I think I begin to understand.

But let me explain my game : 

I begin the game with a storyboard and some files where i can change the storyboard easier. When I’m on the bg_menulvl, you can choose 7 levels, it’s a this moment I want to call the Game class to start the game and it’s work (and I don’t remove the scene) but after when i’m playing, I have a pause button, and on this button when I click, the game is in pause and i have other buttons who are drawing, it’s at this time, when i want to press the buttons :

local function onbtn\_home() storyboard.loadScene( "bg\_home", "fade", 500 ) end local function onbtn\_map() storyboard.loadScene( "bg\_menulvl", "fade", 500 ) end

It doesn’t work, and I don’t know why at this moment.

Do you understand my problem ^^ ?

In your code you have:

local function onbtn\_startlevel3()     local Game     Game = game.new("images/backgrounds/bg\_lecaire.jpg")     Game:startGame()     storyboard.removeScene( "bg\_menulvl" ) end

Looks like you’re removing the scene in these button handlers.

But I think i see what you’re saying.  First you should probably be doing a storyboard.gotoScene() not .loadScene().  “bg_menulvl” is the current scene and your game is setting on top of it.  The way storyboard works is anything that you draw that you **DO NOT** insert into a storyboard’s scene group sits on top of storyboard.  You cannot make a storyboard scene sit on top of other graphics.   So when you hit your pause button, because your game is not a scene, storyboard cannot transition your scene on top.  If you set your game’s display group .isVisible to false, I bet your would see your storyboards working as expected.  

Rob

So,

I normally just need to remove the line : storyboard.removeScene on the function onbtn_startLevel()

and in the other file, when i click to put :     game.isVisible = false

right ?

EDIT : And i’m not sure, because my game.lua is like this :

local game = {} local widget = require "widget" local function onbtn\_pause() local Game Game = game.new("images/backgrounds/bg\_pause.jpg") Game:pauseGame() end local function onbtn\_home() storyboard.gotoScene( "bg\_home", "fade", 500 ) end local function onbtn\_map() storyboard.gotoScene( "bg\_menulvl", "fade", 500 ) end local function onbtn\_score() storyboard.gotoScene( "bg\_score", "fade", 500 ) end function game.new(levelN) function initGame() local background = display.newImageRect( levelN, display.contentWidth, display.contentHeight ) background.x, background.y = display.contentCenterX, display.contentCenterY btn\_pause = widget.newButton { defaultFile = "images/boutons/b\_pause.png", width = 150, height = 150, left = 1700, top = 40, onRelease = onbtn\_pause } end function initPause() local background = display.newImageRect( levelN, display.contentWidth, display.contentHeight ) background.x, background.y = display.contentCenterX, display.contentCenterY btn\_son = widget.newButton { defaultFile = "images/boutons/b\_son\_on.png", width = 200, height = 200, left = 150, top = 80, --onRelease = onbtn\_faq } btn\_fx = widget.newButton { defaultFile = "images/boutons/b\_fx\_on.png", width = 200, height = 200, left = 150, top = 300, --onRelease = onbtn\_faq } btn\_reprendre = widget.newButton { defaultFile = "images/boutons/b\_play.png", width = 200, height = 200, left = 1650, top = 80, onRelease = onbtn\_reprendre } btn\_recommencer = widget.newButton { defaultFile = "images/boutons/b\_replay.png", width = 200, height = 200, left = 1650, top = 300, onRelease = onbtn\_recommencer } btn\_home = widget.newButton { defaultFile = "images/boutons/b\_home.png", width = 200, height = 200, left = 150, top = 800, onRelease = onbtn\_home } btn\_map = widget.newButton { defaultFile = "images/boutons/b\_map.png", width = 200, height = 200, left = 400, top = 800, onRelease = onbtn\_map } btn\_score = widget.newButton { defaultFile = "images/boutons/b\_score.png", width = 200, height = 200, left = 1650, top = 820, onRelease = onbtn\_score } end function game:startGame() initGame() end function game:pauseGame() initPause() end return game end return game

ANd i don’t have any group on my game class, it’s a problem ?

You will probably need to have everything in a group so you can easily hide it.

Rob

So, in my funtion : function game.new(levelN)

local game = display.newGroup()

And I add all my variables in this group and after in my function i call game.isVisible = false ?

right ?

Yes, you can add all your variables to the group like that.

Rob

I’m not sure which file is which, but the second one is not a storyboard scene.  It never returns scene and doesn’t have the event handlers for createScene, enterScene, etc. that storyboard expects. 

I can’t tell if you intend the last item to be a storyboard scene, or an object that you would use in a storyboard scene. 

Yes it’s not,

Because if you look the first file, you have a new instance of Game, where i draw the background, it’s after this phase I want to recreate the storyboard but it doesn’t work =/

is the first file above “bg_menulvl”?

If so, it’s perhaps because you’re removing the scene.  Let me try to explain that clearer.  You’re drawing your “game” on top of your storyboard scene then nuking your storyboard scene, so storyboard doesn’t have a current scene to goto.   Is there a reason you are removing it considering you drawing your game level on top?  It would seem that when your game was over, you could just get rid of the game and your menu would be sitting there waiting for you.  Most people would make your game a scene and just go to it.

Rob

No, i didn’t remove my scene, but I think I begin to understand.

But let me explain my game : 

I begin the game with a storyboard and some files where i can change the storyboard easier. When I’m on the bg_menulvl, you can choose 7 levels, it’s a this moment I want to call the Game class to start the game and it’s work (and I don’t remove the scene) but after when i’m playing, I have a pause button, and on this button when I click, the game is in pause and i have other buttons who are drawing, it’s at this time, when i want to press the buttons :

local function onbtn\_home() storyboard.loadScene( "bg\_home", "fade", 500 ) end local function onbtn\_map() storyboard.loadScene( "bg\_menulvl", "fade", 500 ) end

It doesn’t work, and I don’t know why at this moment.

Do you understand my problem ^^ ?

In your code you have:

local function onbtn\_startlevel3()     local Game     Game = game.new("images/backgrounds/bg\_lecaire.jpg")     Game:startGame()     storyboard.removeScene( "bg\_menulvl" ) end

Looks like you’re removing the scene in these button handlers.

But I think i see what you’re saying.  First you should probably be doing a storyboard.gotoScene() not .loadScene().  “bg_menulvl” is the current scene and your game is setting on top of it.  The way storyboard works is anything that you draw that you **DO NOT** insert into a storyboard’s scene group sits on top of storyboard.  You cannot make a storyboard scene sit on top of other graphics.   So when you hit your pause button, because your game is not a scene, storyboard cannot transition your scene on top.  If you set your game’s display group .isVisible to false, I bet your would see your storyboards working as expected.  

Rob

So,

I normally just need to remove the line : storyboard.removeScene on the function onbtn_startLevel()

and in the other file, when i click to put :     game.isVisible = false

right ?

EDIT : And i’m not sure, because my game.lua is like this :

local game = {} local widget = require "widget" local function onbtn\_pause() local Game Game = game.new("images/backgrounds/bg\_pause.jpg") Game:pauseGame() end local function onbtn\_home() storyboard.gotoScene( "bg\_home", "fade", 500 ) end local function onbtn\_map() storyboard.gotoScene( "bg\_menulvl", "fade", 500 ) end local function onbtn\_score() storyboard.gotoScene( "bg\_score", "fade", 500 ) end function game.new(levelN) function initGame() local background = display.newImageRect( levelN, display.contentWidth, display.contentHeight ) background.x, background.y = display.contentCenterX, display.contentCenterY btn\_pause = widget.newButton { defaultFile = "images/boutons/b\_pause.png", width = 150, height = 150, left = 1700, top = 40, onRelease = onbtn\_pause } end function initPause() local background = display.newImageRect( levelN, display.contentWidth, display.contentHeight ) background.x, background.y = display.contentCenterX, display.contentCenterY btn\_son = widget.newButton { defaultFile = "images/boutons/b\_son\_on.png", width = 200, height = 200, left = 150, top = 80, --onRelease = onbtn\_faq } btn\_fx = widget.newButton { defaultFile = "images/boutons/b\_fx\_on.png", width = 200, height = 200, left = 150, top = 300, --onRelease = onbtn\_faq } btn\_reprendre = widget.newButton { defaultFile = "images/boutons/b\_play.png", width = 200, height = 200, left = 1650, top = 80, onRelease = onbtn\_reprendre } btn\_recommencer = widget.newButton { defaultFile = "images/boutons/b\_replay.png", width = 200, height = 200, left = 1650, top = 300, onRelease = onbtn\_recommencer } btn\_home = widget.newButton { defaultFile = "images/boutons/b\_home.png", width = 200, height = 200, left = 150, top = 800, onRelease = onbtn\_home } btn\_map = widget.newButton { defaultFile = "images/boutons/b\_map.png", width = 200, height = 200, left = 400, top = 800, onRelease = onbtn\_map } btn\_score = widget.newButton { defaultFile = "images/boutons/b\_score.png", width = 200, height = 200, left = 1650, top = 820, onRelease = onbtn\_score } end function game:startGame() initGame() end function game:pauseGame() initPause() end return game end return game

ANd i don’t have any group on my game class, it’s a problem ?

You will probably need to have everything in a group so you can easily hide it.

Rob

So, in my funtion : function game.new(levelN)

local game = display.newGroup()

And I add all my variables in this group and after in my function i call game.isVisible = false ?

right ?

Yes, you can add all your variables to the group like that.

Rob