How to create a new item with a mouse click?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
-
- Prole
- Posts: 6
- Joined: Wed Feb 10, 2016 5:05 am
- Location: Conway, South Carolina
- Contact:
How to create a new item with a mouse click?
So I have been learning LOVE2d for a little bit now, and I am just curious on how I would create a new "block" by clicking on the screen? I have this on my Git to show what I have been doing: https://github.com/BlakeBarnes00/Love2d ... 20-%20Ball
-
- Party member
- Posts: 730
- Joined: Sat Apr 26, 2014 7:46 pm
Re: How to create a new item with a mouse click?
Use the love.mouse pressed callback. Then add a block to your game.
Re: How to create a new item with a mouse click?
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: 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:
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: modify it to 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: and change it to: 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
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)
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)
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()))
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
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
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
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
-
- Party member
- Posts: 730
- Joined: Sat Apr 26, 2014 7:46 pm
Re: How to create a new item with a mouse click?
Woah bro nice and thorough. I felt as though he could have discovered the rest. (That and I also didn't have time to write a good response)
Re: How to create a new item with a mouse click?
Thanks, I try my best. I replied mostly because I was working on something similar recently, I'm only learning myself, just thought of giving back to the community a little by sharing what I've learned so far.bobbyjones wrote:Woah bro nice and thorough. I felt as though he could have discovered the rest. (That and I also didn't have time to write a good response)
-
- Prole
- Posts: 6
- Joined: Wed Feb 10, 2016 5:05 am
- Location: Conway, South Carolina
- Contact:
Re: How to create a new item with a mouse click?
Hey man, this was awesome. I just figured out tables today when making another game. Thanks so much!tuupakku wrote: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: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 = {} 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)
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
--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)
modify it toCode: 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()))
so that we iterate through our list of blocks and draw all of them.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
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: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 objects.blocks.body:setPosition(mouseX, mouseY) objects.blocks.body:setLinearVelocity(1, 0) end
which is similar to the code which we used to create the first block.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
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
Re: How to create a new item with a mouse click?
No problem mate. Godspeed.BlakeBarnes00 wrote:Hey man, this was awesome. I just figured out tables today when making another game. Thanks so much!
Who is online
Users browsing this forum: Ahrefs [Bot], Google [Bot] and 4 guests