text on top of background

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
pielago
Party member
Posts: 142
Joined: Fri Jun 14, 2013 10:41 am

text on top of background

Post by pielago »

i dont know what am i doing wrong here is it possible for someone to help me finish this
i want the text to end exactly where the background ends and i tried the normal ways and yet didn't work
keeps on giving me errors all the time????

Code: Select all


function love.load()
	--from where to type
	input ="input: "
   text = "type here only"
end

function love.textinput(t)
    text = text .. t
end

function love.draw()
	--box
	love.graphics.setColor(200,0,0,255)
	love.graphics.rectangle("fill",0,0,500,20)
	
	--text
	love.graphics.setColor(255,255,255,255)
	love.graphics.print(input,0,0)
   love.graphics.printf(text, 40, 0, love.graphics.getWidth())
end

function love.keypressed( key, unicode )
	
    if unicode > 31 and unicode < 127 then  -- here is my problems it wont stop where box end that's where needs to end input text--
        text = text .. string.char(key)
    end
   
   if key == "backspace" then
      text = text:sub(1,-2) 
   end   

end


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

Re: text on top of background

Post by Robin »

First, remove this bit:

Code: Select all

   
    if unicode > 31 and unicode < 127 then  -- here is my problems it wont stop where box end that's where needs to end input text--
        text = text .. string.char(key)
    end
   
Second, you have a box of 500 characters long, and you want to do what?
  1. You want to make the text that doesn't fit in the box invisible, so it doesn't look ugly

    Code: Select all

    function love.draw()
       --box
       love.graphics.setColor(200,0,0,255)
       love.graphics.rectangle("fill",0,0,500,20)
       
       --text
       love.graphics.setColor(255,255,255,255)
       love.graphics.setScissor( 0, 0, 500, 20 )
       love.graphics.print(input,0,0)
       love.graphics.printf(text, 40, 0, love.graphics.getWidth())
       love.graphics.setScissor()
    end
  2. You actually want the typing to stop. Well that's a bit harder. You see, you left 500 - 40 = 460 pixels of space, but how many characters is that? That depends on the font and which characters.

    Code: Select all

    function love.textinput(t)
         if love.graphics.getFont():getWidth(text .. t) <= 460 then
             text = text .. t
         end
    end
Help us help you: attach a .love.
pielago
Party member
Posts: 142
Joined: Fri Jun 14, 2013 10:41 am

Re: text on top of background

Post by pielago »

okay it works thank you for the fast reply.
now I will study how its done and what i did wrong!

box size was just a sample size surely i will make it smaller ..
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 2 guests