-
Keyboard toggling
Hello again,
I am working on a new game with actionscript 3 and was coming up with some complications.
I am very used to actionscript 2.0 and I've been trying to force feed this new script and learn as much as possible.
Now for the question. I have a Keyboard event setup like this.
Code:
stage.addEventListener(KeyboardEvent.KEY_DOWN, guyMove);
stage.addEventListener(KeyboardEvent.KEY_UP, guyStop);
function guyMove(event:KeyboardEvent):void {
if(event.keyCode == 39) {
trace("right");
}
if(event.keyCode == 40) {
trace("down");
}
if(event.keyCode == 37) {
trace("left");
}
if(event.keyCode == 32) {
trace("space");
}
}
function guyStop(event:KeyboardEvent):void {
if(event.keyCode == 39) {
trace("right");
}
if(event.keyCode == 40) {
trace("down");
}
if(event.keyCode == 37) {
trace("left");
}
if(event.keyCode == 32) {
trace("space");
}
}
It actually took me a little while to get that working in some fashion. :rolleyes: But it wasn't acting the way I wanted. When I press and hold a key, it repeats the guyMove function as if I held down a key when I was typing. It would run once, and moments later, run constantly.
I was kind of looking to get a toggle effect going. So it would trace once when I press, and once when I release. Or even simpler if I could combine into one function and actually trace the press and release without the repeating press trace if I hold the key down.
In as2.0 I would usually use an
Code:
onEnterFrame() {
if(Key.isDown(Key.LEFT) {
//do something
}
}
That would usually work fine, it would do what I want when I pressed it in an enterFrame fashion, and stop when I release the key.
ANy suggestions?
Thanks
~Bill L.
-
You can still do something like that.
PHP Code:
stage.addEventListener (KeyboardEvent.KEY_DOWN, guyMove);
stage.addEventListener (KeyboardEvent.KEY_UP, guyStop);
var kCode:int;
function guyMove (event:KeyboardEvent):void
{
kCode = event.keyCode;
event.currentTarget.removeEventListener (KeyboardEvent.KEY_DOWN, guyMove);
addEventListener (Event.ENTER_FRAME,ef);
}
function ef (e:Event):void
{
if (kCode == 39)
{
trace ("right");
}
if (kCode == 40)
{
trace ("down");
}
if (kCode == 37)
{
trace ("left");
}
if (kCode == 32)
{
trace ("space");
}
}
function guyStop (event:KeyboardEvent):void
{
removeEventListener (Event.ENTER_FRAME,ef);
event.currentTarget.addEventListener (KeyboardEvent.KEY_DOWN, guyMove);
}
-
1 Attachment(s)
************EDITED**********************
I am starting to understand it better, thanks you.
I wanted to know something else as well, :P.
If I wanted to make it recognize when you are pressing more than one thing, that wouldn't work, would it? I'm pretty sure it will only recognize one key press at a time.
I was thinking of setting up different functions for each key that may be pressed, and keep the guyMove function listener active all the time. So if I pressed right it would go to the moveRight function. Then if I pressed spacebar, it would go to moveSpace function.
Do you think that would work?
******************************************
I actually found the answer to that last question, and now I have a problem with the walls MC.
What happens is, I put a few MCs inside that walls MC
, but when I get to a certain number of them, my code stops working. I can't figure out why for the life of me... I've moved it around to see if it was the placement. But it just doesnt work with a certain number of MCs... Would there be some kind of confusion with the childAt() function I'm using???
Thanks again,
~Bill Ludwig
P.S. I attached the fla to show you what I am talking about...
-
OMG, I figured it out. I've been messing with it all day, and figured out its a screw up in my "falling" code. So nevermind.
I figure I'll just spend some solid time on some of these problems before resorting to the forums.
Thanks again for all of the help so far!!!!!
Much obliged,
~Bill Ludwig