back to previous scene (with storyboard)

Ok, this error is because you have a spelling (case) issue.

In both scenes you have defined your image as “playbutton” but in the group:insert method you are spelling it “playButton”. The “B” is what is causing your errors. Lua is case sensitive.

You should also watch out for global variables. Your playbutton is not local so you may want to define it with local.

Change your code to:

local playbutton = display.newImage("ene5.png") playbutton.x = 190 playbutton.y = 400 playbutton:addEventListener( "touch", back ) group:insert(playbutton) -- [import]uid: 190375 topic_id: 34428 reply_id: 137005[/import]

I just have one last question, if I want to re-create the scene, for example. In the code show:
SCENE1.LUA
[lua]local storyboard = require( “storyboard” )
local scene = storyboard.newScene()
local playButton = display.newImage(“ene5.png”)
local function back ()
storyboard.gotoScene( “scene2”, “slideLeft”, 800 ) --end
local variable = 3
local function menos ()
variable = variable -1
print(variable)

end

timer.performWithDelay(1,menos, 1)

– put all scene related graphics and sound creation here if you want them created before the
– scene transitions on screen. Must be at least one thing created here.

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

playButton.x = 190
playButton.y = 260
playButton:addEventListener( “touch”, back )
group:insert(playButton) –
end


– Put things here that need created after the scene is on the screen, like timers, audio voice overs,
– runtime listeners, etc. Anything you create here like timers and runtime listeners must be removed
– in exitScene. For most scenes you don’t need to do anything here.

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[/lua]

The variable = 3, however, comes down to two by a function, if I want to scene1, the variable is still worth 2 and does not rise to 1, not if I explain. Any suggestions? [import]uid: 98258 topic_id: 34428 reply_id: 137006[/import]

not reset the scene [import]uid: 98258 topic_id: 34428 reply_id: 137007[/import]

@aitor.10794 please move line #3 inside your createScene() function. The way Lua loads modules, that will create the image, but if this scene ever gets purged due to low memory or some other reason, that code at the top will NOT be re-executed. You should not create anything outside of either createScene() or enterScene() and you should not initialize any variable outside of createScene() or enterScene() that would need to be initialized if that scene gets removed for some reason.

It’s okay to have functions that create things and do things outside of createScene() and enterScene() as long as those functions are called from inside of createScene() or enterScene();

[import]uid: 199310 topic_id: 34428 reply_id: 137019[/import]

@aitor.10794 - Are you saying you want the value in “variable” to be passed from scene to scene?

If that is the case you could use Parameter Passing or take advantage of the fact that you will be requiring Storyboard for each of your scenes and pass the data through Storyboard - details here: http://www.coronalabs.com/blog/2012/08/07/managing-state-between-scenes/

In each scene during createScene you can access the valus, update as needed and save them back to your Storyboard variable or pass it along. [import]uid: 190375 topic_id: 34428 reply_id: 137023[/import]

but if I have a variable life and I want when I return back to the initial state lives (3 lives) how do I do? Currently in my real project I still ticking 0 life. [import]uid: 98258 topic_id: 34428 reply_id: 137033[/import]

Resume:

game.lua

lives = 3

when my sprite dead, is 0 live = gameover.lua

I press restart and go to game.lua, but in game.lua remains 0 lives and not 3 lives (Not restart)

This is my grand problem :frowning: [import]uid: 98258 topic_id: 34428 reply_id: 137034[/import]

aitor.10794 - You need to set it back to 3 when ever is appropriate. Thus if you have a reset button then you would set this variable back to 3 and pass it back or save it back to the Storyboard variable you are using or if you want it always reset to 3 when you restart the game then in your main.lua file you would set it there and pass it. [import]uid: 190375 topic_id: 34428 reply_id: 137035[/import]

I reset in case the variable “Lives”. Reiniciar_partida function included in the following. In gameober.lua

[lua]local function reiniciar_partida ()

display.remove(nickText)
display.remove(background)
display.remove(botonmenu)
display.remove(botonenviar)
display.remove(scoreText)
lives = 3
score = 0
storyboard.gotoScene( “game”, “slideLeft”, 800 )

end[/lua] [import]uid: 98258 topic_id: 34428 reply_id: 137036[/import]

but still not working… :’( [import]uid: 98258 topic_id: 34428 reply_id: 137037[/import]

Well when and how are you calling this function? How are you passing the value of lives back to the start? Is this a global variable? If not then you are just setting lives to 3 locally and it will not persist.

You need to pass this value back to the start one way or the other. Either through Storyboard or through a global variable. Obviously using locals and passing values is better than a bunch of global variables. [import]uid: 190375 topic_id: 34428 reply_id: 137040[/import]

Ok, this function is called from a addEventListener. The variable is global score but just now I realize that always happens with value 0 (even really the value 4 for example).
So how I can pass the variable to gameover.lua can score reset after? (Like the variable lives)
What would be the correct way? (as I see it with a global variable either) [import]uid: 98258 topic_id: 34428 reply_id: 137042[/import]

really involves passing score to gameover.lua variable and then press the button when restart set the variable to 0 and the score to 3 lives. [import]uid: 98258 topic_id: 34428 reply_id: 137043[/import]

Does gameover.lua use the storyboard system?

When something happens to trigger game over then I assume you go to a scene called “gameover”? Then in game over you will show a button to restart or something? So there you could set the variables for score to 0 and lives to 3 and go to game start (scene1?).

Thus you just need to set the variables as shown in the link I posted above for passing variables, for example…

in main.lua

storyboard.state = {}
storyboard.state.score = 0
storyboard.state.lives = 3

in gameover.lua

storyboard.state.score = 0
storyboard.state.lives = 3
storyboard.gotoScene( “game”, “slideLeft”, 800 ) [import]uid: 190375 topic_id: 34428 reply_id: 137044[/import]

It is exactly as you think.
Then on this basis. I do the following in game.lua?
_G. score = Storyboard.state.score
and
Local lives = storyboard.state.lives???

I apply what I’ve said in my variables? (I’m a bit awkward in this respect)
Thanks for your help! [import]uid: 98258 topic_id: 34428 reply_id: 137046[/import]

Assuming game.lua is a storyboard scene you could access the lives as such:
local lives = Storyboard.state.lives
local score = Storyboard.state.score

During some action in game.lua

lives = lives - 1
score = score + 10
or whatever…

Then before you transition to another scene make sure to save these back:

Storyboard.state.lives = lives
… etc.
[import]uid: 190375 topic_id: 34428 reply_id: 137049[/import]

I did a print and tells me it’s score is nil. Anyway tomorrow I will write back the 1600 lines of code because right now I have a mess … Anyway it may be that nil? [import]uid: 98258 topic_id: 34428 reply_id: 137051[/import]

The print was in gameover.lua [import]uid: 98258 topic_id: 34428 reply_id: 137052[/import]

I think you need to stop copy and pasting code and just understand how to write it yourself first. How can you have 1600 lines of code and yet not understand the basics of variable scope?

You seem to be ahead of yourself. I love to help but I’m not going to write the game for you. [import]uid: 190375 topic_id: 34428 reply_id: 137053[/import]

apology. I explain wrong, sometimes I have trouble enough English to express myself rather well. I meant that I did not understand what the storyboard step variable from one scene to another, I know that the global can and does until yesterday I did well, what happens is that both modify the code to finish rolling. 1600 lines are just so many features that I returned to copy the same code (saving). Anyway, it’s true that I’m starting and not yet mastered this language very well. There are things that escape me and I try to find a complete online course but I find it difficult to find, only found the reference guide and little else. Tomorrow rewrite the code from scratch more than anything to get it right from the start and not so wrong. Thanks anyway to you and to rob. Coming to schedule picking fluency so these days if I have a problem I write (I hope not to have them!).

Thanks! :wink: [import]uid: 98258 topic_id: 34428 reply_id: 137060[/import]