OOP Map Question
Posted: Sun Mar 13, 2022 11:58 am
I have a project in wich i have a Block class
and i have a problem with drawing these Blocks, when theyre created in a for loop.
I can imagine you somehow have to put the blocks created in the loop into a table, wich you then draw using my Block:draw() function, but i cant grasp this.
Code: Select all
Block = {}
Block.__index = Block
function Block.new(x, y, r, g, b )
local instance = setmetatable({}, Block)
instance.x = x
instance.y = y
instance.width = 50
instance.height = 50
instance.r = r
instance.g = g
instance.b = b
instance.body = love.physics.newBody(world, instance.x, instance.y, "static")
instance.shape = love.physics.newRectangleShape(instance.width, instance.height)
instance.fixture =love.physics.newFixture(instance.body, instance.shape)
return instance
end
function Block:draw()
love.graphics.rectangle("fill", self.x - self.width / 2, self.y - self.height / 2, self.width, self.height)
end
Code: Select all
Blocks = {0, 0, 1, 1, 1, 0}
for i, block in ipairs(Blocks) do
Block.new(--some magic involving i)
end