Hello Blake,
I assume what you meant is creating a new block at the click of the mouse, instead of repositioning the current one.
Well, looking at the code in objects.lua, in this section:
Code: Select all
--Creating blocks
objects.blocks = {}
objects.blocks.body = love.physics.newBody(world.world, 100, 550, "dynamic")
objects.blocks.shape = love.physics.newRectangleShape(100, 50)
objects.blocks.fixture = love.physics.newFixture(objects.blocks.body, objects.blocks.shape)
your objects.blocks table can only hold a single block. Instead what we want is to make a list of blocks, which leads us to the code:
Code: Select all
--Creating blocks
objects.blocks = {}
block = {}
block.body = love.physics.newBody(world.world, 100, 550, "dynamic")
block.shape = love.physics.newRectangleShape(100, 50)
block.fixture = love.physics.newFixture(block.body, block.shape)
table.insert(objects.blocks, block)
Now we can hold multiple blocks inside our table, now we have to change how we iterate on those blocks to reflect that change. In this part of the code in objects.lua:
Code: Select all
--Drawing the blocks
love.graphics.setColor(50, 50, 50) -- set the drawing color to grey for the blocks
love.graphics.polygon("fill", objects.blocks.body:getWorldPoints(objects.blocks.shape:getPoints()))
modify it to
Code: Select all
--Drawing the blocks
love.graphics.setColor(50, 50, 50) -- set the drawing color to grey for the blocks
for _, block in pairs(objects.blocks) do
love.graphics.polygon("fill", block.body:getWorldPoints(block.shape:getPoints()))
end
so that we iterate through our list of blocks and draw all of them.
Next what we want to do is create a new block when the mouse is clicked, rather than changing the position of the current block. So go to controls.lua and find the part where it says:
Code: Select all
function love.mousepressed( mouseX, mouseY, button, istouch ) -- This is so that we can place the block back down
if button == 1 then
objects.blocks.body:setPosition(mouseX, mouseY)
objects.blocks.body:setLinearVelocity(1, 0)
end
and change it to:
Code: Select all
function love.mousepressed( mouseX, mouseY, button, istouch ) -- This is so that we can place the block back down
if button == 1 then
block = {}
block.body = love.physics.newBody(world.world, mouseX, mouseY, "dynamic")
block.body:setLinearVelocity(1, 0)
block.shape = love.physics.newRectangleShape(100, 50)
block.fixture = love.physics.newFixture(block.body, block.shape)
table.insert(objects.blocks, block)
end
which is similar to the code which we used to create the first block.
And that's pretty much it! Here's the end result:
This is but one of many ways to do this. With this knowledge you can modify your code to do whatever it needs to