Block Placing

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Astro
Prole
Posts: 10
Joined: Fri May 02, 2014 12:02 pm
Location: Illinois, United States of America

Block Placing

Post 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
Attachments
Sierra.love
(5.43 KiB) Downloaded 89 times
Never underestimate the power of a programming language.
User avatar
HugoBDesigner
Party member
Posts: 403
Joined: Mon Feb 24, 2014 6:54 pm
Location: Above the Pocket Dimension
Contact:

Re: Block Placing

Post 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!
@HugoBDesigner - Twitter
HugoBDesigner - Blog
User avatar
Astro
Prole
Posts: 10
Joined: Fri May 02, 2014 12:02 pm
Location: Illinois, United States of America

Re: Block Placing

Post 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!
Never underestimate the power of a programming language.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 5 guests