How do you simple make a button?

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.
User avatar
GijsB
Party member
Posts: 380
Joined: Wed Jul 20, 2011 10:19 pm
Location: Netherlands

How do you simple make a button?

Post by GijsB »

I DID look at other code, but its always way too overcomplicated(i think?)

whats the simplest way?
User avatar
kraftman
Party member
Posts: 277
Joined: Sat May 14, 2011 10:18 am

Re: How do you simple make a button?

Post 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.
User avatar
GijsB
Party member
Posts: 380
Joined: Wed Jul 20, 2011 10:19 pm
Location: Netherlands

Re: How do you simple make a button?

Post 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()
User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Denver, CO
Contact:

Re: How do you simple make a button?

Post 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.
User avatar
GijsB
Party member
Posts: 380
Joined: Wed Jul 20, 2011 10:19 pm
Location: Netherlands

Re: How do you simple make a button?

Post by GijsB »

oke :)
User avatar
GijsB
Party member
Posts: 380
Joined: Wed Jul 20, 2011 10:19 pm
Location: Netherlands

Re: How do you simple make a button?

Post 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
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: How do you simple make a button?

Post by Robin »

You don't have to worry about optimization, especially not now. Try finishing your game first, since that is much more important.
Help us help you: attach a .love.
User avatar
GijsB
Party member
Posts: 380
Joined: Wed Jul 20, 2011 10:19 pm
Location: Netherlands

Re: How do you simple make a button?

Post 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
User avatar
T-Bone
Inner party member
Posts: 1492
Joined: Thu Jun 09, 2011 9:03 am

Re: How do you simple make a button?

Post 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]
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: How do you simple make a button?

Post 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).
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 58 guests