button errors

Discuss using and improving Lua and the Lua Player specific to the PSP.

Moderators: Shine, Insert_witty_name

Post Reply
link
Posts: 61
Joined: Wed Oct 19, 2005 6:17 am

button errors

Post by link »

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!
User avatar
JoshDB
Posts: 87
Joined: Wed Oct 05, 2005 3:54 am

Post by JoshDB »

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.
SiGi
Posts: 11
Joined: Thu Oct 20, 2005 3:24 am
Location: Phoenix, AZ

Controls library

Post by SiGi »

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: 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&#58;analogX&#40;&#41;< &#40;-Buttons.analogCutOff&#41; then 
    return &#40;&#40;Buttons.pad&#58;analogX&#40;&#41;+Buttons.analogCutOff&#41;/Buttons.analogBase&#41;*Buttons.analogRange
end

return 0

end





function Buttons.analogY&#40;&#41;


if Buttons.pad&#58;analogY&#40;&#41;>Buttons.analogCutOff then
  return &#40;&#40;Buttons.pad&#58;analogY&#40;&#41;-Buttons.analogCutOff&#41;/Buttons.analogBase&#41;*Buttons.analogRange
end


if  Buttons.pad&#58;analogY&#40;&#41;< &#40;-Buttons.analogCutOff&#41; then 
    return &#40;&#40;Buttons.pad&#58;analogY&#40;&#41;+Buttons.analogCutOff&#41;/Buttons.analogBase&#41;*Buttons.analogRange
end

return 0

end







--***************************************
--********** CONTROLS LIBRARY ***********
--***************** END *****************
--***************************************
SiGi
Posts: 11
Joined: Thu Oct 20, 2005 3:24 am
Location: Phoenix, AZ

Post by SiGi »

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&#40;&#41;

white= Color.new &#40;255,255,255&#41;
red= Color.new &#40;255,30,30&#41;
blue= Color.new &#40;30,30,255&#41;
green= Color.new &#40;30,255,30&#41;
yellow= Color.new &#40;255,255,30&#41;

black = Color.new&#40;0, 0, 0&#41;
grey = Color.new&#40;127,127,127&#41;



CenterX=240
CenterY=136






x=CenterX
y=CenterY

CurrentColor=white

Buttons.reset&#40;&#41;


while true do

screen&#58;clear&#40;&#41;

Buttons.get&#40;&#41;



if Buttons.up&#40;&#41; then 
  y=y-1
end

if Buttons.down&#40;&#41; then 
  y=y+1
end

if Buttons.right&#40;&#41; then 
  x=x+1
end

if Buttons.left&#40;&#41; then 
  x=x-1
end



x=x+Buttons.analogX&#40;&#41;

y=y+Buttons.analogY&#40;&#41;




if Buttons.start&#40;&#41; then 
  break
end

if Buttons.cross&#40;&#41; then 
  CurrentColor=white
end

if Buttons.circle&#40;&#41; then 
  CurrentColor=red
end

if Buttons.square&#40;&#41; then 
  CurrentColor=blue
end

if Buttons.triangle&#40;&#41; then 
  CurrentColor=green
end



if Buttons.l&#40;&#41; or Buttons.r&#40;&#41; then 

x=CenterX
y=CenterY

end


screen&#58;fillRect&#40;x, y, 10,10, CurrentColor&#41;





screen.waitVblankStart&#40;&#41;

screen.flip&#40;&#41;




end

Bob535
Posts: 56
Joined: Fri Nov 04, 2005 6:52 am

Post by Bob535 »

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.
link
Posts: 61
Joined: Wed Oct 19, 2005 6:17 am

Post by link »

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
link
Posts: 61
Joined: Wed Oct 19, 2005 6:17 am

Post by link »

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: Select all

if PasswordDigit1&#40;what symbol goes here?&#41;5 and PasswordDigit2&#40;what symbol goes here?&#41;3 then
screen&#58;print&#40;190, 0,"Incorrect Password", Color.new&#40;255, 255, 255&#41;&#41;
end

chaos
Posts: 135
Joined: Sun Apr 10, 2005 5:05 pm

Post by chaos »

if x ~= 1 then x_is_not_one() end
Chaosmachine Studios: High Quality Homebrew.
SiGi
Posts: 11
Joined: Thu Oct 20, 2005 3:24 am
Location: Phoenix, AZ

Post by SiGi »

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: Select all


Buttons.reset&#40;&#41; --creates and resets buttons state, needs to be included once before your inf. loop

while true do

Buttons.get&#40;&#41; --read the buttons, needs to be included just once in your loop

if Buttons.up&#40;&#41; then
  --do stuff
end

if Buttons.cross&#40;&#41; then
  --do stuff
end


x=x+Buttons.analogX&#40;&#41;

y=y+Buttons.analogY&#40;&#41; 


--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.
Bob535
Posts: 56
Joined: Fri Nov 04, 2005 6:52 am

Post by Bob535 »

not my code, i think it was durante who wrote it, not sure though.
Post Reply