button errors
Moderators: Shine, Insert_witty_name
button errors
I have been trying to add a password function to my game. when I click the correct key though each click is 4-6 instead of one. this makes it impossible for me to have the correct password input. please help!
Controls library
I made a handy library that handles this, feel free to use if you want to.
It is made specifically to handle your problem. For arrow buttons, it also features repeat treshold and repeat speed rate. You have to use Buttons.get() to read the PSP buttons (use just once in your loop). The library is meant to be used in a loop that waits for screen refresh using screen.waitVblankStart() (with no arg, wait just for one refresh). It also features comfortable reading of the analog stick.
It is made specifically to handle your problem. For arrow buttons, it also features repeat treshold and repeat speed rate. You have to use Buttons.get() to read the PSP buttons (use just once in your loop). The library is meant to be used in a loop that waits for screen refresh using screen.waitVblankStart() (with no arg, wait just for one refresh). It also features comfortable reading of the analog stick.
Code: Select all
--***************************************
--***************************************
--********** CONTROLS LIBRARY ***********
--***************************************
--***************************************
Buttons={}
function Buttons.reset ()
Buttons.rPressed=false
Buttons.lPressed=false
Buttons.rightPressed=false
Buttons.leftPressed=false
Buttons.downPressed=false
Buttons.upPressed=false
Buttons.crossPressed=false
Buttons.circlePressed=false
Buttons.squarePressed=false
Buttons.trianglePressed=false
Buttons.startPressed=false
Buttons.selectPressed=false
Buttons.PressedTreshold=20
Buttons.PressedSpeed=3 --the less the faster
Buttons.rPressedCounter=0
Buttons.lPressedCounter=0
Buttons.uPressedCounter=0
Buttons.dPressedCounter=0
Buttons.analogCutOff=18
Buttons.analogRange=3
Buttons.analogBase=128-Buttons.analogCutOff
end
function Buttons.get()
Buttons.pad=Controls.read()
end
function Buttons.l ()
if Buttons.pad:l() then
if Buttons.lPressed==false then
Buttons.lPressed=true
return true
end
else
Buttons.lPressed=false
end
return false
end
function Buttons.r ()
if Buttons.pad:r() then
if Buttons.rPressed==false then
Buttons.rPressed=true
return true
end
else
Buttons.rPressed=false
end
return false
end
function Buttons.start ()
if Buttons.pad:start() then
if Buttons.startPressed==false then
Buttons.startPressed=true
return true
end
else
Buttons.startPressed=false
end
return false
end
function Buttons.select ()
if Buttons.pad:select() then
if Buttons.selectPressed==false then
Buttons.selectPressed=true
return true
end
else
Buttons.selectPressed=false
end
return false
end
function Buttons.circle()
if Buttons.pad:circle() then
if Buttons.circlePressed==false then
Buttons.circlePressed=true
return true
end
else
Buttons.circlePressed=false
end
return false
end
function Buttons.square()
if Buttons.pad:square() then
if Buttons.squarePressed==false then
Buttons.squarePressed=true
return true
end
else
Buttons.squarePressed=false
end
return false
end
function Buttons.cross()
if Buttons.pad:cross() then
if Buttons.crossPressed==false then
Buttons.crossPressed=true
return true
end
else
Buttons.crossPressed=false
end
return false
end
function Buttons.triangle()
if Buttons.pad:triangle() then
if Buttons.trianglePressed==false then
Buttons.trianglePressed=true
return true
end
else
Buttons.trianglePressed=false
end
return false
end
function Buttons.left()
if Buttons.pad:left() then
if Buttons.leftPressed==false or Buttons.lPressedCounter>Buttons.PressedTreshold then
Buttons.lPressedCounter=Buttons.lPressedCounter-Buttons.PressedSpeed
Buttons.leftPressed=true
return true
end
Buttons.lPressedCounter=Buttons.lPressedCounter+1
else
Buttons.leftPressed=false
Buttons.lPressedCounter=0
end
return false
end
function Buttons.right()
if Buttons.pad:right() then
if Buttons.rightPressed==false or Buttons.rPressedCounter>Buttons.PressedTreshold then
Buttons.rPressedCounter=Buttons.rPressedCounter-Buttons.PressedSpeed
Buttons.rightPressed=true
return true
end
Buttons.rPressedCounter=Buttons.rPressedCounter+1
else
Buttons.rightPressed=false
Buttons.rPressedCounter=0
end
return false
end
function Buttons.down()
if Buttons.pad:down() then
if Buttons.downPressed==false or Buttons.dPressedCounter>Buttons.PressedTreshold then
Buttons.dPressedCounter=Buttons.dPressedCounter-Buttons.PressedSpeed
Buttons.downPressed=true
return true
end
Buttons.dPressedCounter=Buttons.dPressedCounter+1
else
Buttons.downPressed=false
Buttons.dPressedCounter=0
end
return false
end
function Buttons.up()
if Buttons.pad:up() then
if Buttons.upPressed==false or Buttons.uPressedCounter>Buttons.PressedTreshold then
Buttons.uPressedCounter=Buttons.uPressedCounter-Buttons.PressedSpeed
Buttons.upPressed=true
return true
end
Buttons.uPressedCounter=Buttons.uPressedCounter+1
else
Buttons.upPressed=false
Buttons.uPressedCounter=0
end
return false
end
function Buttons.analogX()
if Buttons.pad:analogX()>Buttons.analogCutOff then
return ((Buttons.pad:analogX()-Buttons.analogCutOff)/Buttons.analogBase)*Buttons.analogRange
end
if Buttons.pad:analogX()< (-Buttons.analogCutOff) then
return ((Buttons.pad:analogX()+Buttons.analogCutOff)/Buttons.analogBase)*Buttons.analogRange
end
return 0
end
function Buttons.analogY()
if Buttons.pad:analogY()>Buttons.analogCutOff then
return ((Buttons.pad:analogY()-Buttons.analogCutOff)/Buttons.analogBase)*Buttons.analogRange
end
if Buttons.pad:analogY()< (-Buttons.analogCutOff) then
return ((Buttons.pad:analogY()+Buttons.analogCutOff)/Buttons.analogBase)*Buttons.analogRange
end
return 0
end
--***************************************
--********** CONTROLS LIBRARY ***********
--***************** END *****************
--***************************************
Also, let me include a sample demo (paste it after the cotrols library code) to give you idea how to use the lib.
Code: Select all
System.usbDiskModeActivate()
white= Color.new (255,255,255)
red= Color.new (255,30,30)
blue= Color.new (30,30,255)
green= Color.new (30,255,30)
yellow= Color.new (255,255,30)
black = Color.new(0, 0, 0)
grey = Color.new(127,127,127)
CenterX=240
CenterY=136
x=CenterX
y=CenterY
CurrentColor=white
Buttons.reset()
while true do
screen:clear()
Buttons.get()
if Buttons.up() then
y=y-1
end
if Buttons.down() then
y=y+1
end
if Buttons.right() then
x=x+1
end
if Buttons.left() then
x=x-1
end
x=x+Buttons.analogX()
y=y+Buttons.analogY()
if Buttons.start() then
break
end
if Buttons.cross() then
CurrentColor=white
end
if Buttons.circle() then
CurrentColor=red
end
if Buttons.square() then
CurrentColor=blue
end
if Buttons.triangle() then
CurrentColor=green
end
if Buttons.l() or Buttons.r() then
x=CenterX
y=CenterY
end
screen:fillRect(x, y, 10,10, CurrentColor)
screen.waitVblankStart()
screen.flip()
end
Thanks works alot better now. Alot easier than that other one. lolBob535 wrote:wow, way to complicated, try this
controls:read(pad)
if pad ~= oldPad then
do stuff
oldPad = pad
end
This code goes inside your infinite loop, and should be around anything inputs that change what is occurring on the screen.
One more ? how do you tell it to do check to see if it is anything other then your number. ex
say i want PasswordDigit1 to equal 5
how do i tell it to recognize any number other than 5(4,10,3)
instead of going through and making
PasswordDigit1<5
PasswordDigit1>5
because i have about 7 digits in my password. so i dont want to go through and make every variatoin of the correct password. is there a way to do this
say i want PasswordDigit1 to equal 5
how do i tell it to recognize any number other than 5(4,10,3)
instead of going through and making
PasswordDigit1<5
PasswordDigit1>5
because i have about 7 digits in my password. so i dont want to go through and make every variatoin of the correct password. is there a way to do this
Code: Select all
if PasswordDigit1(what symbol goes here?)5 and PasswordDigit2(what symbol goes here?)3 then
screen:print(190, 0,"Incorrect Password", Color.new(255, 255, 255))
end
Well, it is a coprehensive library to allow very easy reading of PSP's controls. Usage is very easy. Your infinite loop should like something like this:wow, way to complicated, try this
Code: Select all
Buttons.reset() --creates and resets buttons state, needs to be included once before your inf. loop
while true do
Buttons.get() --read the buttons, needs to be included just once in your loop
if Buttons.up() then
--do stuff
end
if Buttons.cross() then
--do stuff
end
x=x+Buttons.analogX()
y=y+Buttons.analogY()
--etc.
end