To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here


A Flash Developer Resource Site

Go Back   Flash Kit Community Forums > Flash Help > Flash ActionScript

Reply
 
Thread Tools Search this Thread Display Modes
Old 10-18-2004, 11:31 PM   #1
t_roy_flash
Member
 
Join Date: Aug 2003
Location: Australia
Posts: 38
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
__________________
T-Roy
t_roy_flash is offline   Reply With Quote
Old 10-18-2004, 11:38 PM   #2
jbum
Senior Member
 
jbum's Avatar
 
Join Date: Feb 2004
Location: Los Angeles
Posts: 2,920
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 11:45 PM.
jbum is offline   Reply With Quote
Old 10-19-2004, 05:20 AM   #3
t_roy_flash
Member
 
Join Date: Aug 2003
Location: Australia
Posts: 38
looks good, i'll have a crack at it later, thanks for ur help!
__________________
T-Roy
t_roy_flash is offline   Reply With Quote
Old 10-20-2004, 01:19 AM   #4
t_roy_flash
Member
 
Join Date: Aug 2003
Location: Australia
Posts: 38
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
__________________
T-Roy
t_roy_flash is offline   Reply With Quote
Old 10-20-2004, 03:43 AM   #5
jbum
Senior Member
 
jbum's Avatar
 
Join Date: Feb 2004
Location: Los Angeles
Posts: 2,920
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;
__________________
jbum is offline   Reply With Quote
Old 10-20-2004, 08:50 AM   #6
t_roy_flash
Member
 
Join Date: Aug 2003
Location: Australia
Posts: 38
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!
__________________
T-Roy
t_roy_flash is offline   Reply With Quote
Old 02-23-2007, 08:57 PM   #7
OzzieOrca
Junior Member
 
Join Date: Aug 2006
Posts: 3
Ascii Multiple Keys

Is there any way to get flash to detect more than one ascii key at once?
OzzieOrca is offline   Reply With Quote
Old 03-09-2007, 02:49 AM   #8
OzzieOrca
Junior Member
 
Join Date: Aug 2006
Posts: 3
Anybody?
__________________
Scotty - OzzieOrca.com - OzzieOrca Forums
OzzieOrca is offline   Reply With Quote
Old 07-23-2007, 02:03 AM   #9
OzzieOrca
Junior Member
 
Join Date: Aug 2006
Posts: 3
you can use:
Code:
if(Key.isDown(code:number)){
}
so for W u would use:
Code:
if(Key.isDown(87)){
}
__________________
Scotty - OzzieOrca.com - OzzieOrca Forums
OzzieOrca is offline   Reply With Quote
Old 07-23-2007, 04:53 PM   #10
shavingcream
14yr old Member
 
shavingcream's Avatar
 
Join Date: Jun 2007
Location: Santa Cruz CA
Posts: 272
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
shavingcream is offline   Reply With Quote
Old 12-05-2008, 01:27 PM   #11
fjodorvanveen
Member
 
Join Date: Sep 2008
Posts: 36
Smile 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
fjodorvanveen is offline   Reply With Quote
Reply

Go Back   Flash Kit Community Forums > Flash Help > Flash ActionScript

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 11:23 PM.


internet.commerce
Be a Commerce Partner
 »  »  »  »  »  »  »
 »  »  »  »  »  »
 

    

Acceptable Use Policy

internet.comMediabistrojusttechjobs.comGraphics.com

WebMediaBrands Corporate Info


Advertise | Newsletters | Feedback | Submit News

Legal Notices | Licensing | Permissions | Privacy Policy


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.