Getting error message while creating a rect using a create wall function, and accessing the function with two variables.

I made a function “wall” that pretty much creates a new rectangle. I created a variable called leftw that calls the function and aligns the wall to the left, and that worked, however when I create a second variable that calls the same function I get a error message… This error message: Attempt to call global wall (a table value) stack traceback: 23 in main chunk.

Am I not allowed to use a function with different variables?

Please help

Here is my code.

--starting physics local physics = require( "physics" ) physics.start( ) display.setStatusBar( display.HiddenStatusBar ) --screen variables local screenW = display.contentWidth local screenH = display.contentHeight local screenCY = display.contentCenterY local screenCX = display.contentCenterX --create walls and floors function wall(x, y , w, h) wall = display.newRect( x, y, w, h ) wall:setFillColor( 10, 10, 10 ) wall.anchorX=0 wall.anchorY=0 return wall end local leftw = wall(0, 0, 70,100) local rightw = wall( 0, 0, 200, 100)

You can’t use the same variable name twice. You have a global function wall, which you then overwrite with the global rect, meaning the function no longer exists.

Change this:

--create walls and floors function wall(x, y , w, h) wall = display.newRect( x, y, w, h ) wall:setFillColor( 10, 10, 10 ) wall.anchorX=0 wall.anchorY=0 return wall end

to something like this:

--create walls and floors function wall(x, y , w, h) local tmp = display.newRect( x, y, w, h ) tmp:setFillColor( 10, 10, 10 ) tmp.anchorX=0 tmp.anchorY=0 return tmp end

Oh, and be careful of globals.  You’re making them all over the place.

You can’t use the same variable name twice. You have a global function wall, which you then overwrite with the global rect, meaning the function no longer exists.

Change this:

--create walls and floors function wall(x, y , w, h) wall = display.newRect( x, y, w, h ) wall:setFillColor( 10, 10, 10 ) wall.anchorX=0 wall.anchorY=0 return wall end

to something like this:

--create walls and floors function wall(x, y , w, h) local tmp = display.newRect( x, y, w, h ) tmp:setFillColor( 10, 10, 10 ) tmp.anchorX=0 tmp.anchorY=0 return tmp end

Oh, and be careful of globals.  You’re making them all over the place.