| View previous topic :: View next topic |
| Author |
Message |
Leo28C
Joined: 18 Jan 2007 Posts: 19
|
Posted: Sun Feb 04, 2007 3:58 pm Post subject: 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! ;-) |
|
| Back to top |
|
 |
PsyOps
Joined: 23 Jan 2007 Posts: 5
|
Posted: Sun Feb 04, 2007 8:21 pm Post subject: |
|
|
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. _________________ Snoochie Boochies |
|
| Back to top |
|
 |
Leo28C
Joined: 18 Jan 2007 Posts: 19
|
Posted: Mon Feb 05, 2007 4:04 am Post subject: |
|
|
OK, this kind of works:
| Code: |
-- 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
|
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! ;-) |
|
| Back to top |
|
 |
romero126
Joined: 24 Dec 2005 Posts: 200
|
Posted: Mon Feb 05, 2007 6:30 am Post subject: |
|
|
| Reset the timer each time you press a button.. |
|
| Back to top |
|
 |
Leo28C
Joined: 18 Jan 2007 Posts: 19
|
Posted: Mon Feb 05, 2007 8:56 am Post subject: |
|
|
Mhmm... I don't get it... :-/
I mean, how can I check for a button press? Like;
* Push button
* Perform action
* Release button
* Push button
* Perform action
* Release button
Right now, it doesn't wait for the release to perform the action again... What's the way to check for that? Thanks! ;-) |
|
| Back to top |
|
 |
cheriff Regular
Joined: 23 Jun 2004 Posts: 262 Location: Sydney.au
|
Posted: Mon Feb 05, 2007 9:14 am Post subject: |
|
|
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 _________________ Damn, I need a decent signature! |
|
| Back to top |
|
 |
romero126
Joined: 24 Dec 2005 Posts: 200
|
|
| Back to top |
|
 |
Leo28C
Joined: 18 Jan 2007 Posts: 19
|
Posted: Mon Feb 05, 2007 11:24 am Post subject: |
|
|
| Cheriff's solution worked, many thanks to all of you! ;-) |
|
| Back to top |
|
 |
Altair
Joined: 20 May 2006 Posts: 76 Location: The Netherlands
|
Posted: Tue Feb 06, 2007 7:18 am Post subject: |
|
|
Can't believe nobody posted this solution. Everybody use's it as far as I knew:
| Code: | pad = Controls.read()
if pad~=oldpad then
-- put the button stuff here.
end
oldpad = pad |
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. |
|
| Back to top |
|
 |
romero126
Joined: 24 Dec 2005 Posts: 200
|
Posted: Tue Feb 06, 2007 7:49 am Post subject: |
|
|
| I posted that on the previous post. It just looks a lil diff. |
|
| Back to top |
|
 |
Altair
Joined: 20 May 2006 Posts: 76 Location: The Netherlands
|
Posted: Tue Feb 06, 2007 9:04 am Post subject: |
|
|
| Ah yeah I see now. However yours reads the controls twice, which isn't necessairy. However for the rest it's the same, you're right. |
|
| Back to top |
|
 |
the underminer
Joined: 03 Oct 2005 Posts: 124 Location: Netherlands
|
Posted: Tue Feb 06, 2007 6:44 pm Post subject: |
|
|
| romero126 wrote: |
while true do
if not key = Controls.read() then
key = Controls.read()
if key:triangle() then
-- do stuff here
end
end
end |
shouldn't it be
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 |
|
| Back to top |
|
 |
Altair
Joined: 20 May 2006 Posts: 76 Location: The Netherlands
|
Posted: Wed Feb 07, 2007 1:09 am Post subject: |
|
|
| yeps it should, but that wasn't really what it was about |
|
| Back to top |
|
 |
romero126
Joined: 24 Dec 2005 Posts: 200
|
Posted: Wed Feb 07, 2007 7:21 am Post subject: |
|
|
| I was drunk as a skunk when i wrote it leave me alone. |
|
| Back to top |
|
 |
Leo28C
Joined: 18 Jan 2007 Posts: 19
|
Posted: Wed Feb 07, 2007 12:35 pm Post subject: |
|
|
| romero126 wrote: | | I was drunk as a skunk when i wrote it leave me alone. |
LMAO! xD |
|
| Back to top |
|
 |
|