Page 1 of 3
How do you simple make a button?
Posted: Wed Jul 20, 2011 11:15 pm
by GijsB
I DID look at other code, but its always way too overcomplicated(i think?)
whats the simplest way?
Re: How do you simple make a button?
Posted: Wed Jul 20, 2011 11:29 pm
by kraftman
You need x and y coordinates to position the button, and a width and height to give it some substance.
You then use the mousepressed function to check if the location of the mouse when it was pressed lies within the confines of your button. If you load an image, and set it to the same position and size, it will show people where to click.
Re: How do you simple make a button?
Posted: Thu Jul 21, 2011 12:06 am
by GijsB
so actualy have to make some collision detection with a mouse and a image :O?
thats pretty sad 3:
why can't LÖVE ad a function like when an image can mousepressed? =
image = blabla image
image.MousePressed:connect()
Re: How do you simple make a button?
Posted: Thu Jul 21, 2011 12:10 am
by TechnoCat
GijsB wrote:so actualy have to make some collision detection with a mouse and a image :O?
thats pretty sad 3:
why can't LÖVE ad a function like when an image can mousepressed? =
image = blabla image
image.MousePressed:connect()
LOVE tries to stay relatively low level. It also doesn't try to create a single solution for everybody's problems. Everyone has different needs.
On a different note, the community is here to help. (as kraftmen so kindly spent time doing already)
And we help a lot.
Re: How do you simple make a button?
Posted: Thu Jul 21, 2011 12:13 am
by GijsB
oke
Re: How do you simple make a button?
Posted: Thu Jul 21, 2011 12:19 am
by GijsB
oh and, is there any way i can optimise my code?(its a base of a puzzle game im making) =
diffeculty = 7
size = 500/diffeculty--logic
BOXES = {}
math.randomseed(os.time())
for x = 1,diffeculty do
for y = 1,diffeculty do
BOX = {O = math.random(0,1), X = x, Y = y}
table.insert(BOXES,BOX)
end
end
function findnumberx(x)
n = ""
c = 0
for i,v in pairs(BOXES) do
if v.X == x then
if v.O == 1 then
c = c+1
else
if c > 0 then
n = n..tostring(c)..","
end
c = 0
end
end
end
if c > 0 then
n = n..tostring(c)..","
end
return n
end
function findnumbery(y)
n = ""
c = 0
for i,v in pairs(BOXES) do
if v.Y == y then
if v.O == 1 then
c = c+1
else
if c > 0 then
n = n..tostring(c)..","
end
c = 0
end
end
end
if c > 0 then
n = n..tostring(c)..","
end
return n
end
function love.draw()
for i,v in pairs(BOXES) do
love.graphics.draw(love.graphics.newImage("BUTTONOFF.png"),v.X*size,v.Y*size,0,size/30,size/30,0,0)--30 pixels width images, so size/pixels
end
for x = 1,diffeculty do
love.graphics.print(findnumberx(x), x*size, size-30)
end
for y = 1,diffeculty do
love.graphics.print(findnumbery(y), size-30, y*size)
end
end
Re: How do you simple make a button?
Posted: Thu Jul 21, 2011 12:32 am
by Robin
You don't have to worry about optimization, especially not now. Try finishing your game first, since that is much more important.
Re: How do you simple make a button?
Posted: Thu Jul 21, 2011 1:21 am
by GijsB
umm my collision detection doesnt work 3:
local ap = {x = v.X, y = v.Y}
local as = {x = size, y = size}
local bp = {x = mx, y = my}
local bs = {x = 1,y = 1}
E = ap.x + as.x > bp.x and bp.x + bs.x > ap.x and ap.y + as.y > bp.y and bp.y + bs.y > ap.y
gives wrong answer
v is a table of the position of the image, and mx and my are mouse positions
Re: How do you simple make a button?
Posted: Thu Jul 21, 2011 7:01 am
by T-Bone
First of all, use code blocks here in the forums when you post code, makes it easier to read (and keeps indentation). You use it like this:
[code]code here[/code]
Re: How do you simple make a button?
Posted: Thu Jul 21, 2011 8:47 am
by bartbes
Code: Select all
local inBox = mx > v.x and my > v.y and mx < v.x+size and my < v.y+size
This should work, for your standard non-rotated squares (if you have a y and a x size you can make it a rectangle, of course).