I have a question!

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
XxFullMetalxX
Prole
Posts: 9
Joined: Thu Jul 04, 2013 5:52 pm

I have a question!

Post by XxFullMetalxX »

i am new to love2d, and I was just wondering if there is a way to find the center of a screen.

If there is any way to find out please tell me!
User avatar
Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

Re: I have a question!

Post by Plu »

You can use:
http://www.love2d.org/wiki/love.graphics.getHeight
http://www.love2d.org/wiki/love.graphics.getWidth

to get the total height/width of the screen. If you divide them by 2, you get the middle of the screen.
User avatar
Eamonn
Party member
Posts: 550
Joined: Sat May 04, 2013 1:29 pm
Location: Ireland

Re: I have a question!

Post by Eamonn »

Just to add to what Plu said: If you ever want to find the center of the screen with an image, you would do the following:

Code: Select all

img = love.graphics.newImage("img.png")
centerX = ( ( love.graphics.getWidth()/2 )-( img:getWidth()/2 ) )
centerY = ( ( love.graphics.getHeight()/2 )-( img:getHeight()/2 ) )
Just in case you ever want to get the center of an image, there you go! Hope this helped! :awesome: :awesome: :awesome:
"In those quiet moments, you come into my mind" - Liam Reilly
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: I have a question!

Post by davisdude »

Not to be nit-picky or anything but you should really give a less general name to your post than that. Instead of 'I have a question!' you could have called it something like 'Getting the center of the screen' or something like that.

Figured I would give you a heads up for future questions.

Also something else useful: getting the width of text and what not.
If you ever want to do this (and believe me, you most likely will), this is how you would go about doing it:

Code: Select all

font = love.graphics.newFont('timesNewRomans.ttf', 32) --name of font, size
love.graphics.setFont(font)
text = 'Brought to you by XxFullMetalxX'
height = font:getHeight(text) --note that you don't actually have to put the argument text there since the font height will be the same. It's more of an organizational thing
width = font:getWidth(text) --You do need it for this one, however
center = { width/2, height/2 }
And there you have it! This is more than you probably wanted, but you never know if it might come in handy! :awesome:
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 0 guests