Text Adventure Storyboard issues

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:

in your createScene code you’ll need to add the scrollview to the scene’s group:

group:insert(scrollView)

would that be instead of self.view?

No, like this:  

-- -- scene1.lua -- ---------------------------------------------------------------------------------- local widget = require( "widget" ) 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 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, } 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

Awesome that seems to have worked - it did create a second problem though. In my main.lua i have 3 buttons, one of which is supposed to start scene1. it now does that, but the buttons from main stay on the screen

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,

    }

Well, really there are a couple ways to fix it.  But since you are using storyboard then you should stick with the storyboard way:

This should be your button screen code:

-- -- -- ---------------------------------------------------------------------------------- local widget = require( "widget" ) 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. ----------------------------------------------------------------------------- -- 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, } group:insert(option1) -- other options local option2 = widget.newButton { top = 340, width = display.contentWidth, height = 59, label = "Help", onRelease = goBack, } group:insert(option2) local option3 = widget.newButton { top = 400, width = display.contentWidth, height = 59, label = "Quit", onRelease = goBack, } group:insert(option3) 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

Also, I would really suggest you read up on some storyboard tutorials so you can understand more about how it works.  Notice that I am adding all of the display objects (anything that gets displayed on the screen) to ‘group’.  Anything that you add to ‘group’ will automatically be handled by storyboard.