Doubt with true and false

I wanted two make two physics bodies using one class and that too had two different cases. But I couldn’t differentiate between the two, either the first one would get drawn or the second one. I know what is going wrong. But can’t find a solution for it. ‘Horizontal’ is set to true or false depending on the last wall created. How should I prevent that and make it true and false for walls individually.
So if the last wall is w[3] then only it will be drawn but If the last one is w[1or2] then those two will be drawn.
here’s the code. I want all the walls to be drawn.


--# Main
function setup()
    w = {}
    w[1] = Walls(WIDTH/2,0,WIDTH/2,HEIGHT/2,false)
    w[2] = Walls(WIDTH/3,0,WIDTH/3,HEIGHT/2,false)
    w[3] = Walls(0,HEIGHT/2,WIDTH/5,HEIGHT/2,true)
end

function draw()
    background(244, 242, 242, 255)
    for i = 1,#w do
        w[i]:draw()
    end
end
--# Walls

Walls = class()

function Walls:init(x1,y1,x2,y2,h)
    self.x1 = x1
    self.y1 = y1
    self.x2 = x2
    self.y2 = y2
    if h then
        horizontal = true
        self.wall1 = physics.body(EDGE,vec2(self.x1,self.y1 - 5),vec2(self.x2,self.y1 - 5))
        self.wall2 = physics.body(EDGE,vec2(self.x1,self.y1 + 5),vec2(self.x2,self.y2 + 5))
        self.wall1.info = "UpWall"
        self.wall2.info = "DownWall"
    else
        horizontal = false
        self.wall1 = physics.body(EDGE,vec2(self.x1 - 5,self.y1),vec2(self.x1 - 5,self.y2))
        self.wall2 = physics.body(EDGE,vec2(self.x1 + 5,self.y1),vec2(self.x1 + 5,self.y2))
        self.wall1.info = "LeftWall"
        self.wall2.info = "RightWall"
    end
end

function Walls:draw()
    pushStyle()
    fill(0, 0, 0, 255)
    stroke(127, 127, 127, 255)
    strokeWidth(3)
    if horizontal then
        rect(self.x1,self.y1 - 5,self.x2 - self.x1,10)
    else
        rect(self.x1 - 5,self.y1,10,self.y2 - self.y1)
    end
    popStyle()
end

As it is, horizontal is a global variable. You need to make it specific to the instance. Rename it self.horizontal.

Thanks!! @Andrew_Stacey

You can streamline the code a little more by taking the conditional out of the draw() function. If you have two draw functions, say drawh and drawv then in the init function you can do self.draw = self.drawh or self.draw = self.drawv as appropriate.

Nice. Thanks for the tip. I didn’t think about two draw functions.