all groups > flash actionscript > april 2006 >
You're in the

flash actionscript

group:

How do I assign different Actions to letter keys?


How do I assign different Actions to letter keys? WeirdGuy
4/9/2006 11:39:09 PM
flash actionscript:
Well that's only half the question. Is there a way to make //if
(Key.isDown(Key.RIGHT)) command but work with other keys like the WASD keys. I
want to make something move with the WASD keys instead of the Arrow Keys.
However when I plug the commands into it I can't get it working with the
"Key.isDown" part so even if I stop pressing the WASD keys it doesn't stop my
little guy from moving.

I'm trying to find a way to mix these two codes for the same effect:

if (Key.isDown(Key.RIGHT))
if (thekey ==68)

I use " thekey = Key.getCode();" To get my "thekey" working. I basically want
a stoping effect like "Key.isDown" but with the second code there so that I can
use the WASD keys instead of teh arrows.






Here's the code I'm using.


onClipEvent(load){

moveSpeed=5;

}


onClipEvent (enterFrame) {
//Trace Numbers
thekey = Key.getCode();
trace(thekey);
//End Trace Numbers

//if (Key.isDown(Key.RIGHT))
if (thekey ==68) {
this._x+=moveSpeed;
//if (Key.isDown(Key.LEFT))
} else if (thekey ==65) {
this._x-=moveSpeed;
}
//if (Key.isDown(Key.DOWN))
if (thekey ==83) {
this._y+=moveSpeed;
//if (Key.isDown(Key.UP))
} else if (thekey ==87) {
this._y-=moveSpeed;
}

if(this._x <0){
this._x += 5;
}
if(this._x> "515"){
this._x -= 5;
}
if(this._y <0){
this._y += 5;
}
if(this._y> "364"){
this._y -= 5;
}
//test
if (cell_mc.hitTest(blockade_mc)) {
this._x-=moveSpeed;
this._y-=moveSpeed;
}
//test



}
Re: How do I assign different Actions to letter keys? abeall
4/10/2006 12:53:17 AM
You can just do:
Key.isDown(thekey)

Re: How do I assign different Actions to letter keys? WeirdGuy
4/10/2006 1:04:34 AM
Originally posted by: abeall
You can just do:
Key.isDown(thekey)

In fact, the Key.isDown method takes the argument 'keyCode' ... that's whay
you use Key.RIGHT which gives you the keyCode of the Right arrow key

I'm a beginer and therefor got confused by what you said. lol

I tried this stuff:
if (Key.isDown(thekey ==68)) {
this._x+=moveSpeed;

It didn't work.
I need it working for all the buttons W A S and D instead of the Arrows. If I
just put thekey it doesn't specify the button that's needed to press?
Re: How do I assign different Actions to letter keys? abeall
4/10/2006 1:28:49 AM
What I said is that the method, as described in the language reference, is:
Key.isDown(keyCode)

Where 'keyCode' is the key code of the key which should be down(not
surprisingly!). The constant Key.RIGHT returns the keyCode for the right arrow
key. Try this:
trace(Key.RIGHT)

Notice it traces a number. So 'thekey' should be just the keycode, since the
method is Key.isDown(keyCode):

if (Key.isDown(68))

Or to make it easier and more readable, you might define your own variables to
be used:

var keyA = 65;
var keyW = 87;
var keyD = 68;
var keyS = 83;
if(Key.isDown(keyW))

Or, one last option, is to use the String class to get the character code for
you, which is similar to what you are do with Key.RIGHT:

if(Key.isDown(String("W").charCodeAt(0))){

the part of interest there is:
String("W").charCodeAt(0)

String() is "constructing" the letter "W" as a String object, more or less,
and the charCodeAt(index) is sort of like getCode, where it gives you the
character code of the letter at the 'index' which starts at 0.
Re: How do I assign different Actions to letter keys? WeirdGuy
4/10/2006 1:34:59 AM
Wow, I actually get it now. Thanks! If I could kiss you... well, no need to go that far.

Re: How do I assign different Actions to letter keys? Star Tail Pro
4/10/2006 2:04:45 AM
AddThis Social Bookmark Button