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]
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]
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?
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]
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 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]
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]
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]