Groups are failing me...

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.

[lua]–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()[/lua]

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… :frowning: [import]uid: 76200 topic_id: 13841 reply_id: 313841[/import]

you are just messing with the group:insert code
try this…simple, easy to read and debug :slight_smile:
[lua]–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()[/lua] [import]uid: 71210 topic_id: 13841 reply_id: 50844[/import]

@jorkobe.wang what was that ? [import]uid: 71210 topic_id: 13841 reply_id: 50859[/import]

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 :wink:

bingham, with Renivs’ code I assume your problem is now solved? :slight_smile: [import]uid: 52491 topic_id: 13841 reply_id: 50881[/import]

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. :slight_smile: [import]uid: 71210 topic_id: 13841 reply_id: 50887[/import]

Aw, well now the screen is just black. Hmm… [import]uid: 76200 topic_id: 13841 reply_id: 50943[/import]

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 [import]uid: 1294 topic_id: 13841 reply_id: 50975[/import]

well said Darren. Thanks. @bingham.noah hope you got it right. [import]uid: 71210 topic_id: 13841 reply_id: 51056[/import]

bingham, how did that go for you? :slight_smile:

@Renjith - sorry :stuck_out_tongue: 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 :wink: [import]uid: 52491 topic_id: 13841 reply_id: 51091[/import]

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
[import]uid: 71210 topic_id: 13841 reply_id: 51095[/import]

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? [import]uid: 76200 topic_id: 13841 reply_id: 51213[/import]

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:

[lua]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[/lua]

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 [import]uid: 1294 topic_id: 13841 reply_id: 51216[/import]

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

[import]uid: 71210 topic_id: 13841 reply_id: 51226[/import]

Ok, problem is, it shows the modes menu, not the main menu, and it wont load the menu when I click the back button. [import]uid: 76200 topic_id: 13841 reply_id: 51232[/import]

try this
[lua]–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()[/lua] [import]uid: 71210 topic_id: 13841 reply_id: 51236[/import]

Renjith, thanks, this worked and now I can continue! You all were a big help! Thanks guys! :slight_smile: I can now move on to more important things like making the modes screen work from menu to game. XD [import]uid: 76200 topic_id: 13841 reply_id: 51239[/import]

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. [import]uid: 71210 topic_id: 13841 reply_id: 51240[/import]

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. [import]uid: 76200 topic_id: 13841 reply_id: 51241[/import]

Glad it’s all smooth with no blacks screens :slight_smile:

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 :wink: [import]uid: 52491 topic_id: 13841 reply_id: 51273[/import]

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. :slight_smile:
[import]uid: 71210 topic_id: 13841 reply_id: 51286[/import]