Going from one screen to another

No replies
dmehta97
User offline. Last seen 5 years 40 weeks ago. Offline
Joined: 2 Mar 2013

So I am currently making a game. Its main.lua file is directed, using storyboard to the start.lua file. Here, in the start.lua file, I have it so that when the person presses "play", it goes to the game.lua file. If the person presses "credits", then it displays a new background and some images. And I have it so that the main title screen with the play and credits button are not visible using the titleView.isVisible = false command. However, I have been having some trouble going back to the title screen. I tried various things but it just doesn't work.

Here's the start.lua file code which contains the code to go to play game or to go to credits screen.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
 --> Add physics engine, start up the engine, and apply engine
local physics = require("physics")
physics.start()
 
-->Set gravity to act "down" (ie, toward bottom of the device)
physics.setGravity(0, 2.5)
 
-->Displays Background
local background = display.newImage ("Title Screen.png")
 
-->Requires Storyboard API
local storyboard = require "storyboard"
 
-->Displays Play Game Button
local playgame = display.newImage ("playBtnBigger.png")
playgame.x = display.contentWidth/2
playgame.y = display.contentHeight - 160
 
-->Displays Credits Button
local credits = display.newImage ("creditsBtnBigger.png")
credits.x = display.contentWidth/2
credits.y = playgame.y + 60
 
-->Group into Title View
local titleView = display.newGroup(background, playgame, credits)
 
-->Add Tap Event for Play Game
function startGame(event)
        local playgame = event.target
        storyboard.gotoScene("game", "fade", 400)
end
 
--> Add Tap Event for Credits
function showCredits(event)
        titleView.isVisible = false
        
        local creditsBG = display.newImage ("creditsScreen.png")
        
        local creditsBalloonDar = display.newImage ("creditsBalloonDar.png")
        physics.addBody (creditsBalloonDar, "static", {bounce = 1.5, friction = 10, radius = 45})
        creditsBalloonDar.x = display.contentWidth/2 - 95
        creditsBalloonDar.y = display.contentHeight/2
        
        local creditsBalloonJay = display.newImage ("creditsBalloonJay.png")
        physics.addBody (creditsBalloonJay, "static", {bounce = 1.5, friction = 10, radius = 45})
        creditsBalloonJay.x = creditsBalloonDar.x + 170
        creditsBalloonJay.y = creditsBalloonDar.y + 150
        
        local creditsBalloonCebo = display.newImage ("creditsBalloonCebo.png")
        physics.addBody (creditsBalloonCebo, "static", {bounce = 1.5, friction = 10, radius = 45})
        creditsBalloonCebo.x = creditsBalloonDar.x - 150
        creditsBalloonCebo.y = creditsBalloonDar.y + 150
        
                function moveCreditBalloonsDar()
                        transition.to(creditsBalloonDar, {time=750, speed=0.5, y=math.random(300, 370), onComplete=moveCreditBalloonsDar})
                        transition.to(creditsBalloonJay, {time=750, speed=0.5, y=math.random(220, 300)})
                        transition.to(creditsBalloonCebo, {time=750, speed=0.5, y=math.random(220, 300)})
                end
                
                moveCreditBalloonsDar()
end
 
--> Add Event Listeners for Play Game & Credits
playgame:addEventListener("tap", startGame)
credits:addEventListener("tap", showCredits)