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
Printable View
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
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.
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
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!
Is there any way to get flash to detect more than one ascii key at once?
Anybody?
you can use:
so for W u would use:Code:if(Key.isDown(code:number)){
}
Code:if(Key.isDown(87)){
}
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
All keycodes: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...
}
}
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....
can we press button using the same keyboard ?
sampe to press button1 it use A, button2 use a