I didn’t intend for it to be used in such a way, and I’m not sure if there are any unwanted side-effects.
However, if it works, it works. Although I don’t understand why it works ;).
I didn’t intend for it to be used in such a way, and I’m not sure if there are any unwanted side-effects.
However, if it works, it works. Although I don’t understand why it works ;).
Tried it. It’s the same as above, I need scene1 to be removed only if relative parameters get changed and those I only managed to get checked in “hide scene2”.
So if I go ahead and try as you say basically the scene gets detroyed and reloaded regardless of whether something was changed or not in the option screen.
This I’d rather avoid since there’s a specific visual effect I’d like to get: if appareance of scene1 is set to be changed in scene2 once I get back from scene2 to scene1 everything gets ‘rebuilt’ with transitions and such, with a different visual layout. If nothing is set to be changed it just gets back to scene1 smoothly as if scene2 was a sort of overlay (which is not).
The gotcha resides in the fact that the buttons in scene2 are designed to pass a parameter which is a result of a comparison of the state of the buttons when scene2 is entered and when it’s exited.
Hope I got clear enough… Ultimately, the problem is I’m a noob at programming i guess.
ahah. Yes, I’m sorry if I’m ‘bending’ the intent of your template to fit my purpose, but hey, if it works in the end, as you say it’s a win anyway
Thanks a lot for the help!!!
Then if you don’t need to remove the scene all the time:
if userDidSomethingToRequireASceneChange then
composer.removeScene(“scene1”)
end
composer.gotoScene(“scene1”)
Rob
Thanks Rob!
As I was discussing with ingemar the matter of the question at this point is where to put something as
if userDidSomethingToRequireASceneChange then
composer.removeScene(“scene1”)
end
composer.gotoScene(“scene1”)
If put inside of ‘scene hide’ of scene2 it just won’t work. If put in ‘scene show’ of scene2 it wouldn’t have any info regarding ‘userDidSomethingToRequireASceneChange’ 'cause those paramaters can be checked only inside of the ‘hide scene2’ function.
That’s basically the catch…
But thanks a lot anyway!!!
Do not put it inside of scene:hide. It’s too late then. You have to have something, a button or trigger that says to go back to scene1. You do this inside whatever is handing that button event.
Rob
Exactly. But that would be too early, cause the function that handles the button event (which triggers go to scene1) doesn’t have the parameters yet to determine whether the scene ( 1 ) needs to be removed or not, because those determining parameters are checked and passed in ‘hide scene2’.
Why do you have to delay the parameters until the hide phase? Won’t know you when the user selects something to change?
I hope you don’t mind, but personally I think the logic is a bit off.
I’d remove all checking of parameters from the hide event and put it into its own function. Then on every parameter-button in scene2 that affects scene1 I’d add an event-handler that calls the new parameter-checking function.
It’s cleaner, and that way you don’t need to use reload.lua. You could then use one of the solutions Rob has given you above.
Wow. This is just a real bummer. C’mon Team Corona!!! Make it happen please!!
Love,
The Fella
I moved the ‘check phase’ inside of ‘create scene2’ where everything else about the scene is.
Also made the back button of scene2 to manage whether to simply go back to scene1 if the parameters didn’t change (working), or remove scene 1 and go to scene 1 (rebuild the scene) if some changes occurred (not working).
In this latter case the screen got frozen on scene 2, no error returned. I can hear a specific sound effect of entering scene1 and from this I infer that the parameters have been passed properly and scene1 got loaded, but scene2 is still hanging on the screen. Here’s the function I use to go back to scene1 (it’s triggered by pressing the back button)
--- we're in scene2 and just hit the back button: local function gotoPlay () --- "Play" would be scene1 if didSTchange == didSTchangeout --- a comparison to determine if changes have been made... composer.gotoScene ("Play", "zoomInOutFade", 150) else composer.removeScene ("Play") composer.gotoScene ("Play", "zoomInOutFade", 150) end end
Any idea regarding the frozen screen?
I don’t believe this is a bug. It’s just not how you want it to work. This same issue was present in Storyboard which lead me to do the video tutorial on Reloading Storyboard:
http://coronalabs.com/blog/2013/08/20/tutorial-reloading-storyboard-scenes/
The gist of it is when a scene is on the screen, it cannot be destroyed and re-created… plain and simple. If it did, there would be nothing to show.
Reloading a scene does not generally require the scene to be recreated. It simply needs reset. Keeping in mind any code run (initializing variables, starting physics, etc.) that is in the scene’s main chunk only executes when the scene is “required” for the first time, so:
local livesRemaining = 10
at the top of your module won’t reset unless you’re able to call composer.removeScene() and you shouldn’t try and remove the scene you’re in. The solution? Use a cut scene and remove your game scene while the cut scene is showing. This is a very common design pattern among games… pacman, angry birds, candy crush, etc. If you don’t mind the flash, you could have an all black cut scene that simply removes the game and goes back to it as quickly as possible.
However the **right** thing to do is to use your scene:show()'s “will” phase and reposition your objects that may have moved back to their starting location. A pain, yes, but if you’re doing:
player = display.newImageRect(“player.png”, 100, 100(
player.x = 100
player.y = 100
you are already writing the position code, just move those last two lines to the “will” phase and don’t worry about positioning them in scene:create().
For the last round of issues, you cannot call gotoScene() in a scene’s scene:hide() event because gotoScene is what triggers the scene:hide(). If you plan to use the sceneRemove() technique to reset a scene, then it has to happen before calling gotoScene() because it’s too late to remove the scene that gotoScene() is trying to go to.
I’m developing a multi-level game right now using the composer API. I’m using the cut scene method to show the level score, number of stars, etc. and I can load my game scene over and over and over. Create a scene that says “Try again”, show that message for a couple of seconds while you’re removing the game scene and go back to it. Even better give your user a choice to try again or quit (goto your menu, etc.) from that cut scene.
Rob
Now for your latest issue. It sounds to me like scene1 is crashing for some reason. While you may not have gotten a popup message, there should be something in your terminal/cmd window that says what the issue is. That’s usually the cause of black screens and since you have an audio hit that your scene:create() is getting called, I think we need to look there. Perhaps one of your parameter changes is causing the scene1 to error out. Without seeing the code, we can only speculate.
As for the audio, you probably should delay audio playing until the scene has transitioned on the screen i.e. start it in scene:show()'s “did” phase.
Rob
Hi Rob,
Thanks for the thorough explanation. I’ll look into every of the possible alternate solutions you are proposing.
I tried already moving to a ‘bridge’ scene in between scene2 and scene1. It was a blank scene containing only the command ‘gotoscene1’. It works, I can take the ‘to black screen’ transition per say, but it only works for the first time. The second time I go to scene2 and then back, within the same app session, it returns still-black screen. No error returned. Is it just a matter of delaying a bit the call of scene1? If so, how long would be the ideal time??’
By the way I’m not insinuating this is a corona’s bug. At the level I’m programming I don’t have any ground to say what’s what. I Just came across this topic while looking for a solution to my ‘design’ problem, and the reload.lua is working great so far.
So for now I will keep that as a solution, if problems arise while testing on device I’ll let you guys know.
ingemar’s template works like a charm in my project: I also have a third scene in my program, let’s call it scene0. It’s an intro screen and it runs when application starts, it leads straight to scene1, then it’s just bouncing between scene1 and scene2.
But in scene2 you got a button that calls scene0 in order to rewatch the intro. Now, with ingemar’s code called in hide scene2, if I change option parameters and instead of going back straight to scene1 I go to scene0, when the intro ends and the program redirects to scene1 this gets reloaded properly with the adequate changes that had been set two scenes before. Just amazing!
There has to be an error. You are not by any chance using a variable named “debug” are you? “debug” is a global variable that’s used by the output system generate messages to the console log.
You can also put in some print statements in your scene’s create and show functions to see how far you’re getting too.
Rob
I’m not using any variables with that name or similar. But I realize now might the incongruence in the functioning of my program be due to the version of the corona version I’m using??? :
Version 2014.2189 (2014.3.6)
The current public build is 2393 for Windows and 2393a for Macs. You should update for certain.
Rob
I am using 2393a on my Mac, i did observe that reloading 2nd time doesnt trigger scene:create, scene:hide at all.
this is the flow -
-create scene called
-scene showed
-in a button handler - removeScene(current) , gotoScene(current)
for the first time, scene:hide got triggered, but not second time.
You can’t remove the current scene. It’s still on the screen. It’s best to either remove a scene just before you go to it, or remove a scene after already in the next scene. With a little work, you don’t have to remove the scene. Using the “show”, “will” phase, you can reposition things to their start point. You should not get a create if the scene hasn’t been removed. The lack of a hide could be because you are trying to remove the current scene.
Rob
I’m having similar issues as seen in this topic-switching scenes, reloading, etc… But my code may not be setup correctly…
Majority of the code lies within the ‘Scene:Create’ part of the composer scene. Should any of this code be placed outside of the Scene Composer functions? If I’m not going BACK to the scene after removing - the scene works fine.
Then in the ‘Scene:Destory’ section - I have the removal of all my eventListeners. And the Button removal/nil because it’s a widget (which oddly still does NOT get removed… Tho I have same exact code in Menu for different button that goes to Level1 and that gets removed…??)
So I have a button in Level1 that 'removes “Level1” then goToScene"menu" when button is released… and vice versa for the menu button.
I’m not sure exactly how the code looks now as I’m away from computer with the code.
The issue (and I’m sure it has to do with the garbage collection) is when I go from Level1 back to the Menu. My tap event listener is still on from level1 tho I removed it in the ‘scene:destory’ function. This causes some havoc as it’s creating things that shouldn’t be while in the Menu portion that is directly from Level1. I even tried removing the eventListener within the menu scene - or removing level1 while in menu scene:start and nothing.
Everything else seems to work. If I start from Menu – nothing happens when I touch outside of button. When I press Menu button to go to Level1 - it’s a smooth transition, things load correctly - no abnormalities in Level1. When I hit button to go back to Menu - and I press somewhere on screen that’s not the button to go back to level1 - a touch event is triggered from level1. I have no touch/tap event listeners in Menu.
Another thing - I have a memory tracking function that prints memory usage that I found on this site. The memory stays relative with no spikes when I go from Menu --> Level1 and even back to Menu. However when I go back to Level1 again - the memory load spikes up quite a bit.
Aghh any help / direction would be appreciated, been a nightmare for past few days