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.
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
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:
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
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:
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
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
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.
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.