Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

A little help with scenes?
Started by amar.masic6 May 22 2018 09:29 AM

2 replies to this topic
scene
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

amar.masic6

[GLOBAL: userInfoPane.html]
amar.masic6
  • Observer

  • 8 posts
  • Corona SDK

I don't know how else to explain this so I'll explain my entire game.

 

I have 4 scenes:

main.lua

menu.lua

game.lua

gameOver.lua

 

When main.lua gets loaded it automatically goes to menu.lua. Then you press the start button and you get to game.lua, there appears a text box that displays your score and a text box that displays how much time is left (according to a 60 second timer), when the time runs out a function is ran which goes like this: 

function gameOver()
scoreText:removeSelf()
timerText:removeSelf()
timer.cancel(ostaloVremena)
saveScore()
if(score > bestScore) then
bestScore = score
saveBestScore()
end
if(balloonSpawned == 1) then
balloon:removeSelf()
end
composer.gotoScene("gameOver", "slideLeft", 500)
end

Once you get to gameOver scene you have a play again button and two text boxes that display your current score and your high score.

 

Once you press the button it should take you back to the game.lua scene, and that works but how can I start my timers again and everything else I want to do?

 

I was thinking to somehow go to that scene and restart it so everything that's supposed to happen the first time you run it it's going to happen when you run it from the play again button, but how to do that?

 

Or to say it this way, I want it to restart the game but not to the point where the splash screen appears and to the main menu, just the game.lua scene.

 

I hope you understand what I mean, if any code is needed just say and I will put it here. Thanks in advance.



[TOPIC: post.html]
#2

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 25,385 posts
  • Enterprise

In your gameOver scene, just before you call:

composer.gotoScene("game")

add the line:

composer.removeScene("game")
composer.gotoScene("game")

That's the brute force way to restart a game. It's probably not the best practice, but the best practice would require you, in game.lua to basically hand reset anything that had moved or changed during game play. In your case, I'm not sure why you're removing all your display objects. If you are inserting them into the scene's view group, Composer will handle showing and hiding them. 

 

The brute force way is one line of code, which is appealing, but going that way, you're bypassing Composer's attempt to be efficient and cache the scene to avoid a view dozen lines of code to reposition everything, remove objects that need removed (items that don't exist at the beginning of the game), resetting scores, lives and their displays.

 

Rob



[TOPIC: post.html]
#3

amar.masic6

[GLOBAL: userInfoPane.html]
amar.masic6
  • Observer

  • 8 posts
  • Corona SDK

Thank you so much, Rob. Everything's working perfectly  :)




[topic_controls]
[/topic_controls]