love.keypressed Stops Working After First Press

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
Kookerus
Prole
Posts: 39
Joined: Sat Jun 28, 2014 4:33 am

love.keypressed Stops Working After First Press

Post by Kookerus »

I'm trying to make a program wher the user just types in a box, and if backspace is pressed, the last character is deleted. I have the code working, but the second time bacekspace is pressed, it won't delete the last character.

Code: Select all

function love.load()
	intro = "Type away! -- \n"
	text = ""
	numTimes = 0
end

function love.textinput(t)
	text = text .. t
	numTimes = numTimes + 1
end

function love.keypressed(key)
    if key == "backspace" then
        text = string.sub(text, 1, numTimes - 1)
    end
end

function love.draw()
	love.graphics.printf(text, 0, 0, love.graphics.getWidth())
end
Can anyone help me?
Kookerus
Prole
Posts: 39
Joined: Sat Jun 28, 2014 4:33 am

Re: love.keypressed Stops Working After First Press

Post by Kookerus »

Never mind. I realized I forgot to decrease the "numTimes" variable. Changed it and it worked like a charm!
User avatar
Positive07
Party member
Posts: 1014
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: love.keypressed Stops Working After First Press

Post by Positive07 »

Okey just so that you can learn some things:
you could use something like

Code: Select all

text = text:sub(1, numTimes - 1)
notice that text:sub is the same as string.sub(text...)
also you can do

Code: Select all

text = text:sub(1, text:len()- 1)
text length should return the number of characters in text
or instead use something like

Code: Select all

text = text:sub(1,-2)
Since negative numbers count from the last character + 1 (because it is inclusive)
Have fun with lua!! :awesome:
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
Kookerus
Prole
Posts: 39
Joined: Sat Jun 28, 2014 4:33 am

Re: love.keypressed Stops Working After First Press

Post by Kookerus »

Positive07 wrote:Okey just so that you can learn some things:
you could use something like

Code: Select all

text = text:sub(1, numTimes - 1)
notice that text:sub is the same as string.sub(text...)
also you can do

Code: Select all

text = text:sub(1, text:len()- 1)
text length should return the number of characters in text
or instead use something like

Code: Select all

text = text:sub(1,-2)
Since negative numbers count from the last character + 1 (because it is inclusive)
Have fun with lua!! :awesome:
Thank you so much! I've never used lua before, and it's quite a bit different than python
User avatar
Positive07
Party member
Posts: 1014
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: love.keypressed Stops Working After First Press

Post by Positive07 »

Well... I have never used python (I used it once but just a little) so I will call you when I need to :awesome:
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
Kookerus
Prole
Posts: 39
Joined: Sat Jun 28, 2014 4:33 am

Re: love.keypressed Stops Working After First Press

Post by Kookerus »

Sounds like a plan! :nyu:
Post Reply

Who is online

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