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)
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.
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.
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]
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]
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]
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]
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]