Page 1 of 1

Intercepting many arrow keys (SOLVED)

Posted: Fri Jul 18, 2014 3:59 pm
by MaxV
Hello,
I was trying to muove a car on the screen, but I can't understand how to intercept 2 pressed arrow keys.
The following code works only with one arrow key at time. If I'm rotating the car, I can't start moving:
##start card code###
on opencard
muovimac
end opencard

on arrowkey puls
switch puls
case "up"
put the velocita of me into v
add 10 to v
set the velocita of me to v
break
case "down"
put the velocita of me into v
add -10 to v
set the velocita of me to v
break
case "left"
put the angle of image "car" into temp
add 5 to temp
set the angle of image "car" to temp
break
case "right"
put the angle of image "car" into temp
add -5 to temp
set the angle of image "car" to temp
break
end switch
end arrowkey

on muovimac
#prendiamo la velocita'
put the velocita of me into v
put the angle of image "car" into temp
#creaimo una strina del tipo x,y
put -v * sin( pi / 180 * temp) into movimento
put "," after movimento
put -v * cos( pi / 180 * temp) after movimento
move image "car" relative movimento
if v > 0 then
add -1 to v
end if
if v < 0 then
add 1 to v
end if
set the velocita of me to v
send muovimac to me in 0.02 sec
end muovimac
###end card code######

Re: Intercepting many arrow keys

Posted: Fri Jul 18, 2014 6:44 pm
by Klaus
Buonasera Max,

you can only "catch" ONE arrowkey at a time, not two!
Maybe you can use an additional key (r = rotate?) and "catch" "keysdown()"?


Best

Klaus

Re: Intercepting many arrow keys

Posted: Sat Jul 19, 2014 3:15 pm
by dunbarx
Max.

Is your idea to be able to move the car on the diagonal, and that is why you want to be able to press, say, the "up" and "left" at the same time to move to the upperLeft?

If so, I would suggest that you use the mouse pointer inside a small rectangle, where the position of the pointer in that space moves the car. In this way you can completely control both the direction and speed of the car. This would be a very fun task. Do you see what I mean?

Craig Newman

Re: Intercepting many arrow keys

Posted: Sun Jul 20, 2014 4:45 pm
by [-hh]
..........

Re: Intercepting many arrow keys

Posted: Sun Jul 20, 2014 4:48 pm
by richmond62
Here's a thought:

Use keyDown and trap for the 1,2,3,4,6,7,8 and 9 keys on the numeric keyPad.

This may prove problematic and you may have to use rawKeyDown.

Re: Intercepting many arrow keys

Posted: Sun Jul 20, 2014 5:01 pm
by richmond62
This should help with rawKey codes:
KD.png
KEYDOWNER.rev.zip
Here's the stack.
(18.58 KiB) Downloaded 203 times

Re: Intercepting many arrow keys

Posted: Sun Jul 20, 2014 6:30 pm
by [-hh]
..........

Re: Intercepting many arrow keys

Posted: Sun Jul 20, 2014 7:09 pm
by dunbarx
All.

I still say a square "field" where the mouse pointer is moved around is the most fun. It also allows speed control, since if you move the pointer, say, at a 30° angle from a center point, you can direct the car in exactly that direction, but also the distance from that center could set the velocity.

Craig

Re: Intercepting many arrow keys

Posted: Sun Jul 20, 2014 9:06 pm
by richmond62
Here's my take on this:

a stack with 2 methods.

Crack open the card scripts and have fun.
bug.png

Re: Intercepting many arrow keys

Posted: Mon Jul 21, 2014 8:33 am
by MaxV
The keysdown solution is the best solution, you can move a car (an image) with just this code:

on arrowkey puls
put the keysdown into temp2
repeat for each item tItem in temp2
switch tItem
case "65362"
put the angle of image "car" into temp
#creaimo una strina del tipo x,y
put -5 * sin( pi / 180 * temp) into movimento
put "," after movimento
put -5 * cos( pi / 180 * temp) after movimento
move image "car" relative movimento
break
case "65364"
#dobbiamo muoverla in indietro rispetto al verso del muso
#prendiamo l'angolo della direzione
put the angle of image "car" into temp
#creaimo una strina del tipo x,y
put 5 * sin( pi / 180 * temp) into movimento
put "," after movimento
put 5 * cos( pi / 180 * temp) after movimento
move image "car" relative movimento
break
case "65361"
put the angle of image "car" into temp
add 1 to temp
set the angle of image "car" to temp
break
case "65363"
put the angle of image "car" into temp
add -1 to temp
set the angle of image "car" to temp
break
end switch
end repeat
end arrowkey

Re: Intercepting many arrow keys

Posted: Mon Jul 21, 2014 9:39 pm
by [-hh]
..........

Re: Intercepting many arrow keys

Posted: Tue Jul 22, 2014 6:38 am
by richmond62
"Richmond, I would like to add a "else pass rawkeydown" to your code. Without this and with a fast repeat rate the spooling of the keyboard buffer is not interruptable."

As I spent no more than 20 minutes knocking that stack together just to illustrate 2 ways of moving an object that is probably the least of
its problems :)

Re: Intercepting many arrow keys

Posted: Tue Jul 22, 2014 7:28 am
by [-hh]
..........

Re: Intercepting many arrow keys (SOLVED)

Posted: Wed Jul 23, 2014 11:01 am
by MaxV
An old trick about speed up animations is the following: cheat the user eyes!
If your machine is slow, increase angle rotation steps and translation steps 8)
For example, rotation code from:

Code: Select all

put the angle of image "car" into temp
add 1 to temp
set the angle of image "car" to temp
became:

Code: Select all

put the angle of image "car" into temp
add 5 to temp
set the angle of image "car" to temp
and so on... You can obtain incredible effects deceiving user sensations.