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.
User avatar
nevon
Commander of the Circuloids
Posts: 938
Joined: Thu Feb 14, 2008 8:25 pm
Location: Stockholm, Sweden
Contact:

Re: I have a question.

Post by nevon »

Doesn't Font:getWidth() require a string as an argument?
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: I have a question.

Post by kikito »

You are completely right - I've corrected my post.
When I write def I mean function.
User avatar
com_1
Prole
Posts: 44
Joined: Fri Oct 22, 2010 11:54 pm
Location: No Comment

Re: I have a question.

Post by com_1 »

Code: Select all

function love.load()
  scrx = 640; scry = 480; zx = (scrx/2); zy = (scry/2); zi=200; 
  love.graphics.setMode(scrx, scry, false, true, 0);
  font = love.graphics.newFont("courbi.ttf", 15); --changed 'setFont' to 'newFont'
  height = font:getHeight();
end

function love.draw()
  love.graphics.line(zx - zi, zy, zx + zi, zy); love.graphics.line(zx, zy - zi, zx, zy + zi);
  love.graphics.setFont(font); -- invoked setFont here
  i = "Thanks people";
  width = font:getWidth(i); -- moved getWidth here and added the string
  love.graphics.print(i, zx - (width/2), zy + (height/5));
end
User avatar
nevon
Commander of the Circuloids
Posts: 938
Joined: Thu Feb 14, 2008 8:25 pm
Location: Stockholm, Sweden
Contact:

Re: I have a question.

Post by nevon »

com_1, do take a look at love.graphics.printf(). It's very handy when you're just trying to center text. Saves you the trouble of having to do a bunch of getWidth()/getHeight().
User avatar
zac352
Party member
Posts: 496
Joined: Sat Aug 28, 2010 8:13 pm
Location: In your head.
Contact:

Re: I have a question.

Post by zac352 »

nevon wrote:com_1, do take a look at love.graphics.printf(). It's very handy when you're just trying to center text. Saves you the trouble of having to do a bunch of getWidth()/getHeight().

Code: Select all

local myStr="Hi there"
love.graphics.print(myStr,myX-myFont:getWidth(myStr)/2,myY)
Hello, I am not dead.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: I have a question.

Post by kikito »

nevon wrote:com_1, do take a look at love.graphics.printf(). It's very handy when you're just trying to center text. Saves you the trouble of having to do a bunch of getWidth()/getHeight().
I'm afraid that love.graphics.draw (for images) has the 'cx,cy' params, but print/printf don't - unless the wiki is incomplete.
When I write def I mean function.
User avatar
nevon
Commander of the Circuloids
Posts: 938
Joined: Thu Feb 14, 2008 8:25 pm
Location: Stockholm, Sweden
Contact:

Re: I have a question.

Post by nevon »

kikito wrote:
nevon wrote:com_1, do take a look at love.graphics.printf(). It's very handy when you're just trying to center text. Saves you the trouble of having to do a bunch of getWidth()/getHeight().
I'm afraid that love.graphics.draw (for images) has the 'cx,cy' params, but print/printf don't - unless the wiki is incomplete.
You don't need them. love.graphics.printf( text, x, y, limit, align )

So to center text between x=100 and x=300, at y=400, do:

Code: Select all

love.graphics.printf("Our text", 100, 400, 200, "center")
User avatar
com_1
Prole
Posts: 44
Joined: Fri Oct 22, 2010 11:54 pm
Location: No Comment

Re: I have a question.

Post by com_1 »

Code: Select all

//With
love.graphics.printf("Thanks people", 320, 240, 0, "center")
//or
love.graphics.printf("Thanks"..(" ")..("people"), 320, 240, 0, "center")
//Not possible
This image with PRINTF.

Image
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: I have a question.

Post by Robin »

com_1 wrote:

Code: Select all

//With
love.graphics.printf("Thanks people", 320, 240, 0, "center")
//or
love.graphics.printf("Thanks"..(" ")..("people"), 320, 240, 0, "center")
//Not possible
Why the second one, may I ask?
Help us help you: attach a .love.
User avatar
zac352
Party member
Posts: 496
Joined: Sat Aug 28, 2010 8:13 pm
Location: In your head.
Contact:

Re: I have a question.

Post by zac352 »

com_1 wrote:

Code: Select all

love.graphics.printf("Thanks"..(" ")..("people"), 320, 240, 0, "center")
I don't understand why that wouldn't work.
"Thanks" "people" of course wouldn't, unless you set the metatable of strings to automatically concatenate when __call is used.
Hello, I am not dead.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 233 guests