Restart code

Hi,

Is there a simple restart code that does not include a timer? I just want to click the menu button and start again.

Appreciate any help

What do you mean by restart code? Restart what?

If you need to restart your game/scene/etc. then you can do it in whatever way you want, no timer needed.

Sorry I should have wrote would you be able to show me the code for restarting a level? No timer involved.

Thanks

In theory, if you just want to restart the level, then you need to click on the “button” using the composer, go to the stage with the beginning of the level.
Do you have a game consisting of several scenes or of one? And you know how to use the composer?

You are asking for a very project specific question without providing any information on your project and how you’ve created your levels.

To answer your original question again, no, you don’t need to use a timer. All that a timer does is call a function once the allotted time has run out. You can just as easily put that function call to a button or anywhere else in your code.

How to restart a level is again a very project specific question. There is no one-size fits all code solution for something like restarting levels. If you are using Composer and your levels are just separate Lua files without the need for any other code or initialisation parameters, then you could just exit the current scene, completely remove the old scene and return to the scene. In any other case, no one can answer you without having a detailed look at your code.

This is sort of like asking: how to program a level when you don’t know if the person asking the question is working on an FPS, a text adventure, an MMO, match-3, etc.

Hi, I have a menu page with 2 buttons, a play game and a options button. Play game scene is a ball falls from the top to the bottom. The problem is when I go back to the menu page and then back to the Play game scene the ball is still at the bottom. This is simple beginners code and I’m trying to learn composer scene. Would you be able to write the code that would reset the Play game scene? Oh, you have to tap the ball to make it fall.
Thanks in advance

When you go from the menu scene to the game scene, are you using composer.removeScene on your game scene prior to transitioning to it?

local function playButton( event )
	if ( "ended" == event.phase ) then
		composer.removeScene( "gameScene" )
		composer.gotoScene( "gameScene" )
end
	return true
end
  1. Colinmorgan beat me to it:) the code he wrote allows you to change scenes; removeScene (“gameScene”), where “gameScene” is the name of the scene you are currently on, and gotoScene (“gameScene”) - “gameScene” is the name of the scene you are moving to. Try using this method.

  2. If the scene still doesn’t reset, make sure that the scene objects are added to sceneGroup.
    Here are some examples:
    a)

local ObjOne = display.newRoundedRect(sceneGroup, display.contentCenterX,display.contentCenterY, 220, 220, 0)
ObjOne:setFillColor(0.5)  

b)

    buttonGo = widget.newButton {
    shape = 'Circle',
    radius = 0,
    width = 45, height = 30, 
    left  = 200, top = 100, 
    fontSize = 12,
    fillColor = { default={ 1 }, over={0} },
    labelColor = {default={0}, over={1} },
    label = "Go",
    onPress = function(event)

        composer.removeScene("scene1");
        composer.gotoScene("scene2")   

            end
        }

    sceneGroup:insert(buttonGo)

In this way you can add objects to sceneGroup as you like.
Yes, and it seems you need to add at the beginning of the code
local sceneGroup = self.view;

Hope this one was helpful! If anything, correct it)

Try downloading this example set: https://github.com/roaminggamer/CoronaGeek/raw/master/Hangouts/composer_scene_manager.zip

Run example 12 ==> 12_rebuild_playgui

From, my old CoronaGeek help content: https://github.com/roaminggamer/CoronaGeek

OFF TOPIC: You may generally find these useful too (lots of stuff to dig through)…

RG Free Stuff https://github.com/roaminggamer/RG_FreeStuff

  • AskEd Folder - Forums answers of the years
  • Products - Free samples and templates (see this thread for images: Template Portfolio Release)

Super Starter Kit -https://github.com/roaminggamer/SSK2
Docs: https://roaminggamer.github.io/RGDocs/pages/SSK2/

Hi, I tried yours colinmorgan and Chepkin’s but I get errors messages, so I started over again. There’s the 3 usual scenes and now only a menu and a game scene, I’m only using png’s it’s easier for me. I’m probably not suppose to do this but here’s my code I tried to keep it clean. Thanks sorry.

– main –

– hide the status bar
display.setStatusBar( display.HiddenStatusBar )

– include the Corona “composer” module
local composer = require( “composer” )

– load menu screen
composer.gotoScene( “menu” )


– menu –

local composer = require( “composer” )
local scene = composer.newScene()

