Need help with limiting mouse clicks

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
User avatar
PlatinumFaceGirl2
Prole
Posts: 4
Joined: Sat Jan 04, 2014 5:18 pm

Need help with limiting mouse clicks

Post by PlatinumFaceGirl2 »

I am currently working on a game, memory game, almost done, everything working fine by now, but I have little problem and needs to be fixed. Everyone has seen a memory game before, you need to click 2 items, and if they match they go away, if they don't match they stay and you continue... My problem here is that when 2 objects are clicked , program checks only those in that moment if they match or not,but the time when they are open until they flip back again (love.timer.getTime()+1) if you click fast in that one second you can open probably 4-5 more . The program does not react to them , it has only the 2 first clicked in the memory. Can someone help me fixing this problem? Maybe by limiting mouse clicks when 2 items are already clicked? And how to do that? I will appreciate every answer back. Thank you
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Need help with limiting mouse clicks

Post by micha »

Try having a game state variable. That is a string that contains information about the current state of the game. In the beginning when the game is waiting for the player to click it could have the value "waitingForFirst" then when the player clicks onto an image, it changes to "waitingForSecond" and when the player clicks onto another one (which is different from the first one) then it should change to "comparing". While in the state "comparing" clicks just do nothing and after a second or so, the two images are removed or put back and the state goes to "waitingForFirst" again.
User avatar
Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

Re: Need help with limiting mouse clicks

Post by Plu »

In a simple case like this one, you can also add to your mouse-click handler a check that measures the number of items clicked and simply refuses to do anything if there is already two open. (For larger projects, micha's suggestion is a better one though!)

Pseudocode:

Code: Select all

function love.mousepressed( x, y, button )
  if tilesOpen() >= 2 then
    return false
  end
  openClickedTile()
end
User avatar
PlatinumFaceGirl2
Prole
Posts: 4
Joined: Sat Jan 04, 2014 5:18 pm

Re: Need help with limiting mouse clicks

Post by PlatinumFaceGirl2 »

Code: Select all

local clicked = love.mouse.isDown( "l" )
          if game.scene.mode == 1 or game.scene.mode == 2 then -----choose 2 to check
			for i = #objects.cards,1,-1 do
				local c = objects.cards[i]
				if mx > c.sprite.rect[1] and mx < c.sprite.rect[1] + c.sprite.rect[3]
				and my > c.sprite.rect[2] and my < c.sprite.rect[2] + c.sprite.rect[4] then -- mouse is over an object
					if clicked and love.mouse.wasDown == false then
						if c.state == 1...

Code: Select all

function getFullCardLayout()
	local cardLayout = {}
	---
	cardLayout[1] = {407,84}
	cardLayout[2] = {494,84}
	cardLayout[3] = {581,79}
	cardLayout[4] = {669,74}
	cardLayout[5] = {760,73}
	cardLayout[6] = {853,74}

Code: Select all

local all_cards_flipped = true
				for i = 1,#objects.cards,1 do
					local c = objects.cards[i]
					if c.state ~= 3 then
						all_cards_flipped = false ---one pair is active for checking
						break
					end
				end
				if all_cards_flipped then --all cards are closed 

Code: Select all

function love.mousepressed( x, y, button )
  if clicked == '1' and ? then
  return false
  end
  all_cards_flipped ?
end
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Need help with limiting mouse clicks

Post by micha »

Does that mean you have solved the problem or do you have a specific question?
User avatar
PlatinumFaceGirl2
Prole
Posts: 4
Joined: Sat Jan 04, 2014 5:18 pm

Re: Need help with limiting mouse clicks

Post by PlatinumFaceGirl2 »

yeah, i got it, i had little trouble while forming it right, but now its fine. Thank you Plu and micha
Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests