Crashing Problem

Ok so here’s my code to start with

--# Main
--# Main
-- class
 
-- Use this function to perform your initial setup
function setup()
    b={}
    for i=1,1 do
        b[i]=Ball()
    end
    --Custom Balls

    --End Custom Balls  

                    CreateWalls:draw()

end
 
-- This function gets called once every frame
function draw()
print(collectgarbage("count"))
    
        background(195, 195, 201, 255)
                b[1]:draw()



    

end
 
--this function creates one wall (actually just a line)

--# CreateWalls
CreateWalls = class()

function CreateWalls:init(x,y,x1,y1,r)
    -- you can accept and set parameters here
    w = physics.body(EDGE,vec2(x,y),vec2(x1,y1)) -- vec2
    w.restitution=r --see comment above
    return w

end

function CreateWalls:draw()
    -- Codea does not automatically call this method
    leftWall = CreateWalls(1,1,1,HEIGHT-1,1.0) 
    rightWall = CreateWalls(WIDTH-1,0,WIDTH-1,HEIGHT-1,0.9) 
    bottomWall = CreateWalls(1,1,WIDTH-1,1,0.2) 
    topWall = CreateWalls(1,HEIGHT-1,WIDTH-1,HEIGHT-1,0.7)
end

function CreateWalls:touched(touch)
    -- Codea does not automatically call this method
end

--# Ball
--# Ball
Ball = class()
 
function Ball:init(x,y,d,c)

    self.x=x or math.random(0,WIDTH)
    self.y=y or math.random(0,HEIGHT)
    self.diameter=d or math.random(50,200)
    self.colr=c or color(math.random(0,255),math.random(0,255),math.random(0,255))
    self.p=Physics(CIRCLE,{self.diameter/2},self.x,self.y) --physics uses radius
end
 
function Ball:draw()
    pushStyle() -- store style settings
    fill(self.colr) --set color
    pushMatrix()
    local x,y,a=self.p:currentPosition()
    translate(x,y)
    rotate(a)
    ellipse(0,0,self.diameter)
    popMatrix()
    popStyle() -- put back style settings
end

--# Physics
--# Physics
Physics = class()
 
--type=CIRCLE or POLYGON
--data is a table. For circles it is one value, the radius, while for a polygon it is a set of vec2
--x,y is the initial position of the centre
--a is the initial angle
--g is the gravity value, 0=tabletop, 1=normal (things fall down)
--r is the restitution, ie springiness
--f is friction
--lv is linear velocity
--i is any info you want to store to identify this object, eg a name
function Physics:init(type,data,x,y,a,g,r,f,lv,i)  
    if type==CIRCLE then
        self.body=physics.body(CIRCLE,data[1]) 
    elseif type==POLYGON then
        self.body=physics.body(POLYGON,unpack(data))
    end
    self.body.x=x
    self.body.y=y
    self.body.angle=a or 0
    if g then self.body.gravityScale=1 else self.body.gravityScale=0 end
    self.body.restitution=r or 1
    self.body.friction=f or 0.1
    if lv==nil then lv=vec2(100+math.random(400),100+math.random(400)) end
    self.body.linearVelocity=lv
    self.body.info=i
end
 
function Physics:currentPosition()
    return self.body.x, self.body.y,self.body.angle
end 

please excuse how messy and unnecessary parts of this code are, I have cut pieces out everywhere trying to stop this project from crashing. I have the collect garbage thingy running there to show me how much memory is being used by Codea and this number was increasing very high(10/second or so) before I moved createwalls:draw() out of the main draw function. Now it is moved though the garbage count is increasing about 2/second until about 240 then goes down and reaches a point in the 200’s where it decides it will let my ball run through the walls, off the screen to never come back. If I place createwalls:draw() back in the main draw function and not in the main setup this does not happen but the app crashes quite quickly. I’m not sure why I am accumulating so much garbage as I can’t see anything that should be causing me any harm. Any help would be appreciated

Cheers, Brodie

One problem I can see is the way you run CreateWalls:draw()

This function is inside a class, so you should be saying something like

walls=CreateWalls()
walls:draw()

ie you need to create a variable to hold an instance of the class, then you can run draw.

It’s not a good idea to use a class to create global variables like leftWall. Ideally, all variables created by a class, are local to that class.

Better is

CreateWalls.leftWall = CreateWalls(1,1,1,HEIGHT-1,1.0)

which makes leftWall a variable within the CreateWalls class

But I think using a class to create more instances of itself as you are doing there, is unnecessary.

Why not just do this?

--in setup, call a function to make walls
SetupWalls()

--and in separate functions, no need for a class
function SetupWalls()
    leftWall = CreateWall(1,1,1,HEIGHT-1,1.0) 
    rightWall = CreateWall(WIDTH-1,0,WIDTH-1,HEIGHT-1,0.9) 
    bottomWall = CreateWall(1,1,WIDTH-1,1,0.2) 
    topWall = CreateWall(1,HEIGHT-1,WIDTH-1,HEIGHT-1,0.7)
end

function CreateWall(x,y,x1,y1,r)
    w = physics.body(EDGE,vec2(x,y),vec2(x1,y1)) 
    w.restitution=r 
    return w
end

Thanks for that, that will work perfectly.