Groups are failing me...

19 replies [Last post]
Noah Atchison
User offline. Last seen 4 years 23 weeks ago. Offline
Joined: 14 Jul 2011

The title practically says it all. What I want to do is make a function for a menu screen button called play, right? I use the Debug Draw UI for a faster button creation system, by the way. (Thought that might help you solve this...) But anyways, I can't create good groups. Here is an example of what I'm trying to do.

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
--opens modes...
function openModes()
if menu then
menu.remove()
local modes = display.newGroup()
 
modes:insert(
        local backButton = ui.newButton{
        default = "back.png",
        over = "backOver.png",
        onPress = loadMenu,
        text = "",
        size = 20,
        emboss = true,
        x = halfW,
        y = 250
        }
                backButton.xScale = 3.5
                backButton.yScale = 3.5
        
        local easyButton = ui.newButton{
        default = "buttonRed.png",
        over = "buttonRedOver.png",
        onPress = runGameEasy,
        text = "Easy",
        size = 20,
        emboss = true,
        x = halfW,
        y = 400
        }
                easyButton.xScale = 3.5
                easyButton.yScale = 3.5
)
end
end
--function used to load menu if to say someone leaves a current game
function loadMenu()
if modes then
modes.remove()
local menu = display.newGroup()
 
menu:insert(
        local playButton = ui.newButton{
                default = "smallButton.png",
                over = "smallButtonOver.png",
                onPress = openModes,
                text = "Play",
                size = 20,
                emboss = true,
                x = halfW,
                y = 400
        }
                playButton.xScale = 3.5
                playButton.yScale = 3.5
)
end
end
loadMenu()

Typically I'll receive some output like "Syntax error: C:\Users\Noah\Desktop\Bucket Ball\main.lua:55: unexpected symbol near 'local'" I can't figure out how to make this work... :(

Replies

renvis@technowand
User offline. Last seen 16 weeks 3 days ago. Offline
Joined: 22 Jun 2011

you are just messing with the group:insert code
try this...simple, easy to read and debug :)

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
--opens modes...
function openModes()
if menu then
menu.remove()
local modes = display.newGroup()
 
 
        local backButton = ui.newButton{
        default = "back.png",
        over = "backOver.png",
        onPress = loadMenu,
        text = "",
        size = 20,
        emboss = true,
        x = halfW,
        y = 250
        }
        backButton.xScale = 3.5
        backButton.yScale = 3.5
        
        local easyButton = ui.newButton{
        default = "buttonRed.png",
        over = "buttonRedOver.png",
        onPress = runGameEasy,
        text = "Easy",
        size = 20,
        emboss = true,
        x = halfW,
        y = 400
        }
        easyButton.xScale = 3.5
         easyButton.yScale = 3.5
                                
                                
       modes:insert(backButton)
       modes:insert(easyButton)
end
end
--function used to load menu if to say someone leaves a current game
function loadMenu()
if modes then
modes.remove()
local menu = display.newGroup()
 
 
        local playButton = ui.newButton{
                default = "smallButton.png",
                over = "smallButtonOver.png",
                onPress = openModes,
                text = "Play",
                size = 20,
                emboss = true,
                x = halfW,
                y = 400
        }
        playButton.xScale = 3.5
        playButton.yScale = 3.5
        menu:insert(playButton)
end
end
loadMenu()

renvis@technowand
User offline. Last seen 16 weeks 3 days ago. Offline
Joined: 22 Jun 2011

@jorkobe.wang what was that ?

peach pellen
User offline. Last seen 7 years 33 weeks ago. Offline
Alumni
Joined: 12 Apr 2011

Renvis, the post above yours was removed because it was spam - so it looks like you're now talking to no one. Sorry about that ;)

bingham, with Renivs' code I assume your problem is now solved? :)

renvis@technowand
User offline. Last seen 16 weeks 3 days ago. Offline
Joined: 22 Jun 2011

yeaaa thats right..... why don't we have a delete button for our own posts ?

and peach, call me Renjith(remember am ur facebook friend) Renvis is just a display name. :)

Noah Atchison
User offline. Last seen 4 years 23 weeks ago. Offline
Joined: 14 Jul 2011

Aw, well now the screen is just black. Hmm...

Ludicrous Software
User offline. Last seen 3 years 3 days ago. Offline
Joined: 1 Jul 2009

Don't have the images so I can't test this to be sure, but here's a best guess. By the way, I'm working off the code that Renjith posted, since the original code has issues with the calls to :insert().

Your modes and menu variables are local to their enclosing functions. Since they're local, there's no reference to them that exists after the functions have finished executing. So when the code runs for the first time, loadMenu() can't find an object called 'modes', so your menu isn't created. This is why the screen is black.

And even if the menu were created, when the playButton is pressed and openModes() is executed, it wouldn't be able to find the menu display group, because that's local to loadMenu(). Try declaring the modes and menu variables outside of the functions (i.e. before either function is declared), and see if that helps.

