Clicking inside a rectangle

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
NNck123
Prole
Posts: 1
Joined: Sun May 22, 2022 12:30 pm

Clicking inside a rectangle

Post by NNck123 »

Is there a way to optimize this code? A am new at game development, so I tried to create a simple clicker game where you need to click inside a rectangle to earn points. But i don't think my way is the best way to do this. Code below. Also sorry for bad eng.

Code: Select all

function click()
  function love.mousepressed(x, y, button, istouch, presses)
    if button == 1
    and count >= 0
    and x > box1.x
    and x < box1.x + box1.width
    and y > box1.y
    and y < box1.y + box1.height then
      count = count + 1
      clicks = clicks + 1
    elseif button == 1
    and count >= 10
    and x > box2.x
    and x < box2.x + box2.width
    and y > box2.y
    and y < box2.y + box2.height then
      count = count + 2
      clicks = clicks + 1
    end
  end
end
User avatar
BrotSagtMist
Party member
Posts: 607
Joined: Fri Aug 06, 2021 10:30 pm

Re: Clicking inside a rectangle

Post by BrotSagtMist »

Not really, there are 4 borders so you need 4 conditions.
But you probably should not include button type and counting check in the same if case, its kinda messy.
Also your elseif branch will never get triggered. My quick edit would be:

Code: Select all

function love.mousepressed(x, y, button, istouch, presses)
 if button == 1  and count >= 10 then
  if x > box1.x
  and x < box1.x + box1.width
  and y > box1.y
  and y < box1.y + box1.height then
   count = count + 1
   clicks = clicks + 1
  end
 elseif button==1 then
  if x > box2.x
  and x < box2.x + box2.width
  and y > box2.y
  and y < box2.y + box2.height then
   count = count + 2
   clicks = clicks + 1
  end
 end
end
obey
User avatar
BrotSagtMist
Party member
Posts: 607
Joined: Fri Aug 06, 2021 10:30 pm

Re: Clicking inside a rectangle

Post by BrotSagtMist »

On a second thought such minor changes like different numbers can be dealt with using and/or constructs quite well.
But that is something beginners rarely use:

Code: Select all

function love.mousepressed(x, y, button, istouch, presses)
 if button == 1 
 and  x > box1.x
 and x < box1.x + box1.width
 and y > box1.y
 and y < box1.y + box1.height then
  count = count<11 and count + 1 or count +2
  clicks = clicks + 1
 end
end
obey
User avatar
pgimeno
Party member
Posts: 3544
Joined: Sun Oct 18, 2015 2:58 pm

Re: Clicking inside a rectangle

Post by pgimeno »

Placing function love.mousepressed() inside function click() is an awful idea.

I'd also suggest to make a function to check if a point is inside a rectangle, to make your life easier when you're adding more buttons:

Code: Select all

function inside(box, x, y)
  return x >= box.x 
    and x < box.x + box.width
    and y >= box.y
    and y < box.y + box.height
end

function love.mousepressed(x, y, button, istouch, presses)
  if button == 1 then
    if inside(box1, x, y) then
      clicks = clicks + 1
      if count < 10 then
        count = count + 1
      else
        count = count + 2
      end
    end -- box1

    if inside(box2, x, y) then
       -- do stuff for box2
    end -- box2
  end -- button = 1
end -- function
User avatar
milon
Party member
Posts: 472
Joined: Thu Jan 18, 2018 9:14 pm

Re: Clicking inside a rectangle

Post by milon »

I agree with pgimeno. Callbacks shouldn't be nested within other functions. Also, here's my tweak of pgimeno's code to handle multiple boxes easier. And since it's dynamic, it can work with any number of boxes and each one can have its own separate behavior.

Code: Select all

local boxes = {} -- put box1, box2, etc in here as boxes[1], boxes[2], etc

...

function boxes[1].click() -- each clickable boxes[] entry also needs a click() function to handle user input
  clicks = clicks + 1
  if count < 10 then
    count = count + 1
  else
    count = count + 2
  end
end

...

function love.mousepressed(x, y, button) -- I've discarded 'istouch' and 'presses' but you can do whatever you like here
  if button == 1 then
    for i = 1, #boxes do
      if inside(boxes[i], x, y) and boxes[i].click then boxes[i].click() end
    end
  end
end -- function
If you want to see a working example of this, have a look at my Wordle-clone that I recently posted. It's still very much WIP, but I'm pretty happy with how I handle UI events.
Any code samples/ideas by me should be considered Public Domain (no attribution needed) license unless otherwise stated.
Post Reply

Who is online

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