trouble while creating a button

hi guys, 

i am having trouble while creating a button.

the code i wrote is this:-

                      

local widget = require(“widget”)

local function handleButtonEvent( event )

local phase = event.phase

if “ended” == phase then

   print( “You pressed and released a button” )

end

end

local myButton = widget.newButton

   left = 150,

   top = 200,

   width = 350,

   height = 150,

   defaultFile = “startbutton1.png”,

   overFile = “startbutton.png”,

   label = “Start”,

   onEvent = handleButtonEvent,

}

and the error the Corona simulator gives is this:-

main.lua:17: ‘)’ expected (to close ‘(’ at line 16) near ‘=’

Thanks in advance.

First, welcome to the forums! 

Next, it’s really helpful to use code formatting when posting code. You can either type:

[lua] paste your code here [/lua]

or simply click the blue <> button in the editting tools bar with Bold, Italic, etc. and paste your code into the window that pops up. Now on to your problem:

local myButton = widget.newButton ( --\<-------------- left = 150, top = 200, width = 350, height = 150, defaultFile = "startbutton1.png", overFile = "startbutton.png", label = "Start", onEvent = handleButtonEvent, } --\<------------- 

Notice the two lines I put arrows on.  widget.newButton is expecting a table to be passed which means you need to inclose all of the constructor parts inside curly braces {}. The first one isn a parenthesis, not a curly brace.

Most functions you write expect to have parenthesis around all parameters like every other programming language. However, if the function only takes a single table, Lua lets you leave off the parens in that case. Thus either:

local button = widget.newButton({ &nbsp; &nbsp; &nbsp; button code })

or 

local button = widget.newButton { &nbsp; &nbsp; &nbsp;button code }

are legal. So why do the first one? It’s extra typing… Yes, it is, but it also is syntactically consistent with other functions that require multiple parameters or non-table parameters. For me it’s a personal preference to include the parens. The Lua language doesn’t care.

Rob

Thanks very much for your help Rob, and i am sorry for the ‘(’ and not this ‘{’ bracket. I replaced this ‘)’ bracket but forgot to replace the one on the top.

i tried your code but the corona simulator gives me this error message:

main.lua:5: attempt to index global ‘widget’ (a nil value) stack traceback: main.lua:5: in main chunk.

It looks like you have this in your code:
 

local widget = require( “widget” )

You might have a fancy quote or something in there. But that error indicates that the above command didn’t work as expected.

Rob

Thanks for your help. I found the solution.

i added “local widget = require("widget)” before “l_ocal button = widget.newbutton_”

First, welcome to the forums! 

Next, it’s really helpful to use code formatting when posting code. You can either type:

[lua] paste your code here [/lua]

or simply click the blue <> button in the editting tools bar with Bold, Italic, etc. and paste your code into the window that pops up. Now on to your problem:

local myButton = widget.newButton ( --\<-------------- left = 150, top = 200, width = 350, height = 150, defaultFile = "startbutton1.png", overFile = "startbutton.png", label = "Start", onEvent = handleButtonEvent, } --\<------------- 

Notice the two lines I put arrows on.  widget.newButton is expecting a table to be passed which means you need to inclose all of the constructor parts inside curly braces {}. The first one isn a parenthesis, not a curly brace.

Most functions you write expect to have parenthesis around all parameters like every other programming language. However, if the function only takes a single table, Lua lets you leave off the parens in that case. Thus either:

local button = widget.newButton({ &nbsp; &nbsp; &nbsp; button code })

or 

local button = widget.newButton { &nbsp; &nbsp; &nbsp;button code }

are legal. So why do the first one? It’s extra typing… Yes, it is, but it also is syntactically consistent with other functions that require multiple parameters or non-table parameters. For me it’s a personal preference to include the parens. The Lua language doesn’t care.

Rob

Thanks very much for your help Rob, and i am sorry for the ‘(’ and not this ‘{’ bracket. I replaced this ‘)’ bracket but forgot to replace the one on the top.

i tried your code but the corona simulator gives me this error message:

main.lua:5: attempt to index global ‘widget’ (a nil value) stack traceback: main.lua:5: in main chunk.

It looks like you have this in your code:
 

local widget = require( “widget” )

You might have a fancy quote or something in there. But that error indicates that the above command didn’t work as expected.

Rob

Thanks for your help. I found the solution.

i added “local widget = require("widget)” before “l_ocal button = widget.newbutton_”