PDA

View Full Version : [fmx] arrow keys vs. AWSD keys



tgelston
May 16th, 2003, 09:18 AM
How do I get the AWSD keys to work as smoothly as the arrow keys. I can put the arrow keys inside an enterFrame

onClipEvent (enterFrame) {
//move the tank
if (Key.isDown(Key.RIGHT)) {
this._rotation += 10;
} else if (Key.isDown(Key.LEFT)) {
this._rotation -= 10;


But the AWD keys I can only get to work if I put them inside a keyDown

onClipEvent (keyDown) {
//move the tank
if (Key.getCode() == 68) {
this._rotation += 10;
} else if (Key.getCode() == 65) {
this._rotation -= 10;
} else if (Key.getCode() == 87) {

If I put the AWD keys inside an enterFrame they run until I push another button

Thanks in advance!

-Tobias
http://www.gelstoncafe.com

lostinbeta
May 16th, 2003, 12:45 PM
Check the differences in your if statements;)

For the enterFrame you use Key.isDown(), but for the keyDown you use Key.getCode()

tgelston
May 16th, 2003, 03:46 PM
LostinBeta,

I have never been successful using
if (Key.isDown() == 68) {
inside an enterFrame - am I doing something wrong?

Thanks,
Tobias

lostinbeta
May 16th, 2003, 04:05 PM
Yeah it is if (Key.isDown(68)) not if (Key.isDown() == 68)

Also what are you using this for? There may be a way to optimize it using Key Listeners or through keyDown so that its not running in an onEnterFrame and using up cpu resources.

tgelston
May 16th, 2003, 04:50 PM
Thanks! That works much much better
I am using this to move a tank around the screen - kind of like the original combat for atari

Here is all my keycode - if there is a more efficient way I would love to see it! Thanks for you time.



onClipEvent (enterFrame) {
//move the tank
if (Key.isDown(68)){
this._rotation += 10;
} else if (Key.isDown(65)) {
this._rotation -= 10;
} else if (Key.isDown(87)) {
this._x += (Math.cos(Math.PI/180*this._rotation)*_root.speed);
this._y += (Math.sin(Math.PI/180*this._rotation)*_root.speed);
} else if ((Key.isDown(Key.SHIFT)) and (_root.timer2 == 0)) {
depth++;
name = "projectile"+depth;
_root.shot2.duplicateMovieClip(name, depth);
_root.timer2 = 1;
_root[name]._x = this._x;
//*Math.cos(angle);
_root[name]._y = this._y;
//*Math.sin(angle);
}
}


THANKS

lostinbeta
May 16th, 2003, 05:01 PM
Considering what you are using it for I think you are fine.

Just a tip though, when using if/else statements you must remember that the else only gets activated if the previous statement is false. I noticed you contained everything as if/else if statements. Well this may cause some complications in movement, so you may want to put some things as seperate if statments if it causes any problems.

tgelston
May 16th, 2003, 05:52 PM
Thanks! I appreciate your help!