(All of this is bearing in mind that I don't know what the menu.remove() and modes.remove() functions are supposed to do. If you're trying to call display.remove() then you need to call display.remove(menu) or display.remove(modes), although menu:remove() or modes:remove() may also work - haven't tested it.)

Thanks,
Darren

renvis@technowand
User offline. Last seen 16 weeks 3 days ago. Offline
Joined: 22 Jun 2011

well said Darren. Thanks. @bingham.noah hope you got it right.

peach pellen
User offline. Last seen 7 years 33 weeks ago. Offline
Alumni
Joined: 12 Apr 2011

bingham, how did that go for you? :)

@Renjith - sorry :P I know your name, I just wasn't sure whether or not you'd prefer it or the nickname you were using at the moment ;)

renvis@technowand
User offline. Last seen 16 weeks 3 days ago. Offline
Joined: 22 Jun 2011

I changed the display name with help from Ansca support then had a second thought. wanted to change it again and am afraid they will scold me if I ask them. LOL

Noah Atchison
User offline. Last seen 4 years 23 weeks ago. Offline
Joined: 14 Jul 2011

Thanks Darren. Though, I'm wondering if I should just make another file and import it OR use global variables? I've never used _G before because I never felt the need to. I wonder if now is the time to learn?

Ludicrous Software
User offline. Last seen 3 years 3 days ago. Offline
Joined: 1 Jul 2009

Not sure what you mean by "make another file and import it", so I can't comment on that. In the context of the code snippet you shared, there's no need for the display groups to be global variables. They can be local variables, just make sure they're declared before each of the functions:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
local modes, menu
 
local function openModes()
  if menu then
    menu:removeSelf()
    menu = nil
    modes = display.newGroup()
    -- etc
  end
end
 
local function loadMenu()
  if modes then
    modes:removeSelf()
    modes = nil
    menu = display.newGroup()
    -- etc
  end
end

Since modes and menu were declared before the function code blocks, I didn't use local when creating the display groups inside the function blocks.

Of course, if you have some other reason why these need to be globals, then you can use globals. But I don't see any need to do so.

Thanks,
Darren

renvis@technowand
User offline. Last seen 16 weeks 3 days ago. Offline
Joined: 22 Jun 2011

@bingham.noah are you still getting blank screens ? whats the problem you are facing now ?

Noah Atchison
User offline. Last seen 4 years 23 weeks ago. Offline
Joined: 14 Jul 2011

Ok, problem is, it shows the modes menu, not the main menu, and it wont load the menu when I click the back button.

renvis@technowand
User offline. Last seen 16 weeks 3 days ago. Offline
Joined: 22 Jun 2011

try this

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
--opens modes...
function openModes()
if menu then menu.remove() end
local modes = display.newGroup()
        local backButton = ui.newButton{
        default = "back.png",
        over = "backOver.png",
        onPress = loadMenu,
        text = "",
        size = 20,
        emboss = true,
        x = halfW,
        y = 250
        }
        backButton.xScale = 3.5
        backButton.yScale = 3.5
        
        local easyButton = ui.newButton{
        default = "buttonRed.png",
        over = "buttonRedOver.png",
        onPress = runGameEasy,
        text = "Easy",
        size = 20,
        emboss = true,
        x = halfW,
        y = 400
        }
        easyButton.xScale = 3.5
         easyButton.yScale = 3.5
                                
                                
       modes:insert(backButton)
       modes:insert(easyButton)
 
end
--function used to load menu if to say someone leaves a current game
function loadMenu()
if modes then modes.remove() end
local menu = display.newGroup()
 
 
        local playButton = ui.newButton{
                default = "smallButton.png",
                over = "smallButtonOver.png",
                onPress = openModes,
                text = "Play",
                size = 20,
                emboss = true,
                x = halfW,
                y = 400
        }
        playButton.xScale = 3.5
        playButton.yScale = 3.5
        menu:insert(playButton)
end
loadMenu()

Noah Atchison
User offline. Last seen 4 years 23 weeks ago. Offline
Joined: 14 Jul 2011

Renjith, thanks, this worked and now I can continue! You all were a big help! Thanks guys! :) I can now move on to more important things like making the modes screen work from menu to game. XD

renvis@technowand
User offline. Last seen 16 weeks 3 days ago. Offline
Joined: 22 Jun 2011

good to know it worked. and sorry for the delay.. actually I din't think too much about what you were trying to achieve.. that's why i was not able to figure it out last day.

Noah Atchison
User offline. Last seen 4 years 23 weeks ago. Offline
Joined: 14 Jul 2011

Oh, lol. Sorry I was so vague about this also. I was on a one week challenge (to be completed tomorrow) and I didn't have much time or enough code anyways to contribute to the full understanding.

peach pellen
User offline. Last seen 7 years 33 weeks ago. Offline
Alumni
Joined: 12 Apr 2011

Glad it's all smooth with no blacks screens :)

Renjith, I have no control over usernames, if I did I'd help you - but if you really want a change I'm sure no one will be too cross; just be polite (as you always are) and I'm sure it wont be a problem. Everyone at Ansca is lovely .... Except Carlos, he's an ogre! (I joke, of course.)

Peach ;)

renvis@technowand
User offline. Last seen 16 weeks 3 days ago. Offline
Joined: 22 Jun 2011

yea, I know peach... they were really very patient when I had an issue with building for device and we had to work together for hours to fix that. :)

Viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.