Selecting levels doesn't work with director.

I was working on building a level menu for my game. Heres menu.lua

[lua]module(…, package.seeall)

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

local background = display.newImage (“mainbackground.jpg”)
localGroup:insert(background)

local playbtn = display.newImage (“playbtn.png”)
playbtn.x = 160
playbtn.y = 100
localGroup:insert(Playbtn)
playbtn:addEventListener (“touch”, presslevels)

return localGroup
end[/lua]

& levels.lua
[lua]module(…, package.seeall)

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

local background = display.newImage (“mainbackground.jpg”)
localGroup:insert(background)

local level1btn = display.newImage (“level1btn.png”)
level1btn.x = 160
level1btn.y = 100
localGroup:insert(level1btn)

local level2btn = display.newImage (“level2btn.png”)
level2btn.x = 160
level2btn.y = 225
localGroup:insert(level2btn)

local level3btn = display.newImage (“level3btn.png”)
level3btn.x = 160
level3btn.y = 350
localGroup:insert(level3btn)

local function pressLevel1 (event)
if event.phase == “ended” then
director:changeScene (“level1btn”)
end
end

level1btn:addEventListener (“touch”, pressLevel1)

local function pressLevel2 (event)
if event.phase == “ended” then
director:changeScene (“level2btn”)
end
end

level2btn:addEventListener (“touch”, pressLevel2)

local function pressLevel3 (event)
if event.phase == “ended” then
director:changeScene (“level3btn”)
end
end

level3btn:addEventListener (“touch”, pressLevel3)
return localGroup
end[/lua]

I only get the background picture, no buttons. Do have director.lua in the game folder BTW.

[import]uid: 128294 topic_id: 30543 reply_id: 330543[/import]

Help? Anyone? [import]uid: 128294 topic_id: 30543 reply_id: 122571[/import]

First of all, you should use this approach:
[lua]local M={}

local function new()

end

M.new=new[/lua]
That’s the best way.

Second of all, I’m noticing you’re making a playbtn (lower case) and inserting it into the group as Playbtn (upper case)

Third of all, I’m not seeing a function “pressLevels”. I guess that goes to levels.lua.

Fourth of all, are you getting any errors in the terminal? [import]uid: 147322 topic_id: 30543 reply_id: 122879[/import]

@quinc

OS X’s terminal?

Plus I thought when you do type the local function for pressLevels thought a coder is required to use a upper case letter. Like…

[lua]local function pressYellow (event)
if event.phase == “ended” then
director:changeScene (“yellow”)
end
end

yellowbutton:addEventListener (“touch”, pressYellow)[/lua] [import]uid: 128294 topic_id: 30543 reply_id: 122905[/import]

No, the Corona Terminal. You pretty much need to develop using that. (Applications>Corona SDK>Corona Terminal) Just double-click it and it will open the terminal and Corona Simulator. It’s what is affected when you call print(“Hello!”). Any errors you have show up there.

And for the second problem, I mean this:
[lua]local playbtn = display.newImage (“playbtn.png”) – Creating it as “playbtn”
playbtn.x = 160
playbtn.y = 100
localGroup:insert(Playbtn) – Inserting it as “Playbtn” (with an upper-case “p”)[/lua]
If you check Corona Terminal I’ll bet you’re getting an error with that.

quinc [import]uid: 147322 topic_id: 30543 reply_id: 122995[/import]

Are you sure you have the button filenames correct?

You could avoid having a separate listener for each button like this (makes it easier to add more buttons later on).

[lua]level1btn.id = 1 – set an ID for each level button[/lua]

[lua]level1btn:addEventListener (“touch”, pressButton) – add same listener function to each button[/lua]

