No errors but not working: Dragging&Selecting

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
Raxe88
Prole
Posts: 15
Joined: Mon Jul 29, 2013 11:14 pm

No errors but not working: Dragging&Selecting

Post by Raxe88 »

Hi! I think this is the second time I ask help here just because I suck at programming but I'm starting to get it. The thing is that I made a dragging with the mouse and selection "thing":
Image
Image

The bad thing comes when I try to select actual objects like the little square I made. I'm not sure if all the if statements I did for checking if the selection box that covers the X, Y, X+Width and Y+Height of the cube are correct but it should on my understanding.

So! This is my code:

Code: Select all

--Hello	
	--//Functions
function love.load()
	print("Hi there! Console talking! Wassup?")
	--print(npcs[1].X)
	
	--//Declaring some variables
	npcNumber = 1
	mouseX = love.mouse.getX()
	mouseY = love.mouse.getY()
	oldMouseX = 0
	oldMouseY = 0
	selectionX = 0
	selectionY = 0
	selection = false
	selected = false
	--//Making some entities! :D
	npc1 = {}
	npc1.X = math.random(100,900) --RandomGenerator:random(100,900)
	npc1.Y = math.random(100,500) --RandomGenerator:random(100,500)
	npc1.Height = 16
	npc1.Width = 16
	npc1.mode = "fill"
	--npc1.image = npcSkinImage
	
	npcs = {npc1}
end
--[[function love.keypressed(key)

end]]--
function love.mousepressed(x,y,button)
	if button == "l" then
		oldMouseX = love.mouse.getX()
		oldMouseY = love.mouse.getY()
		print("Left button pressed")
		if selection == false then
			selection = true
		end
		if selection == true and npcs[1].X > oldMouseX and npcs[1].X + npcs[1].Width < oldMouseX + selectionX and npcs[1].Y > oldMouseY and npcs[1].Y + npcs[1].Height < oldMouseY + selectionY then
			print("One NPC selected")
			if selected == false then
				selected = true
			else
				selected = true
			end
		end
	end
end
function love.mousereleased(x,y,button)
	if button == "l" then
		if selection == true then
			selection = false
		end
	end
end
function love.draw()
	love.graphics.print("NPCs Number: " .. npcNumber,0,0)
	love.graphics.print("Mouse X: " .. mouseX,0,10)
	love.graphics.print("Mouse Y: " .. mouseY,0,20)
	love.graphics.print("Old MouseX: " .. oldMouseX,0,30)
	love.graphics.print("Old MouseY: " .. oldMouseY,0,40)
	love.graphics.print("SquareX: " .. npcs[1].X,0,50)
	love.graphics.print("SquareY: " .. npcs[1].Y,0,60)
	love.graphics.print("Selection: " .. tostring(selection),0,70)
	love.graphics.print("SelectionX : " .. selectionX,0,80)
	love.graphics.print("SelectionY : " .. selectionY,0,90)
	love.graphics.print("Selected: " .. tostring(selected),0,100)
	if selected == true then
		love.graphics.setColor(255,0,0,255)
	else
		love.graphics.setColor(255,255,255,255)
	end
	love.graphics.rectangle(npcs[1].mode,npcs[1].X,npcs[1].Y,npcs[1].Width,npcs[1].Height)
	if selection == true then
		love.graphics.setColor(0,0,255,100)
		love.graphics.rectangle("fill",oldMouseX,oldMouseY,selectionX,selectionY)
	end
	love.graphics.setColor(255,255,255,255) -- Back to its normal colors
end
function love.update()
	mouseX = love.mouse.getX()
	mouseY = love.mouse.getY()
	selectionX = mouseX - oldMouseX
	selectionY = mouseY - oldMouseY
end
If you happen to know a better way of coding this let me know! Here's the .love file: https://www.dropbox.com/s/5hqu4zlxmkpag2e/Dragging.love

Also: I thought that if the selection square has negative numbers for the width and the height it might couse some problems, any idea? Thanks for spending your time helping me! :D
Last edited by Raxe88 on Wed Aug 21, 2013 1:09 pm, edited 1 time in total.
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: No errors but not working: Dragging&Selecting

Post by davisdude »

A .love would be greatly appreciated. Are you using a batch file? Because if so, it's likely you forgot the '.' at the end of the whole mess of things. Happens to everybody.
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
Raxe88
Prole
Posts: 15
Joined: Mon Jul 29, 2013 11:14 pm

Re: No errors but not working: Dragging&Selecting

Post by Raxe88 »

Ok, I found out a way of making the .love work, the file will be uploaded now :D
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: No errors but not working: Dragging&Selecting

Post by davisdude »

Note that I only changed the screen height and width to fit my monitor. Ergo, I also had to change the NPC spawn numbers a bit.

I haven't tested it yet, but I don't think you can have negative coordinates for the width and height, but regardless, it shouldn't effect computation too much.
Attachments
Dragging.love
(1.12 KiB) Downloaded 185 times
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
Raxe88
Prole
Posts: 15
Joined: Mon Jul 29, 2013 11:14 pm

Re: No errors but not working: Dragging&Selecting

Post by Raxe88 »

I'm testing this you did but simply doesn't work. I can't select when I click and drag.
Image
The first thing I notice is that the selection = true isn't there, I'll change and see what happens.
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: No errors but not working: Dragging&Selecting

Post by davisdude »

It worked when I did it. I don't know why it's not working for you. I always test before I post anything. Maybe you could repost the .love. I might have forgotten to transfer it (I hate working with zipped folders, You can't edit anything).
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
Raxe88
Prole
Posts: 15
Joined: Mon Jul 29, 2013 11:14 pm

Re: No errors but not working: Dragging&Selecting

Post by Raxe88 »

This is what I downloaded from yours:
Dragging.love
(1.12 KiB) Downloaded 366 times
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: No errors but not working: Dragging&Selecting

Post by davisdude »

It works perfectly for me. Are you clicking on the square? I honestly don't know what else it could be...
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
Raxe88
Prole
Posts: 15
Joined: Mon Jul 29, 2013 11:14 pm

Re: No errors but not working: Dragging&Selecting

Post by Raxe88 »

This was a mis understanding, what I wanted is to make the square be selected when the selection square covered it. It works perfectly but I was trying to select the square instead of click and drag it. This is what I am aiming at:
Image
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 6 guests