Solved

Flash Controls - WASD (Urgent)

Posted on 2006-11-28
2
235 Views
Last Modified: 2008-02-01
Hello all;

Currently I am attempting to make a flash game with 2 players. One player uses the Arrow keys and the other uses the WASD combination.

For the character with the arrow keys, I have the following action script:

onClipEvent (load) {
      // declare and set speed variable
      speed = 10;
}
onClipEvent (enterFrame) {
      // move up, down, left, or right
      if (Key.isDown(Key.LEFT) && !Key.isDown(Key.RIGHT)) {
            _x -= speed;
            _rotation = 270;
      }
      if (Key.isDown(Key.RIGHT) && !Key.isDown(Key.LEFT)) {
            _x += speed;
            _rotation = 90;
      }
      if (Key.isDown(Key.UP) && !Key.isDown(Key.DOWN)) {
            _y -= speed;
            _rotation = 0;
      }
      if (Key.isDown(Key.DOWN) && !Key.isDown(Key.UP)) {
            _y += speed;
            _rotation = 180;
      }
      //
      // move diagonally
      if (Key.isDown(Key.LEFT) && Key.isDown(Key.UP) && !Key.isDown(Key.RIGHT) && !Key.isDown(Key.DOWN)) {
            _rotation = 315;
      }
      if (Key.isDown(Key.RIGHT) && Key.isDown(Key.UP) && !Key.isDown(Key.LEFT) && !Key.isDown(Key.DOWN)) {
            _rotation = 45;
      }
      if (Key.isDown(Key.LEFT) && Key.isDown(Key.DOWN) && !Key.isDown(Key.RIGHT) && !Key.isDown(Key.UP)) {
            _rotation = 225;
      }
      if (Key.isDown(Key.RIGHT) && Key.isDown(Key.DOWN) && !Key.isDown(Key.LEFT) && !Key.isDown(Key.UP)) {
            _rotation = 135;
      }
}

When I attempt to change LEFT, RIGHT, UP, DOWN with A, D, W, S, it doesn't seem to work. I have also tried replacing them with the ascii code for the letters (which I saw on another website), but that didn't seem to work either.

What would I do to make the WASD combination work?

Thanks in advance.
0
Comment
Question by:dh-s
[X]
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
  • 2
2 Comments
 
LVL 34

Accepted Solution

by:
Aneesh Chopra earned 500 total points
ID: 18035200
You should not use onEnterFrame for key listening.. use addKeyListner on KEY

here is the code for you:
I have provided the events for each key you want, you can add ur code for each event
-------------
var myKeyListener = new Object();
myKeyListener.onKeyDown = function()
{
      switch (Key.getCode())
      {
      case Key.UP :
            trace("key pressed:UP");
            break;
      case Key.DOWN :
            trace("key pressed:DOWN");
            break;
      case Key.LEFT :
            trace("key pressed:LEFT");
            break;
      case Key.RIGHT :
            trace("key pressed:RIGHT");
            break;
      case 65 :
            trace("key pressed:A");
            break;
      case 68 :
            trace("key pressed:D");
            break;
      case 83 :
            trace("key pressed:S");
            break;
      case 87 :
            trace("key pressed:W");
            break;
      default :
      }
};
Key.addListener(myKeyListener);
----------


-Aneesh
0
 
LVL 34

Expert Comment

by:Aneesh Chopra
ID: 18050781
Have to tried my solution?
I request if you are satisfy with reply please accept my anwser

-Aneesh
0

Featured Post

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!

Question has a verified solution.

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

This is intended to introduce all collision detection principles in flash, their strengths, weaknesses and workarounds. The main method for Collision Detection in flash is using hitTestObject. But unless you'll be pushing rectangular shapes without …
While working over numerous projects I often had the requirement for doing a screen capture in AS3.0. Unfortunately I found no "ready made" solutions in google search that suited my requirements. But I did come across some great resources which help…
In this tutorial viewers will learn how to create a basic shape tween animation in Flash including shape hints for smooth animation Open a new document in Flash: Draw a shape: Select another frame (how long you want the tween to be): Right click and…
The goal of the tutorial is to teach the user how to select the video input device. Make sure you have an input device that in connected and work and recognized by Adobe Flash Media Live Encoder and select it in the “video input” menu.

626 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