Then handle button presses like this (make sure you declare the function BEFORE any of the buttons are created:

[lua]local pressButton = function (event)

local level = event.target.id

if event.phase == “ended” then

local sceneName = “level”…level…“btn”
director:changeScene (sceneName)

end

end[/lua]

You could also put your buttons into an array and create them within a for loop:

[lua]local buttons = {}
local numLevels = 3
local yOffset = 125

for i = 1, numLevels, 1 do

local fName = “levelbtn”…i…".png"
local btn = display.newImage (fName)
btn.x = 160
btn.y = 100 + ((i-1)*yOffset)
btn.id = i
localGroup:insert(btn)
btn:addEventListener (“touch”, pressButton)
buttons[i] = btn

end[/lua]

Adding more buttons would be simply a case of changing numLevels, and adjusting yOffset to ensure they all fit on screen. [import]uid: 93133 topic_id: 30543 reply_id: 122997[/import]

@quinc

Odd thing is the first time I opened the corona terminal, it didn’t display anything, then I quit it and then it worked. Don’t know why sometimes [lua] package.seeall[/lua] displays a question mark instead of three periods.

@nick_sherman

That code goes into menu.lua or main.lua? I was able to get it past levels to level 1 but the backbutton doesn’t work still.

[text]
?: in function ‘getOrCreateTable’
?: in function ‘addEventListener’
?: in function ‘addEventListener’
buttonfolder/levels.lua:37: in function ‘new’
buttonfolder/director.lua:116: in function ‘loadScene’
buttonfolder/director.lua:394: in function ‘changeScene’
buttonfolder/menu.lua:18: in function
<…>
?: in function
[/text]
[import]uid: 128294 topic_id: 30543 reply_id: 123062[/import] </…>

Help? Anyone? [import]uid: 128294 topic_id: 30543 reply_id: 122571[/import]

First of all, you should use this approach:
[lua]local M={}

local function new()

end

M.new=new[/lua]
That’s the best way.

Second of all, I’m noticing you’re making a playbtn (lower case) and inserting it into the group as Playbtn (upper case)

Third of all, I’m not seeing a function “pressLevels”. I guess that goes to levels.lua.

Fourth of all, are you getting any errors in the terminal? [import]uid: 147322 topic_id: 30543 reply_id: 122879[/import]

@quinc

OS X’s terminal?

Plus I thought when you do type the local function for pressLevels thought a coder is required to use a upper case letter. Like…

[lua]local function pressYellow (event)
if event.phase == “ended” then
director:changeScene (“yellow”)
end
end

yellowbutton:addEventListener (“touch”, pressYellow)[/lua] [import]uid: 128294 topic_id: 30543 reply_id: 122905[/import]

No, the Corona Terminal. You pretty much need to develop using that. (Applications>Corona SDK>Corona Terminal) Just double-click it and it will open the terminal and Corona Simulator. It’s what is affected when you call print(“Hello!”). Any errors you have show up there.

And for the second problem, I mean this:
[lua]local playbtn = display.newImage (“playbtn.png”) – Creating it as “playbtn”
playbtn.x = 160
playbtn.y = 100
localGroup:insert(Playbtn) – Inserting it as “Playbtn” (with an upper-case “p”)[/lua]
If you check Corona Terminal I’ll bet you’re getting an error with that.

quinc [import]uid: 147322 topic_id: 30543 reply_id: 122995[/import]

Are you sure you have the button filenames correct?

You could avoid having a separate listener for each button like this (makes it easier to add more buttons later on).

[lua]level1btn.id = 1 – set an ID for each level button[/lua]

[lua]level1btn:addEventListener (“touch”, pressButton) – add same listener function to each button[/lua]

Then handle button presses like this (make sure you declare the function BEFORE any of the buttons are created:

[lua]local pressButton = function (event)

local level = event.target.id

if event.phase == “ended” then

local sceneName = “level”…level…“btn”
director:changeScene (sceneName)

end

end[/lua]

You could also put your buttons into an array and create them within a for loop:

[lua]local buttons = {}
local numLevels = 3
local yOffset = 125

for i = 1, numLevels, 1 do

local fName = “levelbtn”…i…".png"
local btn = display.newImage (fName)
btn.x = 160
btn.y = 100 + ((i-1)*yOffset)
btn.id = i
localGroup:insert(btn)
btn:addEventListener (“touch”, pressButton)
buttons[i] = btn

end[/lua]

Adding more buttons would be simply a case of changing numLevels, and adjusting yOffset to ensure they all fit on screen. [import]uid: 93133 topic_id: 30543 reply_id: 122997[/import]

@quinc

Odd thing is the first time I opened the corona terminal, it didn’t display anything, then I quit it and then it worked. Don’t know why sometimes [lua] package.seeall[/lua] displays a question mark instead of three periods.

@nick_sherman

That code goes into menu.lua or main.lua? I was able to get it past levels to level 1 but the backbutton doesn’t work still.

[text]
?: in function ‘getOrCreateTable’
?: in function ‘addEventListener’
?: in function ‘addEventListener’
buttonfolder/levels.lua:37: in function ‘new’
buttonfolder/director.lua:116: in function ‘loadScene’
buttonfolder/director.lua:394: in function ‘changeScene’
buttonfolder/menu.lua:18: in function
<…>
?: in function
[/text]
[import]uid: 128294 topic_id: 30543 reply_id: 123062[/import] </…>