How to create a new item with a mouse click?

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
BlakeBarnes00
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?

Post 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
bobbyjones
Party member
Posts: 730
Joined: Sat Apr 26, 2014 7:46 pm

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

Post by bobbyjones »

Use the love.mouse pressed callback. Then add a block to your game.
User avatar
tuupakku
Prole
Posts: 21
Joined: Fri Feb 12, 2016 12:12 pm

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

Post 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 :)
bobbyjones
Party member
Posts: 730
Joined: Sat Apr 26, 2014 7:46 pm

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

Post 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)
User avatar
tuupakku
Prole
Posts: 21
Joined: Fri Feb 12, 2016 12:12 pm

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

Post 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.
BlakeBarnes00
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?

Post 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!
User avatar
tuupakku
Prole
Posts: 21
Joined: Fri Feb 12, 2016 12:12 pm

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

Post 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.
Post Reply

Who is online

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