– include Corona’s “widget” library —
local widget = require (“widget”)


local playBtn1
local function onPlayBtn1Release() – ‘onRelease’ event listener for playBtn —
composer.gotoScene( “game”, “fade”, 500 )
return true – indicates successful touch —
end

local playBtn2
local function onPlayBtn2Release() – ‘onRelease’ event listener for playBtn —
composer.gotoScene( “options”, “fade”, 500 )
return true – indicates successful touch —
end

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

local background = display.newImageRect( "background.png", display.actualContentWidth, display.actualContentHeight )
background.anchorX = 0
background.anchorY = 0
background.x = 0 + display.screenOriginX 
background.y = 0 + display.screenOriginY 

playBtn1 = widget.newButton{

	label="Game",
	labelColor = { default={ 0, 0, 0 }, over={255/255, 0, 0} },
	defaultFile="buttonDefault1.png",
	overFile="buttonOver1.png",		
	width=200, 
	height=30,
	onRelease = onPlayBtn1Release 
}
playBtn1.x = display.contentCenterX
playBtn1.y = display.contentHeight - 190

playBtn2 = widget.newButton{

	label="Options",
	labelColor = { default={ 0, 0, 0 }, over={128} },
	defaultFile="buttonDefault2.png",	
	overFile="buttonOver2.png",			
	width=200, 
	height=30,
	onRelease = onPlayBtn2Release	
}
playBtn2.x = display.contentCenterX
playBtn2.y = display.contentCenterY + 20

sceneGroup:insert( background )
sceneGroup:insert( playBtn1 )
sceneGroup:insert( playBtn2 )

end

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

if ( phase == "will" ) then

	-- Called when the scene is still off screen and is about to move on screen --- 
elseif ( phase == "did" ) then
	-- Called when the scene is now on screen --- 
	-- 
	-- INSERT code here to make the scene come alive --- 
	-- e.g. start timers, begin animation, play audio, etc. --- 
end	

end

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

if ( event.phase == "will" ) then
	-- Called when the scene is on screen and is about to move off screen --- 
	--
	-- INSERT code here to pause the scene --- 
	-- e.g. stop timers, stop animation, unload sounds, etc.) --- 
elseif ( phase == "did" ) then
	-- Called when the scene is now off screen --- 
end	

end

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

-- Called prior to the removal of scene's "view" (sceneGroup) --- 
-- 
-- INSERT code here to cleanup the scene ---
-- e.g. remove display objects, remove touch listeners, save state, etc. ---

if playBtn1 then
	playBtn1:removeSelf()	-- widgets must be manually removed ---
	playBtn1 = nil

elseif playBtn2 then
	playBtn2:removeSelf()	-- widgets must be manually removed ---
	playBtn2 = nil
end

end

– Listener setup —
scene:addEventListener( “create”, scene )
scene:addEventListener( “show”, scene )
scene:addEventListener( “hide”, scene )
scene:addEventListener( “destroy”, scene )

return scene


– game –

local composer = require( “composer” )
local scene = composer.newScene()

– include Corona’s “widget” library
local widget = require (“widget”)

– include Corona’s “physics” library
local physics = require (“physics”)
physics.start()

– forward declarations and other locals
local screenW, screenH, halfW = display.actualContentWidth, display.actualContentHeight, display.contentCenterX

function scene:create( event )
local sceneGroup = self.view
– Called when the scene’s view does not exist.

– INSERT code here to initialize the scene
– e.g. add display objects to ‘sceneGroup’, add touch listeners, etc.

-- We need physics started to add bodies, but we don't want the simulaton
-- running until the scene is on the screen.
physics.start()
physics.pause()

local function onMenuTouch(event)
		if(event.phase == "ended") then
			composer.gotoScene("menu", "slideRight")
		end
	return
end

local background = display.newImageRect( "background.png", display.actualContentWidth, display.actualContentHeight )
background.anchorX = 0
background.anchorY = 0
background.x = 0 + display.screenOriginX 
background.y = 0 + display.screenOriginY

local menuBtn = widget.newButton(
{
label=“Menu”,
labelColor = { default={128}, over={128} },
width = 55,
height = 20,
defaultFile = “homeBtnDefault.png”,
overFile = “homeBtnOver.png”,
onEvent = handleButtonEvent
}
)

menuBtn.x = display.contentCenterX -250
menuBtn.y = display.contentCenterY -140
menuBtn:addEventListener(“touch”, onMenuTouch)

local redBall = display.newImageRect( “redBall.png”, 25, 25 )
redBall.x = display.contentCenterX - 150
redBall.y = display.contentCenterY - 120
physics.addBody( redBall, “dynamic”, { density=1.0, friction=0.5, bounce=0.1, radius=12 } )

local greenBar = display.newImageRect(“greenBar.png”, 60, 10)
greenBar.x = 90
greenBar.y = 100
physics.addBody( greenBar, “kinematic”, { density=1.0, friction=0.3, bounce=1 } )

local blackBar = display.newImageRect( “blackBar.png”, 70, 10 )
blackBar.x = 90
blackBar.y = 300
physics.addBody( blackBar, “static”, { bounce=0.2 } )

sceneGroup:insert( background )
sceneGroup:insert( menuBtn )
sceneGroup:insert( redBall )
sceneGroup:insert( greenBar ) 
sceneGroup:insert( blackBar )

local function doRectTouch( event )
local touchedObject = event.target

        if (event.phase == "began") then
            display.getCurrentStage():setFocus( touchedObject )  
            touchedObject.previousX = touchedObject.x
            touchedObject.previousY = touchedObject.y
            print("greenBar touched")
	-------------------------------------------------------
        elseif (event.phase == "moved") then 
            touchedObject.x = ( event.x - event.xStart )  + touchedObject.previousX
            print("greenBar moved")
	-------------------------------------------------------
        elseif (event.phase == "ended" or event.phase == "cancelled") then
            display.getCurrentStage():setFocus(nil) 
            print("greenBar released")
end
	return true

end
greenBar:addEventListener( “touch”, doRectTouch )

local function greenBarTapped( event )
transition.to( greenBar, { rotation=0, delta= true } )
end
greenBar:addEventListener( “touch”, greenBarTapped )
end

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

if (phase == "will") then
	-- Called when the scene is still off screen and is about to move on screen
elseif (phase == "did") then
	-- Called when the scene is now on screen
	-- 
	-- INSERT code here to make the scene come alive
	-- e.g. start timers, begin animation, play audio, etc.
	physics.start()
end

end

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

if (event.phase == "will") then
	-- Called when the scene is on screen and is about to move off screen
	--
	-- INSERT code here to pause the scene
	-- e.g. stop timers, stop animation, unload sounds, etc.)
	physics.stop()
elseif (phase == "did") then
	-- Called when the scene is now off screen
end	

end

function scene:destroy( event )

-- Called prior to the removal of scene's "view" (sceneGroup)
-- 
-- INSERT code here to cleanup the scene
-- e.g. remove display objects, remove touch listeners, save state, etc.
local sceneGroup = self.view

package.loaded[physics] = nil
physics = nil

end

– Listener setup
scene:addEventListener( “create”, scene )
scene:addEventListener( “show”, scene )
scene:addEventListener( “hide”, scene )
scene:addEventListener( “destroy”, scene )

return scene

Thanks again

Try replacing your onPlayBtn1Release function with this function:

local function onPlayBtn1Release( event )
	if ( "ended" == event.phase ) then
		composer.removeScene( "game" )
		composer.gotoScene( "game", "fade", 500 )
  end
	return true
end

It should work if you just copy and paste that snippet over your existing function. If it doesn’t work, knowing exactly what error you get would be helpful in diagnosing the problem.

On a side note, you should always make functions that are associated with buttons an ( event ) and tie the action to an event phase. Otherwise, tapping the button can cause the function to trigger multiple times.

OMG IT WORKS!! YOU’RE A GENIUS!! Don’t go away, because I have so many more questions.

Thank you so much

Would it be alright if I ask you a few more questions?

That’s perfectly alright, but I would highly recommend that you start a new topic and ask your question there.

This topic has been inactive for almost a year and it ended with your issue being resolved.

My mother passed away last July and this is the first time I’ve been back. I was trying to make a little game, but I got stuck on a few things and then it was too late.

I’m sorry to hear about your mother.

Just remember that you never need to ask for permission to ask for help in the Solar2D community. There’s a lot of helpful people here, willing to share their knowledge.

All I’m saying is that whenever you have an entirely new question or you have a question and the thread has been inactive for several months, that you start a new topic. You’re welcome to make as many new topics as you need.