How can i Reset/Restart a .lua level/scene???

I have this “reset” button on my game and i want it to load the .lua again when the player touches the button, how can i do this?? i tried

director:changeScene(“level1”)

but it doesnt load it again, how can i do this? thanks! [import]uid: 10827 topic_id: 6891 reply_id: 306891[/import]

post some of your code, and we can see whats wrong.
Have you added an eventListener?
and have you required director at the top of your main.lua file?
here is how I would do it.

function button:tap()   
director:changeScene('level1')  
end  
button:addEventListener('tap', button)   
  

[import]uid: 24641 topic_id: 6891 reply_id: 24089[/import]

Or you can just reset your variables … :wink: [import]uid: 6981 topic_id: 6891 reply_id: 24093[/import]

this is what i have, doesnt work though :frowning:

[code]
local reset = display.newImage (“reset.png”)
localGroup:insert(reset)
reset.x = 305
reset.y = 15

local function pressReset (event)
director:changeScene(“level1”)
end

reset:addEventListener(“touch”, pressReset)
[/code] [import]uid: 10827 topic_id: 6891 reply_id: 24108[/import]

that code works good for me? [import]uid: 24641 topic_id: 6891 reply_id: 24113[/import]

If you look in the Ricardo’s director.lua file, you’ll see that it specifically prohibits changing into the same scene.

Excerpt:[lua] -----------------------------------
– If is the same, don’t change

if lastScene then
if string.lower(lastScene) == string.lower(nextScene) then
return true
end
end[/lua]

If you think about it logically, loading a scene actually loads the components into one group, and it’s supposed to load the components of the next scene into another group, then does a transition. So, it cannot physically load the same active scene twice.

What you theoretically can do is create a go-between scene that will loop back to the same level: create a global variable that will store the current level, pass it into another scene that will then quickly go back to that saved level.

level1.lua --> levelx.lua --> level1.lua
[import]uid: 6084 topic_id: 6891 reply_id: 24121[/import]

Create another restart lua file. Put it so that when you click it, it goes to that file, and then put a timer with 1000 miliseconds, so that in one second, it’ll transition back to the level 1 scene. (And that gets all placed in the restart file).

So here is what you need to do. For the current level 1 file. Make it so that when clicked, it goes to the restart file. And then in the restart file, put a timer and make it change scene to Level 1!

I had the same problem for the longest time and Rob Haney helped me out with it.

[import]uid: 19768 topic_id: 6891 reply_id: 24145[/import]

sounds awesome but is there no easier way? i have about 50+ levels in my game and i dont wanna have to make a reset file for each level lol [import]uid: 10827 topic_id: 6891 reply_id: 24173[/import]

You don’t need a separate reset file for each level. Just one.

Set up two global variables: goToScene and fxMode, and one lua file called levelrestarter.lua that contains the following:

[lua]function new()
localGroup = display.newGroup()
director:changeScene( goToScene, fxMode )
return localGroup
end[/lua]

If you already have 50 lua files of all your levels, just change the line that would restart the level to:

[lua]goToScene = “level1.lua” – or whatever level you want to restart
fxMode = “fade” – choose one of the many effects: crossfade, downFlip, flip, etc. or randomize
director:changeScene ( “levelrestarter”, fxMode )[/lua]

That should do it. [import]uid: 6084 topic_id: 6891 reply_id: 24200[/import]

Hi - This seems like the exact solution I am looking for. For some reason this isn’t working for me. MY main, levelrestarter, and level1 file look like this. please let me know where I’m going wrong. Thanks.

MAIN

display.setStatusBar( display.HiddenStatusBar )  
-- Import director class  
local director = require("director")  
  
-- Create a main group  
local mainGroup = display.newGroup()  
-- Main function  
local function main()  
  
 -- Add the group from director class  
 mainGroup:insert(director.directorView)  
  
 goToScene = "level1"  
 fxMode = "fade"  
  
 -- Change scene without effects  
 -- This sends user to the main menu  
 director:changeScene("screen1")  
  
 return true  
end  
  
-- Begin  
main()  

levelrestarter

function new()  
 localGroup = display.newGroup()  
 director:changeScene( goToScene, fxMode )  
 return localGroup  
end  
  

level1

[code]

local btRetry = display.newImage(“images/retry.png”)
local function btRetryTouch ( event )
if event.phase == “ended” then
Runtime:removeEventListener( “enterFrame”, moveSprite2 )
goToScene = “level1”
fxMode = “fade”
director:changeScene( “levelrestarter” )
end
end
btRetry:addEventListener(“touch”,btRetryTouch)
btRetry.x = 20
btRetry.y = 450
btRetry:scale( .1, .1 )
localGroup:insert(btRetry)
[/code] [import]uid: 32061 topic_id: 6891 reply_id: 24593[/import]

I think that this

function new()  
 localGroup = display.newGroup()  
 director:changeScene( goToScene, fxMode )  
 return localGroup  
end  

should look like this

function new()  
 localGroup = display.newGroup()  
 director:changeScene( level1.goToScene, fxMode )  
 return localGroup  
end  
  

Haven’t tested it though [import]uid: 24111 topic_id: 6891 reply_id: 27965[/import]

did anyone get this working? [import]uid: 24641 topic_id: 6891 reply_id: 28619[/import]

^^^^^^^^^^^^^^ anyone? [import]uid: 10355 topic_id: 6891 reply_id: 28647[/import]

I have this code in the levelrestarter.lua file

function new()  
 localGroup = display.newGroup()  
 print("levelrestater loaded")  
 print(goToScene)  
 director:changeScene( goToScene, fxMode )  
 return localGroup  
end  

In the terminal, “levelrestater loaded” and “level1” appear, so I know I’ve made it to this file, but the screen on the simulator is black and it doesn’t load the scene specified by goToScene.

Any ideas?

I have set up all the other variables and code stated in the earlier posts.

S [import]uid: 45932 topic_id: 6891 reply_id: 44742[/import]

Hello,

Director 1.3 now lets you change to the same scene

http://rauberlabs.blogspot.com/2011/07/director-class-13.html [import]uid: 8366 topic_id: 6891 reply_id: 44744[/import]

Do you really have a separate .lua file for each level? Isn’t that a lot of duplicate logic? [import]uid: 58455 topic_id: 6891 reply_id: 44759[/import]

No - you can modularise it

This is Ghost vs Monsters 2 - modularised by Jmp909

http://www.sendspace.com/file/rvtqr4

Described in this thread:

http://developer.anscamobile.com/code/ghosts-vs-monsters#comment-13627 [import]uid: 8366 topic_id: 6891 reply_id: 44763[/import]

That’s still a file per level and a lot of duplication. Unless the size, image, density, physics body, etc. for planks can vary, for example, there is no reason to specify anything but it’s location in the level data.

Create a PlankFactory.newPlank(x, y) function that contains all of that common plank creation code and just specify the location in the level data.

If you factor out all of the duplication, you may be able to keep all of your levels in one lua table. [import]uid: 58455 topic_id: 6891 reply_id: 44765[/import]

I’m away from my computer at the moment so can’t test any of this.

As evs says above “Director 1.3 now lets you change to the same scene”, is there not just a simple way of putting a function in each level which states “director:changeScene( “level1”)” (or “level2”, “level3” etc) that gets called when the restart button is pressed?

Can’t try this until tonight, but that’s what I’m guessing.

S [import]uid: 45932 topic_id: 6891 reply_id: 44768[/import]

Here’s what i did in the go-between file.

[code]
module(…, package.seeall)

function new()
local localGroup = display.newGroup()

function restartGame()
director:changeScene(“game”, “crossfade”)
end

local restartTimer = timer.performWithDelay(1000, restartGame)
return localGroup
end
[/code] [import]uid: 19835 topic_id: 6891 reply_id: 86573[/import]