Help, error with switch

Hi, I am trying to add another switch, but when I save, this error occurs. When I remove the 2nd switch, there is no error. What is the problem?

local widget = require("widget") widget.setTheme ( "widget\_theme\_ios" ) display.setStatusBar(display.HiddenStatusBar) -- most commonly used screen coordinates centerX = display.contentCenterX centerY = display.contentCenterY screenLeft = display.screenOriginX screenWidth = display.contentWidth - screenLeft \* 2 screenRight = screenLeft + screenWidth screenTop = display.screenOriginY screenHeight = display.contentHeight - screenTop \* 2 screenBottom = screenTop + screenHeight display.contentWidth = screenWidth display.contentHeight = screenHeight -- pretty background local bg = display.newImage("images/sky.jpg") bg.x = centerX bg.y = centerY bg.width = screenWidth bg.height = screenHeight local function buttonPressed(event) local btn = event.target if btn.id == "options" then btn:setLabel("Foo!") end print("You pressed button:", btn.id) end local function doButtons() local optionsButton = widget.newButton { label="Set Options", id="options", onRelease=buttonPressed } optionsButton.x = centerX optionsButton.anchorY = 1 optionsButton.y = screenBottom - 15 local newButton = widget.newButton { x = centerX, y = centerY + 50, width = 800, height = 100, id = "newme", onRelease=buttonPressed, defaultFile = "images/btn-horse.png", overFile = "images/btn-horse-pressed.png" } local horseButton = widget.newButton { x = centerX, y = screenTop + 50, width = 400, height = 92, id = "choose", onRelease=buttonPressed, defaultFile = "images/btn-horse.png", overFile = "images/btn-horse-pressed.png" } local blankButton = widget.newButton { x = centerX, y = centerY + 20, width = 400, height = 92, id = "play", onRelease=buttonPressed, defaultFile = "images/btn-board.png", overFile = "images/btn-board-pressed.png", label = "Play", fontSize = 48, labelColor = { default={1,1,1}, over={0,0,0} } } local function cbSwitchListener(event) local status = event.target.isOn print("Switch is on: " .. tostring(status)) horseButton:setEnabled(status) end local cbButton = widget.newSwitch{ style = "checkbox", initialSwitchState = true, onPress = cbSwitchListener } cbButton.x = screenRight - 80 cbButton.y = horseButton.y + horseButton.height/2 + 10 end local function accessSwitchListener(event) local status = event.target.isOn print("Switch is on: " .. tostring(status)) horseButton:setEnabled(play) end local accessButton = widget.newSwitch{ style = "checkbox", initialSwitchState = true, onPress = accessSwitchListener } accessButton.x = screenRight - 160 accesssButton.y = horseButton.y + horseButton.height/2 + 10 end local function doScroll() local function onePic() end local function sixPics() end onePic() --sixPics() end doButtons() --doScroll()

What is the error?

Can you edit your post and highlight your code and press the <> button in the edit bar?

Rob

Also I removed your duplicate posts.  Please only ask the question once.

Now, your error would be pretty obvious if your code was indented properly.  Lua is a language based on blocks that begin with a keyword like:  “function” and end with the word “end”.  Blocks begin with “function”, “for”, “while”, “repeat” and “if”.  Your code is more readable if you indent each line inside a block by four spaces or a tab.  Look at this code:

local function outterBlock() &nbsp;&nbsp;&nbsp;&nbsp; local someVariable = 10 &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; if someVariable == 9 then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; someVariable = someVariable + 1 &nbsp;&nbsp;&nbsp;&nbsp; end end

When you do this, you can easily see the indented if and end are lined up with each other.  For every “if”, there is a matching “end”.  Then you can see the last end lines up with the function declaration.  Two ends, two blocks, the code is happy.

You have extra ends in the middle of your code, closing out a previous block that you’ve not intended to and then you have code after the last end it expects and it has more ends and then it’s confused by the end of the program with the blocks not lining up.

