Page 1 of 1

Clicking inside a rectangle

Posted: Sun May 22, 2022 12:45 pm
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

Re: Clicking inside a rectangle

Posted: Sun May 22, 2022 5:04 pm
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

Re: Clicking inside a rectangle

Posted: Sun May 22, 2022 5:08 pm
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

Re: Clicking inside a rectangle

Posted: Sun May 22, 2022 6:10 pm
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

Re: Clicking inside a rectangle

Posted: Tue May 24, 2022 2:50 pm
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.