Computer Science Canada

help with double tapping

Author:  Chaos_Theory [ Sat Jan 29, 2005 12:51 pm ]
Post subject:  help with double tapping

hey, im making a game, and I want the player to be able to double tap the right/left arrow keys to load my run procedure, or just tap it once to load the walk procedure

can somebody help me make a timer, or maybe you could help me use another method to solve my problem, your help is appreciated Razz

Author:  Cervantes [ Sat Jan 29, 2005 1:08 pm ]
Post subject: 

What have you got so far? If you don't have code yet (which is fine), what are your ideas?

Author:  Chaos_Theory [ Sat Jan 29, 2005 1:22 pm ]
Post subject: 

Cervantes wrote:
What have you got so far? If you don't have code yet (which is fine), what are your ideas?


yah I have some coding:

code:

var Actions : array char of boolean
var FaceRight : boolean := false

process Controls
Input.KeyDown (Actions)

Stand

If Actions (KEY_RIGHT_ARROW) then
     FaceRight := true
     Walk
elsif Actions (KEY_LEFT_ARROW then
     FaceRight := false
     Walk
end if

if Actions (KEY_UP_ARROW) then
     Jump
elsif Actions (KEY_DOWN_ARROW) then
     Crouch
end if

end Controls

fork Controls


I alsoy have the procedures, but i don't think you should need them to help me, I just need to find a way to allow a double tap so it loads the run instead of the walk, so can you help me?

Author:  person [ Sat Jan 29, 2005 2:16 pm ]
Post subject: 

u can do this (havent tested it):

code:

mousewhere (x,y,button)
if button= 0 or button = 100 then
        mousewhere (x1,y1,button1)       
        if button1= 0 or button1 = 100 then
                % do wat ever
        end if
end if


hope this helps

Author:  Delos [ Sat Jan 29, 2005 6:29 pm ]
Post subject: 

person...what on Earth was the point of that?

Seriously, Chaos_Theory needs help on double tapping on the Keyboard...not anything to do w/ the Mouse...

[sigh]
[/mini-rant]

Ok, I'm good.

Let's see...your code isn't bad...I don't like the processes use, but that's just me. Okay, it's not just me - it's about five dozen other ppl around here too.

Anyhow...in terms of double tapping, you could try this idea:
- detect first input. Do something. Set a boolean var to true (i.e. 'FirstTap := true")
- detect second input. If FirstTap is true, then do the second something.
- reset FirstTap everynow and then.

Otherwise...a better method:
- detect first tap. Do something. Record the Time at which tap occured.
- detect second tap. If a sufficient amount of time has passed, or if the tap is within a particular range of time, do the second something.
- it would be even better if instead of Time.() stuff, you used an FPS check...this would ensure some cross-computer compatability...

But just try to get if working first, then you can add in the other stuff. :p.

BTW, no offense person...hehe.

Author:  person [ Sat Jan 29, 2005 6:31 pm ]
Post subject: 

sorry about that

Author:  AsianSensation [ Sat Jan 29, 2005 6:55 pm ]
Post subject: 

you know, I think your incessive spamming should stop, consider this as a warning.

- all bits

Anymore of this type of behavior will result in banning. Go ahead and post nonrelevent stuff in the Spam section, but posting irrelevent code and one-liners only distracts other people seeking help.

Author:  Chaos_Theory [ Sat Jan 29, 2005 7:58 pm ]
Post subject: 

heh, I just thought of something myself, It doesnt quite work, but if soemoen can help me to make it work, i would be very greateful

code:

var Actions : array char of boolean
var DoubleTap : boolean := false

loop
    DoubleTap := false
    Input.KeyDown (Actions)

    if Actions (KEY_RIGHT_ARROW) then
        delay (100)
        for i : 1 .. 10
            Input.KeyDown (Actions)
            delay (100)

            if Actions (KEY_RIGHT_ARROW) then
                DoubleTap := true
            end if
        end for
        if DoubleTap = true then
            put "Run!"
        else
            put "walk..."
        end if
    end if
end loop


now... since this is to walk or run, the player needs to hold the button when they press it, so if you press once and hold it is supposd to walk, but if you press then press again and hold real fast it should run, but, in mine teh way I have it if you hold it he runs no matter what... so could someone help? or is what im asking for impossible on turing?

Author:  basketball4ever [ Sat Jan 29, 2005 8:12 pm ]
Post subject: 

Quote:
now... since this is to walk or run, the player needs to hold the button when they press it, so if you press once and hold it is supposd to walk, but if you press then press again and hold real fast it should run, but, in mine teh way I have it if you hold it he runs no matter what... so could someone help? or is what im asking for impossible on turing?


i honestly have no clue how to do it... but heres what i know...
in turing... it stores up the things u press on the keyboard... basically... if i hold down the right arrow... and the program isn't fast enough... it'll keep pressing the right arrow. I dont know... but that might make ur program a lil buggy. I dont recommend turing for what you're trying but yeh. I might be just wrong Razz.

Couldn't you just do it, that if they're press shift... and then press the right key, it runs, and if they press shift again... they stop running?

Author:  person [ Sat Jan 29, 2005 8:25 pm ]
Post subject: 

im not sure this will work, but i think getch will work

Author:  Trojan Man [ Sat Jan 29, 2005 11:14 pm ]
Post subject: 

I see that you tried using delay in your code, but that won't work well because holding down the key will do the same, unless you start the delay after the button was released, but if you make a delay it also slows your program down, making your program halt between two key clicks.

Quote:

can somebody help me make a timer, or maybe you could help me use another method to solve my problem, your help is appreciated

Do you need the timer just to time your program or to determine the time of double clicking?
If you want to just time your program by putting something like this at the end of your program:
code:

var timeRunning : int
timeRunning := Time.Elapsed
put "This program has run ", timeRunning, " milliseconds"

OR You can use the same command to time the amount between each of the double clicks, you'll just need to have it in a loop. Except I'm not quiet sure what you'd have to have in your if statement. I'm guessing you'd have to like start the timer when the button was released and end the time (exit the loop) if it ran for more than like 2000 miliseconds or if the button was clicked. And then make an if structure that checks if the same button was pressed in the period between the button release and the 2 seconds.
Quote:

Couldn't you just do it, that if they're press shift... and then press the right key, it runs, and if they press shift again... they stop running?

In my opinion this would make more sense, you wouldn't have to use the timer and stuff, besides that's what all the modern shooter games use.
Quote:

im not sure this will work, but i think getch will work

Getch will not work when inputting multiple keys, that's why we have to use Input.KeyDown.

EDIT: If you're gonna use my method, note that when you put it in the loop it, it should look like this:
code:

var timeRunning : int
loop
    timeRunning := Time.Elapsed
    put "This program has run ", timeRunning, " milliseconds"
    exit when timeRunning >= 2000
end loop

First: your loop has to go around the Time.Elapsed so that it increases everytime, and it has to exit when it's either more or equal to 2000, NOT just equal to 2000 because the loop doesn't repeat every milisecond, so its can't always be equal to 2000. My comp's slow (XP 2800+) it takes him about 13-14 mileseconds to restart the loop.

Author:  Cervantes [ Sun Jan 30, 2005 8:27 am ]
Post subject: 

basketball4ever wrote:
in turing... it stores up the things u press on the keyboard... basically... if i hold down the right arrow... and the program isn't fast enough... it'll keep pressing the right arrow. I dont know... but that might make ur program a lil buggy. I dont recommend turing for what you're trying but yeh. I might be just wrong Razz.

Good point. But with some boolean variables, you should be able to make it work just fine.

Chaos_Theory: There's a major problem with the last bit of code you posted: you've got a for loop inside your main loop (well, it's inside the if Actions statement, but that's inside your main loop). That for loop contains some pretty hefty delays. Thus, as soon as you hit KEY_RIGHT_ARROW, you delay for 100 milliseconds, enter the for loop, delay for another 100 milliseconds, check for KEY_RIGHT_ARROW, delay another 100 milliseconds... See, the rest of your program is not running while you're inside that for loop. And copying and pasting all the code of your program inside that for loop is not a good idea! Same with using a procedure that contains all the code of your program.
Delos outlined what you should be doing quite nicely. The only thing is, you will have to solve the problem that basketball4ever outlined. Boolean variables will be your friend.
-Cervantes

Author:  Chaos_Theory [ Sun Jan 30, 2005 2:46 pm ]
Post subject: 

OMFG XD I've almost got it^^ actually i think i did just get it... (just a little hard to tell cause i'm only testing it with text (so that you people can coppy and paste it to check if it works) Razz lol

code:

var Actions : array char of boolean
var Walk : array char of boolean
var Run : array char of boolean
var SingleTap : boolean := false
var DoubleTap : boolean := false

loop
    SingleTap := false
    DoubleTap := false
    Input.KeyDown (Actions)

    if Actions (KEY_RIGHT_ARROW) then
        for i : 1 .. 10
            Input.KeyDown (Walk)

            if Walk (KEY_RIGHT_ARROW) then
                SingleTap := true
            else
                SingleTap := false
            end if
            delay (10)
        end for

        for i : 1 .. 10
            Input.KeyDown (Run)
           
            if Run (KEY_RIGHT_ARROW) and SingleTap = false then
            DoubleTap := true
        else
            DoubleTap := false
        end if
        delay (10)
    end for

    if SingleTap = true then
        put "walk..."
    elsif DoubleTap = true then
        put "Run!"
    end if
end if
end loop


well, if that works.. then... I've finnaly figured it out and thanks every one for your help... and if not, then I'd appreciate if you fixed it up for me 8)

oh, and to cervantes, the delays are no problem, cause this will be in a process so it runs individually form teh rest of the program^^

Author:  Cervantes [ Sun Jan 30, 2005 3:28 pm ]
Post subject: 

Chaos_Theory wrote:
oh, and to cervantes, the delays are no problem, cause this will be in a process so it runs individually form teh rest of the program^^

Uh oh.
Processes = death.
We'll allow Hacker Dan to explain:
Hacker Dan wrote:

I whould not recomend process for this, probly best to make one main loop with deals with eveything. Process can be good but you have to understand how they work and turing has limited conortl over them. Since turing has no way to set proity of a process all you know is that the code may run at some time. So baliky unless you got a dual CPU system a process will run lines of code in it at basicly random times durning the excution of your progame. If it dose not mater when they run then it could be good to use them but it gives you less conortl over things.


Even if you are using this in a process (and we'll ignore the sin for the moment), things will be slowed down. The way you've got it, you can't determine whether he's running or walking until after you've gone through those for loops. And that means that the rest of your program is not thinking your character is running or walking (whicher he is) until a while after you hit the key.

Turing:

var Actions : array char of boolean
var tap := false
var speed := ""
var timer := 0
const doubleHitTime := 300

loop
    Input.KeyDown (Actions)

    if Actions (KEY_RIGHT_ARROW) and tap = false then
        tap := true
        if Time.Elapsed - timer < doubleHitTime then
            speed := "run"
        else
            speed := "walk"
        end if
        timer := Time.Elapsed
    end if
    if not Actions (KEY_RIGHT_ARROW) then
        tap := false
    end if
    locate (1, 1)
    put speed

end loop

Author:  Chaos_Theory [ Sun Jan 30, 2005 5:19 pm ]
Post subject: 

I use to processes in my program... one called getkey

which is bacially just a gech, but it allows the program to contune while waiting for the input... (which is nessesary... nothing wrong with that one... )

and then Controls ,the reason why i chose to use it... is becasue processes run seperatly from the rest of the program, and all it is ; is a process that checks what key is being pressed and does teh appropriate action, where it loads a procedure, but because this procedure is inside a process, it is also run seperate from the main propraram... and thus you can move your character (which requires delaying so the pictures don't cycle through madly fast) and also doesn't delay teh rest fi teh program... if you can tell me another way of doing taht without using a proces.s.. then I'll agree with you but until then, I will continue to use processes.

Author:  Chaos_Theory [ Sun Jan 30, 2005 5:36 pm ]
Post subject: 

oh, and thnx for the coding Cervantes, it's great i fixed it up so it will load a procedure... but, i still think Im gonna need to put it inside of a process

Author:  ssr [ Sun Jan 30, 2005 8:10 pm ]
Post subject: 

I have a question cant u make this with "hasch"
like c how many hasch are there ? Confused

Author:  Bacchus [ Sun Jan 30, 2005 8:44 pm ]
Post subject: 

hasch doesnt register the key pressed or even return a number, it returns a boolean value for when something has been pressed on the keyboard, and for all i know once hasch goes to true it doesnt go back while the program is running. Input.Key() down is the best way to go because it returns which key was pressed and doesnt store keys pressed like getch()

Author:  ssr [ Mon Jan 31, 2005 9:37 pm ]
Post subject: 

what I meant was that
hasch and then getch
if 2 hasch and blah blah

Author:  Cervantes [ Tue Feb 01, 2005 9:07 am ]
Post subject: 

No, stick with Input.KeyDown.


: