Pressing buttons?
Moderators: Shine, Insert_witty_name
Pressing buttons?
Hello everyone! :-D
I'm amazed by this Lua language... It does in 25 lines what C++ would do in like 60. O_O
Now, I might not be that new to coding but I am to games... :-P
My game does a change whenever the user presses the triangle button.
However, when that happens, that change occurs many times due to the fact that the button-press checking occurs every time the game loop goes by... Is there any way to determine how long it should be pressed for that change to take place?
I'm sorry about the bad wording and stuff, I'm really new. Help. Thanks! ;-)
I'm amazed by this Lua language... It does in 25 lines what C++ would do in like 60. O_O
Now, I might not be that new to coding but I am to games... :-P
My game does a change whenever the user presses the triangle button.
However, when that happens, that change occurs many times due to the fact that the button-press checking occurs every time the game loop goes by... Is there any way to determine how long it should be pressed for that change to take place?
I'm sorry about the bad wording and stuff, I'm really new. Help. Thanks! ;-)
Im pretty new so i dunno if you would use my advice. New to programing.
Anywho for lua i use a buttonpress timer so that it doesnt repeat all actions over and over again on press.
initiate timer
checktimer if time >= .05 seconds then
put keypress, code all the pad stuff here
make sure you reset timer afer every keypress
end
Dunno if you can use that. And if there is a better way i would like to know myself.
Anywho for lua i use a buttonpress timer so that it doesnt repeat all actions over and over again on press.
initiate timer
checktimer if time >= .05 seconds then
put keypress, code all the pad stuff here
make sure you reset timer afer every keypress
end
Dunno if you can use that. And if there is a better way i would like to know myself.
Snoochie Boochies
OK, this kind of works:
It kind of works. 'Kind of' because it doesn't do it WHEN the triangle is pressed, but .25 second between the start and end of the press... :-(
Any ideas? Thanks! ;-)
Code: Select all
-- At beg. of file:
counter = Timer.new()
counter:start()
trianglePressed = false
trianglePressedAt = 0
-- Later on:
if pad:left() then x = x - a end
if pad:right() then x = x + a end
if pad:triangle() then
if trianglePressed == false then
trianglePressed = true
trianglePressedAt = counter:time()
else
if counter:time() - trianglePressedAt >= 250 then
-- Handle the press here --
trianglePressed = false
trianglePressedAt = 0
end
end
end
Any ideas? Thanks! ;-)
Have a boolean variable to act as a flag to keep track of current state of the button. Each time you read the pad in the game loop:
If button is not down, clear flag.
If button is down, and the flag is not set, then set the flag and perform your action as this is the first time the button was just pressed.
If button is down, and flag is set, then this is a repeated press/held key which you may choose to ignore, or repeat after a time delay or whatever you want, depending on the use
If button is not down, clear flag.
If button is down, and the flag is not set, then set the flag and perform your action as this is the first time the button was just pressed.
If button is down, and flag is set, then this is a repeated press/held key which you may choose to ignore, or repeat after a time delay or whatever you want, depending on the use
Damn, I need a decent signature!
http://wiki.ps2dev.org/psp:lua_player:f ... s#controls
while true do
if not key = Controls.read() then
key = Controls.read()
if key:triangle() then
-- do stuff here
end
end
end
while true do
if not key = Controls.read() then
key = Controls.read()
if key:triangle() then
-- do stuff here
end
end
end
Can't believe nobody posted this solution. Everybody use's it as far as I knew:
What this does is, it stores the old button state and checks if it differs from what it is know. Only then it will execute the code again. Most easy way ever. No seperate button checks or booleans or whatever.
Code: Select all
pad = Controls.read()
if pad~=oldpad then
-- put the button stuff here.
end
oldpad = pad
-
- Posts: 123
- Joined: Mon Oct 03, 2005 4:25 am
- Location: Netherlands
shouldn't it beromero126 wrote:
while true do
if not key = Controls.read() then
key = Controls.read()
if key:triangle() then
-- do stuff here
end
end
end
if not key == Controls.read() then
Because it seams you are checking wether they are equal (== instead of =)
Behold! The Underminer got hold of a PSP