Page 1 of 1

Block Placing

Posted: Wed Jul 16, 2014 6:08 pm
by Astro
I'm working on a new project and am having slight issues with placing blocks in game. When the player clicks, the game checks what block they have in their hand. If it is "scrap_metal", it will place that block. My problem comes when we go to actually draw the block. Each time you click again, the old block is moved to the new location. This is because each time you click (after moving the mouse), you're getting a new position of the mouse (which is where the block is drawn). What I want to do is to create a value for each block's location when it is placed, so it has its own static location and thus it won't move when the player places another block. How would I go about doing this?

Code: Select all

function love.draw()
for i = 0, blocks.scrapmetal do
love.graphics.draw(smetal, tx, ty)  -- Each time you click, the block moves due to the mouse moving!
end
end

function love.mousepressed()
if player.inv.tool == "scrap_metal" then
blocks.scrapmetal = blocks.scrapmetal + 1
tx = love.mouse.getX()
ty = love.mouse.getY()
end

end

Re: Block Placing

Posted: Wed Jul 16, 2014 6:27 pm
by HugoBDesigner
I see your problem. You're only considering a single block object. Do this:

In love.load:

Code: Select all

blocks.scrapmetal = {}
In love.draw:

Code: Select all

for i, v in pairs(blocks.scrapmetal) do
    love.graphics.draw(smetal, v.x, v.y)
end
In love.mousepressed:

Code: Select all

if player.inv.tool == "scrap_metal" then
    table.insert(blocks.scrapmetal, {x = love.mouse.getX(), y = love.mouse.getY()}
end
This should work. If you have any other problem let me know. I hope I helped!

Re: Block Placing

Posted: Wed Jul 16, 2014 6:32 pm
by Astro
HugoBDesigner wrote: This should work. If you have any other problem let me know. I hope I helped!
Thanks a lot! Works excellently now!