Reset/reload function works only one time

  1. This is my new MVP, you know, I struggle to create the most interesting supergame in the world.

  1. The game process is shown at this picture, is it clear or no? Let me say a couple of words about… Hah, it’s the good ol’ find 'em all of these hidden bastards game! If you find an alien, press immediately a same color buttoon to find another one. Etc.

  2. Okay, but it’s not that easy to play, ¿de acuerdo, my dear Solarians?

  3. So, sometimes you will need to press the reset/reload scene button (it is a white square with a red round anti-clockwise arrow).

  4. This is my code for this case.

    Handle.Reset_Button = function (e)
       if (e.phase == "ended") then
           local currScene = composer.getSceneName ("current")
           composer.removeScene (currScene, true)
           composer.gotoScene ("level1234")
       end
    end
    
    Buttons.resetButton = widget.newButton (
     {
     sheet = Sheets.ui,
     defaultFrame = 1,
     overFrame = 2,
     onEvent = Handle.Reset_Button
     }
    )
    
  5. But, unfortunatelly, onEvent function works only one time, after the scene “level1234” is reloaded it doesn’t work and the game freezes!

  6. Is it enough to set the composer.removeScene (currScene, true) or I have to include to the Handle.Reset_Button removing all objects and all eventListeners?

  7. I tryed to follow Corona/Solar docs, I’ve found lots of topics about this issue (however, a lot of topics concerning Storyboard and Director, agrrr)… And my question, of course, is: HOW to reset a scene in a correct way nowdays. It could be perfect to see a step-by-step video of the really programmed and working example.

  8. Better Call Rooooob!, season 14, episode 9 :wink:

:smiley: I’m having great fun while reading your posts. Dig the style.

Anyway. Instead of composer.removeScene, I’d suggest another approach. Instead of removing the current scene and calling it again in the same function, I’d suggest a simple transition screen. Here is an example project.

example.zip (1.5 KB)

Not the best code or the best approach but works for me. You may need to change some parts to fit your need though.

1 Like

Thank you, @bgmadclown, I’ll be dig.

P. S. @bgmadclown, I like your “example”. I added a little local square in the center of background at gameScene and anotherGroup:insert (square). Then I added transition.to (square, {time = 1500, x = math.random (0, display.contentWidth), y = math.random (0, display.contentHeight)}). Your method works perfectly. But, if the random square.y = display.contentHeight, after reloading scene I can see a half of square left at the bottom of the scene. I might have to experiment more!

1 Like

Another option is to never leave that current scene. Move all of your code that sets up the scene out of the create function, and that way you can call it again when you want to reset the scene. Ditch all of the created objects and then call your set up function again when you want to try again.

I try to never put much code at all inside of the create, show, hide composer functions. That way I have options available to me.

4 Likes

@J_A_Whye one more question, would you clarify for me the flow of your method with an example or a visual scheme (cause I prefer Visual Learning Style, not Kinesthetic)

Could I say that I understood you by one of this flow tables?


OR

function scene:hide( event )
	local sceneGroup = self.view
	local phase = event.phase

	if event.phase == "will" then
	elseif phase == "did" then
		local previous = composer.getSceneName("previous")
		if previous ~= "main" and previous then
			composer.removeScene(previous, false)
		end
	end
end

This is the code I used to process the scene.

Remember to clean up all objects in the scene every time.

This code, you should be able to restart from “scene:create” every time.

1 Like

@vb66r55 t_h_a-n_k_s_a_l_o_t_!

Sorry for the delay. Looks like you got an answer to your problem, but for anyone else who happens by, this is what I meant:

local function setUpDisplay(sceneGroup)
    -- create all display objects and such here
end

function scene:create(event)
    setUpDisplay(self.view)
end

Basically, the less code inside the scene functions, the better I like it – the more options I have later on.

1 Like

@J_A_Whye
No problemo, I am still genuinely interested in the all possible methods. And, I’m sorry too for my irremediable idiocy. I can not catch this one (the last, but not the least) moment. How to reload a scene, after the reload button’s line: if (event.phase == ended)… Should I call setUpDisplay() function again?

Yes, you’d call SetUpDisplay() again, but usually after making sure to kill off whatever you created originally. For example, I have routines to kill any leftover enemies, plus anything else that might need to be recreated (level mazes, etc.). The easiest way to do that is put all enemies inside a specific display group, background images in a specific display group, and so on. Then just ditch those groups.

1 Like

@J_A_Whye, thank you

Thank you so much. The solution was helpful to a great extent.