I’m trying to create a rectangle by passing the parameters in table. But I get an error. Why?
options={ x=200, y=300, width=158, height=50}
myRectangle = display.newRect(options)
I’m trying to create a rectangle by passing the parameters in table. But I get an error. Why?
options={ x=200, y=300, width=158, height=50}
myRectangle = display.newRect(options)
this will work
local r = display.newRect(options[1], options[2], options[3], opttions[4] )
just remove the x=, y= etc… in the table. The args just have to be in order x, y, width, height
I am not sure if there is another way to do what you are trying to do… someone else may know a better way. But this will work.
Hope it helps.
Bob
You can’t pass parameters to display.newRect() in a table because that function’s parameter list is not a single table parameter, but a list of named parameters. Take a look at the documentation page for it and you’ll see that, when compared to functions like display.newText, the difference is defined as either ( [parent,] x, y, width, height ) or ( options ) - and they are not compatible.
You can, however, write a replacement function which keeps a reference to the original newRect function and allows you to pass in a table of optional parameter values, which can provide defaults. I would not recommend doing this if you are not comfortable with the concept of parameter lists yet.
Having said that, here are a few of my replacement functions for doing just that:
-- encapsulates stroked lines local \_newLine = display.newLine display.newLine = function( ... ) if (#arg == 1) then local params = arg[1] local line = \_newLine( unpack( params.path ) ) if (params.parent) then params.parent:insert( line ) end line.x, line.y = params.x or line.x, params.y or line.y if (params.stroke) then display.stroke( line, params.stroke, params.strokeWidth ) end return line else return \_newLine( unpack( arg ) ) end end -- encapsulates full circle defining local \_newCircle = display.newCircle display.newCircle = function( ... ) if (#arg == 1) then local params = arg[1] local circle = \_newCircle( params.x or 0, params.y or 0, params.radius or 0 ) if (params.parent) then params.parent:insert( circle ) end if (params.fill) then circle:setFillColor( unpack( params.fill ) ) end circle.alpha = params.alpha or 1 if (params.stroke) then display.stroke( circle, params.stroke, params.strokeWidth ) end circle.radius = params.radius return circle else return \_newCircle( unpack( arg ) ) end end -- encapsulates full rectangle defining local \_newRect = display.newRect display.newRect = function( ... ) if (#arg == 1) then local params = arg[1] local rect = \_newRect( params.x or 0, params.y or 0, params.width or 1, params.height or 1 ) if (params.parent) then params.parent:insert( rect ) end if (params.fill) then rect:setFillColor( unpack( params.fill ) ) end rect.alpha = params.alpha or 1 if (params.stroke) then display.stroke( rect, params.stroke, params.strokeWidth ) end return rect else return \_newRect( unpack( arg ) ) end end
Thanks a lot for the helpful replies.
this will work
local r = display.newRect(options[1], options[2], options[3], opttions[4] )
just remove the x=, y= etc… in the table. The args just have to be in order x, y, width, height
I am not sure if there is another way to do what you are trying to do… someone else may know a better way. But this will work.
Hope it helps.
Bob
You can’t pass parameters to display.newRect() in a table because that function’s parameter list is not a single table parameter, but a list of named parameters. Take a look at the documentation page for it and you’ll see that, when compared to functions like display.newText, the difference is defined as either ( [parent,] x, y, width, height ) or ( options ) - and they are not compatible.
You can, however, write a replacement function which keeps a reference to the original newRect function and allows you to pass in a table of optional parameter values, which can provide defaults. I would not recommend doing this if you are not comfortable with the concept of parameter lists yet.
Having said that, here are a few of my replacement functions for doing just that:
-- encapsulates stroked lines local \_newLine = display.newLine display.newLine = function( ... ) if (#arg == 1) then local params = arg[1] local line = \_newLine( unpack( params.path ) ) if (params.parent) then params.parent:insert( line ) end line.x, line.y = params.x or line.x, params.y or line.y if (params.stroke) then display.stroke( line, params.stroke, params.strokeWidth ) end return line else return \_newLine( unpack( arg ) ) end end -- encapsulates full circle defining local \_newCircle = display.newCircle display.newCircle = function( ... ) if (#arg == 1) then local params = arg[1] local circle = \_newCircle( params.x or 0, params.y or 0, params.radius or 0 ) if (params.parent) then params.parent:insert( circle ) end if (params.fill) then circle:setFillColor( unpack( params.fill ) ) end circle.alpha = params.alpha or 1 if (params.stroke) then display.stroke( circle, params.stroke, params.strokeWidth ) end circle.radius = params.radius return circle else return \_newCircle( unpack( arg ) ) end end -- encapsulates full rectangle defining local \_newRect = display.newRect display.newRect = function( ... ) if (#arg == 1) then local params = arg[1] local rect = \_newRect( params.x or 0, params.y or 0, params.width or 1, params.height or 1 ) if (params.parent) then params.parent:insert( rect ) end if (params.fill) then rect:setFillColor( unpack( params.fill ) ) end rect.alpha = params.alpha or 1 if (params.stroke) then display.stroke( rect, params.stroke, params.strokeWidth ) end return rect else return \_newRect( unpack( arg ) ) end end
Thanks a lot for the helpful replies.