Text Adventure Storyboard issues

Yes, this is what I have in menu.lua currently

– require controller module

local storyboard = require “storyboard”

local widget = require “widget”

– load first scene

local option1 = widget.newButton

    {

    top = 280,

        width = display.contentWidth,

        height = 59,

        label = “Start Game”,

    onRelease = goBack,

        onPress = function() storyboard.gotoScene( “scene1”,“fade”,400 ); end,

      

    }

  

  – other options

  

  local option2 = widget.newButton

    {

    top = 340,

        width = display.contentWidth,

        height = 59,

        label = “Help”,

    onRelease = goBack,

  }

  

  local option3 = widget.newButton

    {

    top = 400,

        width = display.contentWidth,

        height = 59,

        label = “Quit”,

    onRelease = goBack,

    }

  

The code I posted before (button code) is what you should be using

Okay i changed the menu.lua and also changed main to include 

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

storyboard.gotoScene(“menu”)

and the buttons disappear now as they should. Is there an easy way to explain why what I had before did not work?

Basically it is because in main.lua you called storyboard.gotoScene(“menu”), which makes it expect a storyboard scene.  But with your original menu.lua code it actually wasn’t a storyboard scene because of the missing code (primarily the local scene = storyboard.newScene() and return scene).  You can’t use storyboard.gotoScene if you are not going to an actual storyboard scene.  

That makes perfect sense. Thanks so much for all of your help :slight_smile: You don’t have to keep answering my questions at all, but I do have one more…

Everything is working how I want it to now with the exception of the fact that my variables are also showing on the top of the menu screen. I want them to show up on the top of every screen EXCEPT the menu screen.


– main.lua


– require controller module

local storyboard = require “storyboard”

local widget = require “widget”

local variables = require “variables”

– load first scene

display.setStatusBar(display.HiddenStatusBar)

storyboard.gotoScene(“menu”)

– Display objects added below will not respond to storyboard transitions

titleBar = display.newRect( 0, -100, display.contentWidth, 120 )

titleBar:setFillColor( titleGradient )

healthObject = display.newText ("Health: " … health ,20, 0, “Times New Roman”, 10)

healthObject:setTextColor(255,0,0)

manaObject = display.newText ("Mana: " … mana ,95, 0, “Times New Roman”, 10)

manaObject:setTextColor(0,0,255)

moraleObject = display.newText ("Morale: " … morale ,170, 0, “Times New Roman”, 10)

moraleObject:setTextColor(0,255,0)

goldObject = display.newText ("Gold: " … gold ,245, 0, “Times New Roman”, 10)

goldObject:setTextColor(215,200,0)

So again, when you create display objects and you don’t insert them into ‘group’, then storyboard does not manage these display objects, you must manually manage them yourself.  

If I were you I would create these display objects in every scene you want them in and let storyboard handle them, instead of only creating them once and leaving them there.  

Side note, save the code below to a file and use it every time you create a new scene (aka page) in your app.  This is basically the main things you need to create a storyboard scene.

-- blankscene.lua local storyboard = require( "storyboard" ) local scene = storyboard.newScene() ---------------------------------------------------------------------------------- -- -- NOTE: -- -- Code outside of listener functions (below) will only be executed once, -- unless storyboard.removeScene() is called. -- --------------------------------------------------------------------------------- --------------------------------------------------------------------------------- -- BEGINNING OF YOUR IMPLEMENTATION --------------------------------------------------------------------------------- -- Called when the scene's view does not exist: function scene:createScene( event ) local group = self.view ----------------------------------------------------------------------------- -- CREATE display objects and add them to 'group' here. -- Example use-case: Restore 'group' from previously saved state. ----------------------------------------------------------------------------- end -- Called immediately after scene has moved onscreen: function scene:enterScene( event ) local group = self.view ----------------------------------------------------------------------------- -- INSERT code here (e.g. start timers, load audio, start listeners, etc.) ----------------------------------------------------------------------------- end -- Called when scene is about to move offscreen: function scene:exitScene( event ) local group = self.view ----------------------------------------------------------------------------- -- INSERT code here (e.g. stop timers, remove listeners, unload sounds, etc.) ----------------------------------------------------------------------------- end -- Called prior to the removal of scene's "view" (display group) function scene:destroyScene( event ) local group = self.view ----------------------------------------------------------------------------- -- INSERT code here (e.g. remove listeners, widgets, save state, etc.) ----------------------------------------------------------------------------- end --------------------------------------------------------------------------------- -- END OF YOUR IMPLEMENTATION --------------------------------------------------------------------------------- -- "createScene" event is dispatched if scene's view does not exist scene:addEventListener( "createScene", scene ) -- "enterScene" event is dispatched whenever scene transition has finished scene:addEventListener( "enterScene", scene ) -- "exitScene" event is dispatched before next scene's transition begins scene:addEventListener( "exitScene", scene ) -- "destroyScene" event is dispatched before view is unloaded, which can be -- automatically unloaded in low memory situations, or explicitly via a call to -- storyboard.purgeScene() or storyboard.removeScene(). scene:addEventListener( "destroyScene", scene ) --------------------------------------------------------------------------------- return scene

