[Solved] Director and difficulty changing back to original scene

Trying to learn Director and creating a sample template and I am having some difficulty getting back to the main menu after transitioning to another scene.

I have 4 basic lua file

main
menu
creditsScene
playScene

Everything goes ok to start, menu come up just fine, press either button and it switches to different background but when I move to either the playScene or creditsScene my event listener doesn’t go back to the menu (like I think it should).

Any help to a new developer would be helpful. I am obviously missing something easy - just can’t understand what I am missing.

____________________
main.lua
____________________
_W = display.contentWidth;
_H = display.contentHeight;

local director = require(“director”);

local mainGroup = display.newGroup();

local function main()

mainGroup:insert(director.directorView);

director:changeScene(“menu”);

return true;

end

main();

____________________
menu.lua
____________________
module(…, package.seeall)

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

local bg = display.newImageRect(“bg.png”,_W, _H);
bg:setReferencePoint(display.CenterReferencePoint);
bg.x = _W/2; bg.y = _H/2;

local title = display.newImageRect(“title.png”, _W, 250);
title:setReferencePoint(display.CenterReferencePoint);
title.x = _W/2; title.y = title.height;

local play_btn = display.newImageRect(“play-btn.png”, _W, 125);
play_btn:setReferencePoint(display.CenterReferencePoint);
play_btn.x = _W/2; play_btn.y = _H/2 + play_btn.height;
play_btn.scene = “playScene”

local credits_btn = display.newImageRect(“credits-btn.png”, _W, 125);
credits_btn:setReferencePoint(display.CenterReferencePoint);
credits_btn.x = _W/2; credits_btn.y = _H/2 + play_btn.height + 100;
credits_btn.scene = “creditsScene”

function changeScene(e)

if(e.phase == “ended”) then
director:changeScene(e.target.scene);
end
end

_______________________
creditsScene.lua
_______________________
module (…, package.seeall);

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

local credits = display.newImageRect(“bg_credits.png”, _W, _H);
credits:setReferencePoint(display.CenterReferencePoint);
credits.x = _W/2; credits.y = _H/2;
credits.scene = “menu”;

localGroup:insert(credits);

function changeScene(e)
if(e.phase == “ended”) then
direct:changeScene(e.target.scene);
end
end

credits:addEventListener(“touch”, changeScene)

return localGroup;

end
_______________________
playScene.lua
_______________________

module (…, package.seeall);

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

local playgame = display.newImageRect(“bg_game.png”, _W, _H);
playgame:setReferencePoint(display.CenterReferencePoint);
playgame.x = _W/2; playgame.y = _H/2;
playgame.scene = “menu”;

localGroup:insert(playgame);

function changeScene(e)
if(e.phase == “ended”) then
direct:changeScene(e.target.scene);
end
end

playgame:addEventListener(“touch”, changeScene)

return localGroup;

end

[import]uid: 115952 topic_id: 21091 reply_id: 321091[/import]

In your menu.lua, you seem to be missing:

credits_btn:addEventListener(“touch”, changeScene)
play_btn:addEventListener(“touch”, changeScene)

In the other files, it needs to be:

director:changeScene(e.target.scene)

and not:

direct:changeScene(e.target.scene)
that should fix it.

And btw, if you put the changeScene function above the other buttons in the code, you can keep that local too.
i.e.:

local function changeScene(e)

but it needs to be above the button declarations so they can ‘see’ it.

Hope that helps!
Joe [import]uid: 8444 topic_id: 21091 reply_id: 83363[/import]

Joe - thanks for the help - solved. I really appreciate the fast response.

Going to sign up as a registered user later today because of the support I have gotten from people like you who are willing to help out us those that are learning.

Again, thank you!

David [import]uid: 115952 topic_id: 21091 reply_id: 83371[/import]

Just wanna give you a tip so that it’s easier for other developers to read your code and then help you :wink:

In the forum, always write code between “< code>” and “< /code>”
Without the blankspaces of course :slight_smile:

Then it will look like this:

[code]

_W = display.contentWidth;
_H = display.contentHeight;

local director = require(“director”);

local mainGroup = display.newGroup();

local function main()

mainGroup:insert(director.directorView);

director:changeScene(“menu”);

return true;

end

main();

[/code] [import]uid: 24111 topic_id: 21091 reply_id: 83378[/import]

can you get back to me i wantto know exactly how you were able to change the main file and then go to another file [import]uid: 139898 topic_id: 21091 reply_id: 106120[/import]

can you do the same thing with storyboard rather than director [import]uid: 139898 topic_id: 21091 reply_id: 106127[/import]