How to make a key is pressed in Flash.

Posted on 2005-03-09
Medium Priority
Last Modified: 2010-04-03
I create a fighting game which player may control the fighters with FLASH 5.
I use if(key.isdown(ENTER){
to perform some action inside the movieclip.

Now, i wanna build AI for computer fighters.
Is there got any action scripts make a key is pressed?
That means the action scripts is doing pressing a key function....

And i wanna make the numlock key is ON or CLOSE always by using action script.
is it possible? I just need info...

Sorry, not much point left for me....so i only give 25.
Question by:wkwebsite
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
LVL 18

Expert Comment

ID: 13497880
What key press do you want it to do and why?


Author Comment

ID: 13499968
Because my game fighter's movieclip got if (key.isdown......) actions.

When user press a key, the fightermc will moves. User may press different key to control the mc n perform alot of actions.

Now i wanna build AI system on the same fighter mc. That means the fighter mc will move randomly. I wanna the key is pressed automatically for the same mc (which user may control it in players versus mode, but computer control it in Computer versus mode).

I tried to create a movieclip send order for that mc with .gotoAndPlay actions.....but gotoAndplay actions not running for that mc, because it will cancel the current movement, n also something may not move correct according the fighter's skills.

(something like fighter must jump first before punch in the air) n......different fighters may have different kinds of movement. 800 frames for each......no point for me to add a script check if the if the fighter is jump, or get hit or knocked down then goto frame  bla bla bla..........
(i will go crazy........if i doing this......)

So, i think the best way is make the player control Key be pressed randomly.....
LVL 18

Expert Comment

ID: 13500080
I'm sorry I don't know, but for player vs computer it would make more sense (I think) to have a function for computerplayer  - even if you were able to mimic keystrokes, it would most likely confuse the player's keystrokes.

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 13504719
Flash cannot simulate Key Press event. Only trapping can be achieved with onKeyPressed() event...

If a function is attached to a key (or accociated rather) then yes, you could simulate it, by calling that function.

Lets say you have the Key F2. When F2 is pressed on the Keyboard, it calls the function "myFunction". So now you want to simulate this? Well, whatever you doing, just call the "myFunction".

Hope this explains it well.


Author Comment

ID: 13517041
ar.r.....the function u said is like if key.isdown, then call function......function is already there,
but wat i want to make a key is pressed itself... Like Key F2 function u said, the function is there, but i wanna make the F2 is pressed..so that the function will run.

i think it is impossible for flash to do that thing...
LVL 18

Expert Comment

ID: 13517267
You can put a list of possible choices in an array and then access that randomly to activate the function/s - if that helps


Author Comment

ID: 13563903
thx billy, i know wat u mean..but the main thing is the trigger.
Key is not pressed won't run the action inside.....i donno how to explain it.

Ok,let say inside my movieclip frames got scripts like

IF 1st Frame
If(key.isdown(left) && this._x>enemy.x){ // LEFT KEY is DOwn
gotoAndPlay(40);  // FOR BLOCK


BUT In 40th Frame
If(key.isdown(left) && this._x>enemy.x){  // LEFT KEY IS Down
gotoAndPlay(100);  // FOR ROLLING BACKWARD

So, when a "LEFT" key is pressed in different situation, the fighter mc will act differently n accordingly.

IF i using everysecond running function, letsay running ROLLING action(LEFT KEY): THe fighter must be getting hit first before he can rolling back. Different situation will hav different key reaction. if running function everysecond, the fighter maybe suddenly jump n punch (must jump before punch) or it can rolling backward anytime without getting a hit first.

I cannot make a if function to check that fighter is jump or block or hurling a power b4 to running a function. coz it is too hard to make it, n alot of value( like if block == true, jump ==true or rush==true) need to be added in For 800 frame X 8 fighters.

I really donno how to explain this 2 make u all understand.
anyway, thanks for ur help. :)

LVL 18

Accepted Solution

Billystyx earned 600 total points
ID: 13564127
The only thing I can suggest is that you may have to rewrite all of these moves for computer fighter. .. if its true that you want the computer now to move randomly, and the random moves mostly relate to which frame is playing, you may need a
randomplay();//where randomplay is a function full of random moves for the computer
if(key.isDown etc....

but aside from that I don't know if I can help


Author Comment

ID: 13571833
ermmm, thanks 4 help,
i change 2 another question....
i wanna ask is there anyway to make the NumLock key or Caplock is on always with action scripts?

n for Flash 5.
IF(key.isDOwn(Punch)){   //
gotoAndPlay (500);

If i holding the "Punch", the mc will go to frame 500 (for punch action).
 how do i make it punch once only.

So i try to use this for punch.
IF(key.isDOwn(Punch) && action==0){   //
 gotoAndPlay (500);

outside the mc i put

But i think is not a good idea.....coz if got two fighters, key collision will happens alwiz.....
Somemore, when a player press more than 4 keys, the other side of player will not able to control the fighter. And also, the timing is very hard to get for the movement key.....

Let say, a special movement keys is down, forward n then punch to be pressed in sequence
but i can only make like

if (key.isdown(Down) && Key.isdown(forward) && key.isdown(Punch){

So the user just hit 3 keys together but not in sequence for the special move.

anyway to solve?


Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are times in your Flash CS4 application when you want more than a simple pointer or a hand, and it's hard to find an ideal walk-through to tell you what to do.  I spent a few days recently learning my way around making custom cursors in Flash,…
The last time I worked with Flash and Socket connections was in AS1. A recent project required flash connecting to a Socket, and sending receiving information - we figured it would be easy enough - we all know about the socket policy documents and c…
The goal of the tutorial is to teach the user how to how to record live broadcast.
The goal of the tutorial is to teach the user how to use the auto adjust feature and what the different options do. When your video is not working right you can choose the auto adjust feature to help choose your settings.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question