dcsimg
A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Keyboard toggling

  1. #1

    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. 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.
    So many Ideas

    So little Time

  2. #2
    Mod cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,448
    You can still do something like that.
    PHP Code:
    stage.addEventListener (KeyboardEvent.KEY_DOWNguyMove);
    stage.addEventListener (KeyboardEvent.KEY_UPguyStop);
    var 
    kCode:int;
    function 
    guyMove (event:KeyboardEvent):void
    {
        
    kCode event.keyCode;
        
    event.currentTarget.removeEventListener (KeyboardEvent.KEY_DOWNguyMove);
        
    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_DOWNguyMove);

    - The right of the People to create Flash movies shall not be infringed. -
    | www.Flashscript.biz | Flashscript Biz Classes/Components |

  3. #3
    ************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...
    Attached Files Attached Files
    Last edited by thekillerbigmac; 04-27-2009 at 03:05 PM. Reason: New question, attaching file
    So many Ideas

    So little Time

  4. #4
    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
    So many Ideas

    So little Time

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center