Okay thanks. That’s what i was figuring I would have to do but I thought there might be a way to only have to write it once. Thanks again Jon :slight_smile:

sigh - and I thought I was done. 

I am trying to alter the variables based on what button is pushed. for example 

  local option2 = widget.newButton

    {

        width = display.contentWidth,

        height = 59,

        label = “YOU DIE HAHA”,

        onRelease = goBack,

        onPress = function()

 storyboard.gotoScene( “scene2”,“fade”,400 ) health = (health - 10);end,

and have the new value for health display on the next scene. i don’t get an error with the above code, but it also does not change the variable in the next scene.

I have a separate variables.lua that just contains this

 health = 100

mana = 10

morale = 10

gold = 0

and my scenes all have local variables = require “variables” at the top

You should check out this: Tutorial: Goodbye Globals! | Corona Labs

That seems to say that what I have should work. I didnt add the prefix “local” to any of my variables, so they should in theory be accessible through the whole application right?

So why is it when i modify health in onPressed, the change doesnt stick?

Where are you updating the text display object?  

At the top of every scene. Here’s an example of a full scene i have currently. The others are exactly the same.

– scene1.lua


local widget = require( “widget” )

local storyboard = require( “storyboard” )

local scene = storyboard.newScene()

variables = require “variables”


– 

–    NOTE:

–    

–    Code outside of listener functions (below) will only be executed once,

–    unless storyboard.removeScene() is called.

– 


 titleBar = display.newRect( 0, -100, display.contentWidth, 120 )

titleBar:setFillColor( titleGradient )

healthObject = display.newText ("Health: " … health ,20, 0, “Times New Roman”, 10)

healthObject:setTextColor(255,0,0)

manaObject = display.newText ("Mana: " … mana ,95, 0, “Times New Roman”, 10)

manaObject:setTextColor(0,0,255)

moraleObject = display.newText ("Morale: " … morale ,170, 0, “Times New Roman”, 10)

moraleObject:setTextColor(0,255,0)

goldObject = display.newText ("Gold: " … gold ,245, 0, “Times New Roman”, 10)

goldObject:setTextColor(215,200,0)


– BEGINNING OF YOUR IMPLEMENTATION


– Called when the scene’s view does not exist:

function scene:createScene( event )

    local group = self.view

local function scrollListener( event )

   local phase = event.phase

   print( phase )

   local direction = event.direction

   – If the scrollView has reached it’s scroll limit

   if ( event.limitReached ) then

      if ( “up” == direction ) then

         print( “Reached Top Limit” )

      elseif ( “down” == direction ) then

         print( “Reached Bottom Limit” )

      elseif ( “left” == direction ) then

         print( “Reached Left Limit” )

      elseif ( “right” == direction ) then

         print( “Reached Right Limit” )

      end

   end

   return true

end

local scrollView = widget.newScrollView

{

   left = 0,

   top = 20,

   width = display.contentWidth,

   height = display.contentHeight,

   scrollWidth = 465,

   scrollHeight = 670,

   friction = 0.972,

   listener = scrollListener,

   

   backgroundColor = {0,0,0}

   

}

–Create a large text string

local thisText = “SCENE ONE TESTING SCROLLING…SCENE ONE TESTING SCROLLING…SCENE ONE TESTING SCROLLING…SCENE ONE TESTING SCROLLING…SCENE ONE TESTING SCROLLING…SCENE ONE TESTING SCROLLING…SCENE ONE TESTING SCROLLING…SCENE ONE TESTING SCROLLING…SCENE ONE TESTING SCROLLING…SCENE ONE TESTING SCROLLING…SCENE ONE TESTING SCROLLING…SCENE ONE TESTING SCROLLING…SCENE ONE TESTING SCROLLING…SCENE ONE TESTING SCROLLING…SCENE ONE TESTING SCROLLING…” 

–Create a text object containing the large text string and insert it into the scrollView

local sceneObject = display.newText( thisText, 0,20, 250,1000, “Times New Roman”, 20)

sceneObject:setTextColor( 255,255,255 ) 

sceneObject:setReferencePoint( display.TopCenterReferencePoint )

sceneObject.x = display.contentCenterX

scrollView:insert( sceneObject )

local option1 = widget.newButton

    {

        width = display.contentWidth,

        height = 59,

        label = “SCENE2”,

    onRelease = goBack,

        onPress = function()

 storyboard.gotoScene( “scene2”,“fade”,400 );end,

    }

    option1.x = display.contentWidth - option1.width/2

    option1.y = sceneObject.contentHeight - option1.height/2

    scrollView:insert( option1 )

  

  local option2 = widget.newButton

    {

        width = display.contentWidth,

        height = 59,

        label = “YOU DIE HAHA”,

        onRelease = goBack,

        onPress = function()

 storyboard.gotoScene( “scene2”,“fade”,400 ) health = (health - 10);end,

    }

    option2.x = display.contentWidth - option2.width/2

    option2.y = sceneObject.contentHeight + option1.height - option2.height/2

    scrollView:insert( option2 )

    group:insert(scrollView)

    -----------------------------------------------------------------------------

        

    –    CREATE display objects and add them to ‘group’ here.

    –    Example use-case: Restore ‘group’ from previously saved state.

    

    -----------------------------------------------------------------------------

    

end

– Called immediately after scene has moved onscreen:

function scene:enterScene( event )

    local group = self.view

    

  

    -----------------------------------------------------------------------------

        

    –    INSERT code here (e.g. start timers, load audio, start listeners, etc.)

    

    -----------------------------------------------------------------------------

    

  

end

– Called when scene is about to move offscreen:

function scene:exitScene( event )

    local group = self.view

    

    -----------------------------------------------------------------------------

    

    –    INSERT code here (e.g. stop timers, remove listeners, unload sounds, etc.)

    

    -----------------------------------------------------------------------------

    

end

– Called prior to the removal of scene’s “view” (display group)

function scene:destroyScene( event )

    local group = self.view

    

    -----------------------------------------------------------------------------

    

    –    INSERT code here (e.g. remove listeners, widgets, save state, etc.)

    

    -----------------------------------------------------------------------------

    

end


– END OF YOUR IMPLEMENTATION


– “createScene” event is dispatched if scene’s view does not exist

scene:addEventListener( “createScene”, scene )

– “enterScene” event is dispatched whenever scene transition has finished

scene:addEventListener( “enterScene”, scene )

– “exitScene” event is dispatched before next scene’s transition begins

scene:addEventListener( “exitScene”, scene )

– “destroyScene” event is dispatched before view is unloaded, which can be

– automatically unloaded in low memory situations, or explicitly via a call to

– storyboard.purgeScene() or storyboard.removeScene().

scene:addEventListener( “destroyScene”, scene )


return scene

Well, you’re still not following what I’m saying about storyboard and display objects.  Again, I recommend you take time away from your project and read up on storyboard (or view videos on youtube).  

you mean adding them to group. ah. woops.

I’ve been coding for about a week - bunch of new concepts. Thanks though I intend to do just that. I find I learn better by just jumping in and doing things usually.

Well I’m not sure this is the correct way to do it but it seems to be working. 

Rather than define variables as global i did this

local storyboard = require “storyboard”

storyboard.state = {}

storyboard.state.health = 100

storyboard.state.mana = 10

storyboard.state.morale = 10

storyboard.state.gold = 0

Made quite a bit of progress. I am having some trouble with making a button unclickable if a state is less than a certain value. This is what I have for the button - Not sure what I am missing here - The error I get says unexpected symbol near ‘if’

local option1 = widget.newButton

    {

        width = display.contentWidth,

        height = 59,

        label = “minus mana plus gold”,

        if storyboard.state.mana < 10 then option1:setEnabled (false)

    onRelease = goBack,

        onPress = function()storyboard.state.mana = storyboard.state.mana - 1; storyboard.state.gold = storyboard.state.gold + 10;

 storyboard.gotoScene( “scene2”,“fade”,400 );end,

    }

    option1.x = display.contentWidth - option1.width/2

    option1.y = sceneObject.contentHeight - option1.height/2

    scrollView:insert( option1 )

You have to put the if statement within the onPress function

Oh wow :facepalm: thanks Jon that fixed it.

Im currently trying to figure out how to add text to the top of the NEXT scene based on what button you push. Any ideas? I assume i’d have to make a new text object but i’m not sure where to put it

ex.

local option1 = widget.newButton

    {

        width = display.contentWidth,

        height = 59,

        label = “Continue”,

    onRelease = goBack,

        onPress = function()

 storyboard.gotoScene( “scene2”,“fade”,400 );

local sceneObject = display.newText( thisIsNewText, 0,20, 250,700, “Times New Roman”, 20)end,

    }

Nevermind, got it :slight_smile: