How to replay current level?

I know this is probably easy, but I can’t get it to work.

When a level is completed, two buttons appear: Next Level & Replay Level.

I can get Next Level to work easily, but what about Replay Level?

For example, if I’m in level1 (level1.lua) and I use director:changeScene(“level1”), nothing happens. However, director:changeScene(“level2”) works fine.

How does one go about replaying a level using the Director class?
[import]uid: 13529 topic_id: 5724 reply_id: 305724[/import]

I don’t think Director class can do that.
I have a restart.lua file that just change back to the previous scene

It’s a bit slow. But if someone has a better way of doing it I’m all ears :slight_smile: [import]uid: 10657 topic_id: 5724 reply_id: 19620[/import]

Because of the way that Director was build, it’s impossible to load the same file with transition effects. In my games I have a function called initVars() where I initiate all the values for all variables. My restart button just calls it and everything begins with initial values. [import]uid: 8556 topic_id: 5724 reply_id: 19638[/import]

So, just making sure. I can’t do:

level1 --> level1 --> level2 --> level3

but if I separate my Level Complete routines to a different lua, I could do:

level1 --> levelComplete --> level1 --> levelComplete --> level2 etc

correct?

[import]uid: 13529 topic_id: 5724 reply_id: 19832[/import]

Right! [import]uid: 8556 topic_id: 5724 reply_id: 19835[/import]

Quick question, I working on an app that does card tricks. I have right now 3 card tricks and I give the user the option to replay the current trick. Do I need to make different lua file for each trick?

Example:

trick1 -->reloadtrick1–>trick1

trick2–>reloadtrick2–>trick2

or can I do this by passing a variable:

trick1–>reloadtrick–>trick1

trick2–>reloadtrick–>trick2 [import]uid: 11809 topic_id: 5724 reply_id: 19869[/import]

You can have a global variable with the level to load from reloadtrick. Each level will change the variable value. [import]uid: 11749 topic_id: 5724 reply_id: 19894[/import]

Sure!

But if you want it to be fast, you can create a function inside each file that put everything back to the original place and value. Something like this:

...  
--------------------------  
-- VARIABLES  
--------------------------  
  
local var1 = display.newImage("var1.png")  
localGroup:insert(var1)  
...  
local varN = display.newImage("varN.png")  
localGroup:insert(varN)  
  
--------------------------  
-- FUNCTIONS  
--------------------------  
...  
local function initVars ()  
 var1.x = 0  
 var1.y = 0  
 ...  
 varN.x = 0  
 varN.y = 0  
end  
...  
  
--------------------------  
-- NEW  
--------------------------  
  
function new ()  
 ...  
 initVars()  
 ...  
 return localGroup  
end  

To restart the level just call the function initVars() from a button:

... local function restartButtonFn ( event ) if event.phase == "ended" then initVars() end end ... [import]uid: 8556 topic_id: 5724 reply_id: 19904[/import]

Thanks Ricardo, that worked fine. The only problem I have is on the second trick, it disables the function of my menu button but not on the first trick. I know it has to do with the background graphic because if I remove it, it works fine. Must me an object order I need to find but thanks for the help. [import]uid: 11809 topic_id: 5724 reply_id: 20134[/import]

You can use the stage focus or remove the listener from the background image and add it back at the end of the button function. [import]uid: 8556 topic_id: 5724 reply_id: 20140[/import]

I’m not using a listener for the background. [import]uid: 11809 topic_id: 5724 reply_id: 20186[/import]

I found my mistake, I wasn’t releasing the objects, thanks for all your help Ricardo. [import]uid: 11809 topic_id: 5724 reply_id: 20205[/import]

Great! [import]uid: 8556 topic_id: 5724 reply_id: 20210[/import]

I am a newb and maybe I do something wrong, but I can totally go from my level1 to my level1 using dorector.

Although my moving objects go crazy, but I can load and play the level. I think, the objects just don’t get removed from memory, therefore they act crazy, but it was just a testing, so I don’t care. When I go to my level2 normally, then it is everything OK.

In the Ghosts vs Monsters game there is also a reload button and it is using director as well.

I use director.lua from that game. [import]uid: 6587 topic_id: 5724 reply_id: 20234[/import]

Recardo helped me with this a lot. My app goes like this

module(…, package.seeall)

local function initVar()
– All my Variables to reset

localGroup:removeSelf() – Used to reset the objects
new() – then call the new function to start screen over again
end

–Needed for Director
function new()
localGroup = display.newGroup()

–my app
return localGroup
end

This is what I did to clear them out. Hope this helps you. [import]uid: 11809 topic_id: 5724 reply_id: 20237[/import]

Hey can someone explain in better detail where the localGroup:removeSelf()
needs to go? i have mine all setup right, but when i click my button to reset all my variables i get an error, i realized that i didnt have this localGroup:removeSelf() anywhere, but im not sure where to put it? [import]uid: 19620 topic_id: 5724 reply_id: 23509[/import]

Ricardo, is the initVars() function just restating every object and their variables? [import]uid: 32061 topic_id: 5724 reply_id: 24474[/import]

Its my understanding that it does, you have that initVars function start and place all your variables with their values, and that way when you click your replay level then it can call the function again and place everything back where it should be. I still have not been able to get it to work, because somehow you need to clear all the old values but im not sure how to do it. [import]uid: 19620 topic_id: 5724 reply_id: 24667[/import]

This is weird, i finally got it so it at least is not giving me errors, when i click restart it puts me at the start of my level, but some of my objects do not reset, if i reset a second time then it seems that they are reset and where they should be… any suggestions?

These are physics objects, so i wonder if its having a hard time rebuilding them after my character object jumbles them all around

is there a way to completely remove the objects before placing them again with initvars()?

im assuming thats what removeSelf() is for but im not sure where to place it [import]uid: 19620 topic_id: 5724 reply_id: 24728[/import]

What worked for me is going to a new screen with two options… restart and home. the restart uses a global variable which is set in whatever level i was just in. [import]uid: 32061 topic_id: 5724 reply_id: 24730[/import]