Need a Little help with tables

Im trying to learn a little more on tables, but i got stuck…

Can someone tell me whats wrong?


-- Platformer

-- Use this function to perform your initial setup
function setup()
    print("Hello World!")
    meshes = {}
    for i = 0, 10 do
        table.insert(meshes, i, Mesh:init(vec2(math.random(200),math.random(200)), vec2(math.random(200), math.random(200)), vec2(math.random(200), math.random(200))))
        end
    circle = physics.body(CIRCLE, 50)
    circle.x = 0
    circle.y = 500
    circle.restitution = 0.5
end

-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(40, 40, 50)
    fill(255, 0, 0, 255)
    ellipse(circle.x, circle.y, circle.radius*2)

    -- This sets the line thickness
    strokeWidth(5)
    for i,v in ipairs(meshes) do
        meshes[i]:draw()
        end

    
    physics.gravity(Gravity)

    -- Do your drawing here
    
end


Mesh = class()

function Mesh:init(v1, v2, v3)
    mesher = mesh()
    mesher.vertices = {v1,v2,v3}
    mesher:setColors(255,255,255,255)
    ground = physics.body(POLYGON, v1,v2,v3)
    ground.type = STATIC
end

function Mesh:draw()
    mesher:draw()
end

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

Okay, i got it working, but how its only rendering 1 mesh and cresting the grounds normally
(It collides with unrendered grounds), why is that?


-- Platformer

-- Use this function to perform your initial setup
function setup()
    print("Hello World!")
    meshes = {}
    for i = 0, 10 do
        oi = Mesh
        table.insert(meshes, i, oi)
        oi:init(vec2(math.random(200),math.random(200)), vec2(math.random(200), math.random(200)), vec2(math.random(200), math.random(200)))
        end
    circle = physics.body(CIRCLE, 50)
    circle.x = 0
    circle.y = 500
    circle.restitution = 0.5
end

-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(40, 40, 50)
    fill(255, 0, 0, 255)
    ellipse(circle.x, circle.y, circle.radius*2)

    -- This sets the line thickness
    strokeWidth(5)
    for i,v in pairs(meshes) do
        meshes[i]:draw()
        end

    
    physics.gravity(Gravity)

    -- Do your drawing here
    
end



Mesh = class()

function Mesh:init(v1, v2, v3)
    mesher = mesh()
    grounde = physics.body(POLYGON, v1, v2, v3)
    
    mesher.vertices = {v1,v2,v3}
    mesher:setColors(255,255,255,255)
    
    grounde.type = STATIC
end

function Mesh:draw()
    mesher:draw()
end

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

Hey @MarcosCapistrano, an easier fix to your first problem would be to just remove the :init(…) after Mesh in the setup function

Hi, i’ll try to give you some suggestions as I am not really sure myself:
in your setup() put the stuff with the brackets after “oi:init” after “oi = Mesh”.
and as jordan suggested delete the oi:init.

if that doesn’t do it i guess somethings wrong with you class also.

I have fixed your code. The problem was that you were calling grounde a global variable, so each Mesh class remade it into a different one, deleting the old one. And same with mesher. there is this trick with a variable called self, which is useful. Here is my code, see if you can understand it:




-- Platformer

-- Use this function to perform your initial setup
function setup()
    print("Hello World!")
    meshes = {}
    for i = 1, 3 do
        table.insert(meshes, i, Mesh(vec2(math.random(200),math.random(200)), vec2(math.random(200), math.random(200)),
        vec2(math.random(200), math.random(200))))
        end
    circle = physics.body(CIRCLE, 50)
    circle.x = 0
    circle.y = 500
    circle.restitution = 0.5
end

-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(40, 40, 50)
    fill(255, 0, 0, 255)
    ellipse(circle.x, circle.y, circle.radius*2)

    -- This sets the line thickness
    strokeWidth(5)
    for i,v in pairs(meshes) do
        v:draw()
        
    end


    physics.gravity(Gravity)

    -- Do your drawing here

end




Mesh = class()

function Mesh:init(v1, v2, v3)
    self.mesher = mesh()
    self.grounde = physics.body(POLYGON, v3, v2, v1)

    self.mesher.vertices = {v1,v2,v3}
    self.mesher:setColors(255,255,255,255)

    self.grounde.type = STATIC
end

function Mesh:draw()
    self.mesher:draw()
end

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