-
how to use other keys for "key.isDown" function
hi,
im trying ot make some games, and use keys to do stuff, as most games do, and i can only seem to use the arrows or function keys (tab, pgdn, delete, space, etc) and i'm wondering, how can i set it up to use keys like w, s, a and d?
thanks
-
Senior Member
Try the following frame script in frame 1:
code:
keyer = new Object();
keyer.onKeyDown = function()
{
trace('this key was pressed: ' + Key.getAscii() + " (code = " + Key.getCode() + ")" );
if (Key.getAscii() == ord('a')) {
trace('A was pressed');
}
else if (Key.getCode() == Key.RIGHT) {
trace('Right was pressed');
}
}
Key.addListener(keyer);
In the keyDown event you can look at either Key.getAscii() - which will give you printable characters, or Key.getCode() which will get you arrows and other special keys.
Here's another version of the onKeyDown handler which uses two switch statements and is a little more elegant:
code:
keyer.onKeyDown = function()
{
trace('this key was pressed: ' + Key.getAscii() + " (code = " + Key.getCode() + ")" );
// check for special keys first
switch (Key.getCode()) {
case Key.RIGHT:
trace('right was pressed');
break;
case Key.LEFT:
trace('left was pressed');
break;
default:
// it wasn't a special key that we're tracking
switch (chr(Key.getAscii())) {
case 'A':
case 'a':
trace('A was pressed');
break;
case 'B':
case 'b':
trace('B was pressed');
break;
case 'C':
case 'c':
trace('C was pressed');
break;
}
}
}
You can also make an event handler for onKeyUp as well.
Last edited by jbum; 10-18-2004 at 10:45 PM.
-
looks good, i'll have a crack at it later, thanks for ur help!
-
i tried that. didn't work for what i needed. i used this bit:
if (Key.getAscii() == ord('w')) {
speed += 1;
}
but as soon as you press W, the car acts as if you dropped a brick on the accelerator and just keeps going. same with turning, u press A for left and it just spins around. i need something like:
if (Key.isDown(getAscii() == ord('w'))){
speed += 1;
}
so that it doesn't keep going, as it checks if the key is down. i tried putting an else thing after that if, to make it slow down if W is not pressed, but no luck.
thanks guys
-
Senior Member
Sounds like you're working on the same assignment as this guy:
http://www.flashkit.com/board/showth...hreadid=590525
His movie, with corrections applied, has a pretty good framework for moving a car.
The trick is applying a damping factor when the key is NOT being held down:
speed *= 0.9;
-
would i do the dampening thing using else{ ? cos i think i tried that, it didn't seem to do nething. cos that means if its not going fowards, slow down. im not sure exactly what im getting up, but whatever i did didn't work.
flash needs to just be able to do
if {Key.isDown(key.W))
etc etc. just use normal letters and not have to do the ascii crap.
damn flash, its gr8, but could be so much better!
-
Ascii Multiple Keys
Is there any way to get flash to detect more than one ascii key at once?
-
-
you can use:
Code:
if(Key.isDown(code:number)){
}
so for W u would use:
Code:
if(Key.isDown(87)){
}
-
14yr old Member
i know
put this code into a movie clip and press any key to fing the code
onClipEvent(keyDown) {
trace("Key Pressed" + Key.getCode());
}
this code will help
65 = a key
if(Key.isDown(65 && 83){
//do something
}
it sais that if the a and s key are down it does something
if you want an or statement replace the && with || and if the a or s key is down do something
-
I think you could use this:
Code:
onClipEvent(enterFrame) {
if (Key.isDown(65)) {//==Key.LEFT
this._x-=3;//or something else...
}
if (Key.isDown(67)) {//==Key.RIGHT
this._x+=3;//or something else...
}
if (Key.isDown(87)) {//==Key.UP
this._y-=3;//or something else...
}
if (Key.isDown(83)) {//==Key.DOWN
this._y+=3;//or something else...
}
}
All keycodes:
A=65
B=66
C=67
D=68
E=69
F=70
G=71
H=72
I=73
J=74
K=75
L=76
M=77
N=78
O=79
P=80
Q=81
R=82
S=83
T=84
U=85
V=86
W=87
X=88
Y=89
Z=90
If you need more help with your games, just ask....
Last edited by fjodorvanveen; 12-05-2008 at 01:29 PM.
Reason: forget code function
-
can we press button using the same keyboard ?
sampe to press button1 it use A, button2 use a
maybe I'm old, but I want to learn more
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|