This is why I want you to format your code to make it easier to read.

Rob

Hi Rob,

When I try to add end at the end of the second local button the error occurs.

local cbButton = widget.newSwitch{ style = "checkbox", initialSwitchState = true, onPress = cbSwitchListener } cbButton.x = screenRight - 80 cbButton.y = horseButton.y + horseButton.height/2 + 10 end local function accessSwitchListener(event) local status = event.target.isOn print("Switch is on: " .. tostring(status)) horseButton:setEnabled(play) end local accessButton = widget.newSwitch{ style = "checkbox", initialSwitchState = true, onPress = accessSwitchListener } accessButton.x = screenRight - 160 accesssButton.y = horseButton.y + horseButton.height/2 + 10 end

Ask yourself… what block am I ended when I put the end there?  Does it go with an IF? Function? While?  Every end has to go with a block start. 

local cbButton = widget.newSwitch{ style = "checkbox", initialSwitchState = true, onPress = cbSwitchListener } cbButton.x = screenRight - 80 cbButton.y = horseButton.y + horseButton.height/2 + 10 end

local cbButton = widget.newButton() does not start a block.  Its a function that takes a table.  Technically speaking the closing } closes the starting { 4 lines up.  The cbButton.x = and cbButton.y = lines are not part of any block.

"end"s only go with “if then else/elseif”, “function”, “while”, and “repeat” structures.

Rob

What is the error?

Can you edit your post and highlight your code and press the <> button in the edit bar?

Rob

Also I removed your duplicate posts.  Please only ask the question once.

Now, your error would be pretty obvious if your code was indented properly.  Lua is a language based on blocks that begin with a keyword like:  “function” and end with the word “end”.  Blocks begin with “function”, “for”, “while”, “repeat” and “if”.  Your code is more readable if you indent each line inside a block by four spaces or a tab.  Look at this code:

local function outterBlock() &nbsp;&nbsp;&nbsp;&nbsp; local someVariable = 10 &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; if someVariable == 9 then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; someVariable = someVariable + 1 &nbsp;&nbsp;&nbsp;&nbsp; end end

When you do this, you can easily see the indented if and end are lined up with each other.  For every “if”, there is a matching “end”.  Then you can see the last end lines up with the function declaration.  Two ends, two blocks, the code is happy.

You have extra ends in the middle of your code, closing out a previous block that you’ve not intended to and then you have code after the last end it expects and it has more ends and then it’s confused by the end of the program with the blocks not lining up.

This is why I want you to format your code to make it easier to read.

Rob

Hi Rob,

When I try to add end at the end of the second local button the error occurs.

local cbButton = widget.newSwitch{ style = "checkbox", initialSwitchState = true, onPress = cbSwitchListener } cbButton.x = screenRight - 80 cbButton.y = horseButton.y + horseButton.height/2 + 10 end local function accessSwitchListener(event) local status = event.target.isOn print("Switch is on: " .. tostring(status)) horseButton:setEnabled(play) end local accessButton = widget.newSwitch{ style = "checkbox", initialSwitchState = true, onPress = accessSwitchListener } accessButton.x = screenRight - 160 accesssButton.y = horseButton.y + horseButton.height/2 + 10 end

Ask yourself… what block am I ended when I put the end there?  Does it go with an IF? Function? While?  Every end has to go with a block start. 

local cbButton = widget.newSwitch{ style = "checkbox", initialSwitchState = true, onPress = cbSwitchListener } cbButton.x = screenRight - 80 cbButton.y = horseButton.y + horseButton.height/2 + 10 end

local cbButton = widget.newButton() does not start a block.  Its a function that takes a table.  Technically speaking the closing } closes the starting { 4 lines up.  The cbButton.x = and cbButton.y = lines are not part of any block.

"end"s only go with “if then else/elseif”, “function”, “while”, and “repeat” structures.

Rob