Page 1 of 1

How to create a new item with a mouse click?

Posted: Thu Feb 11, 2016 11:10 pm
by BlakeBarnes00
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

Re: How to create a new item with a mouse click?

Posted: Fri Feb 12, 2016 1:17 am
by bobbyjones
Use the love.mouse pressed callback. Then add a block to your game.

Re: How to create a new item with a mouse click?

Posted: Fri Feb 12, 2016 9:30 pm
by tuupakku
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:
Image

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?

Posted: Sat Feb 13, 2016 12:48 am
by bobbyjones
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?

Posted: Sat Feb 13, 2016 1:31 am
by tuupakku
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)
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.

Re: How to create a new item with a mouse click?

Posted: Sat Feb 13, 2016 5:53 am
by BlakeBarnes00
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:

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:
Image

This is but one of many ways to do this. With this knowledge you can modify your code to do whatever it needs to :)
Hey man, this was awesome. I just figured out tables today when making another game. Thanks so much!

Re: How to create a new item with a mouse click?

Posted: Sat Feb 13, 2016 11:47 am
by tuupakku
BlakeBarnes00 wrote:Hey man, this was awesome. I just figured out tables today when making another game. Thanks so much!
No problem mate. Godspeed.