| View previous topic :: View next topic |
| Author |
Message |
link
Joined: 19 Oct 2005 Posts: 61
|
Posted: Wed Nov 30, 2005 11:04 pm Post subject: 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! |
|
| Back to top |
|
 |
JoshDB

Joined: 05 Oct 2005 Posts: 87
|
Posted: Thu Dec 01, 2005 12:57 am Post subject: |
|
|
if button:up() then
--[your function here]
screen.waitVblankStart(10)
end
the [screen.waitVblankStart(10)] pauses after the button press. Your problem was that the PSP accepts button presses without pause between them much faster then you can press. |
|
| Back to top |
|
 |
SiGi
Joined: 20 Oct 2005 Posts: 11 Location: Phoenix, AZ
|
Posted: Thu Dec 01, 2005 3:55 am Post subject: 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.
| Code: |
--***************************************
--***************************************
--********** 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 *****************
--***************************************
|
|
|
| Back to top |
|
 |
SiGi
Joined: 20 Oct 2005 Posts: 11 Location: Phoenix, AZ
|
Posted: Thu Dec 01, 2005 4:06 am Post subject: |
|
|
Also, let me include a sample demo (paste it after the cotrols library code) to give you idea how to use the lib.
| Code: |
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
|
|
|
| Back to top |
|
 |
Bob535
Joined: 04 Nov 2005 Posts: 56
|
Posted: Thu Dec 01, 2005 5:32 am Post subject: |
|
|
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. |
|
| Back to top |
|
 |
link
Joined: 19 Oct 2005 Posts: 61
|
Posted: Thu Dec 01, 2005 7:37 am Post subject: |
|
|
| Bob535 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. |
Thanks works alot better now. Alot easier than that other one. lol |
|
| Back to top |
|
 |
link
Joined: 19 Oct 2005 Posts: 61
|
Posted: Thu Dec 01, 2005 12:27 pm Post subject: |
|
|
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
| Code: | 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
|
|
|
| Back to top |
|
 |
chaos
Joined: 10 Apr 2005 Posts: 135
|
Posted: Thu Dec 01, 2005 1:55 pm Post subject: |
|
|
if x ~= 1 then x_is_not_one() end _________________ Chaosmachine Studios: High Quality Homebrew. |
|
| Back to top |
|
 |
SiGi
Joined: 20 Oct 2005 Posts: 11 Location: Phoenix, AZ
|
Posted: Fri Dec 02, 2005 3:12 am Post subject: |
|
|
| Quote: | | wow, way to complicated, try this |
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:
| Code: |
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
|
Just paste my library in the front of your .lua file or save it as a new file and use dofile(). Bob535's code works too, but it would not work, if you would want to add repeat rate of buttons in the future. |
|
| Back to top |
|
 |
Bob535
Joined: 04 Nov 2005 Posts: 56
|
Posted: Fri Dec 02, 2005 4:18 am Post subject: |
|
|
| not my code, i think it was durante who wrote it, not sure though. |
|
| Back to top |
|
 |
|