A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: [RESOLVED] AS3 Key.isDown Equivalent

  1. #1
    Yarrrrrr
    Join Date
    Jul 2006
    Location
    Calgary, Canada
    Posts
    89

    [RESOLVED] AS3 Key.isDown Equivalent

    Hey guys, this ones got me stumped

    In as2 to get keypress information you could use actionlisteners OR check the function Key.isDown(asciivalue);.
    But this has been depriciated in AS3, now Im making a game and you have a ship that turns left and right, and you can furl and unfurl the sails by hitting w and s, (left and right is a and d). Now if you hold a the ship starts turning left, but you hit w to unfurl the sails and the game stops detecting that you are pressing a and while the w works the ship stops turning left (can post example if needed).

    Checking for Key.isDown every frame got around this, is there a way to check if a key is down in as3? or to make the listeners work for this?

    Thanks,
    Ian

  2. #2
    Yarrrrrr
    Join Date
    Jul 2006
    Location
    Calgary, Canada
    Posts
    89
    I read up on this and making your own keydown tracker dosent work if the app losses focus and a key is pressed/released when focus is regained. Also imbedding as2 kind of seems like it defeats the purpose of using as3. Any ideas?

  3. #3
    Junior Member
    Join Date
    Jun 2007
    Posts
    18
    THis is the way I handle keyboard events to drive a car. Maybe it is not the best but it works for me

    Code:
    var keyUP:Boolean = false;
    var keyDOWN:Boolean = false;
    var keyRIGHT:Boolean = false;
    var keyLEFT:Boolean = false;
    stage.addEventListener(KeyboardEvent.KEY_UP,key_Up);
    stage.addEventListener(KeyboardEvent.KEY_DOWN,key_Down);
    
    
    function key_Up(event:KeyboardEvent) {
    	switch (event.keyCode) {
    		case 37 :
    			keyLEFT = false;
    			break;
    		case 38 :
    			keyUP = false;
    			break;
    		case 39 :
    			keyRIGHT = false;
    			break;
    		case 40 :
    			keyDOWN = false;
    	}
    }
    
    function key_Down(event:KeyboardEvent) {
    	switch (event.keyCode) {
    		case 37 :
    			keyLEFT = true;
    			break;
    		case 38 :
    			keyUP = true;
    			break;
    		case 39 :
    			keyRIGHT = true;
    			break;
    		case 40 :
    			keyDOWN = true;
    			
    	}
    }
    
    function myFunction (){
    if (keyUP) {
    ...
    }
    if (keyDOWN) {
    ...
    }
    if (keyLEFT) {
    ...
    }
    if (keyRIGHT) {
    ...
    }
    
    }

  4. #4
    Yarrrrrr
    Join Date
    Jul 2006
    Location
    Calgary, Canada
    Posts
    89
    yeah, i think that is what i will do, thanks

  5. #5
    Junior Member
    Join Date
    Aug 2008
    Posts
    1
    Quote Originally Posted by RockSpyder
    yeah, i think that is what i will do, thanks
    It's probably not that big of a deal if the app loses focus while key down, then user comes back and will have to hit that key to get whatever key was down to come back up, but... I'm kind of obsessive...

    Is there a way to detect if the app HAS lost focus?

    Arg, this kind of stuff really chaps me. I don't see why a newer version would LOSE support for instantly checking if a key is up or down. I think that's kind of nutty. I mean, the people who develop programming languages... are programmers... you'd think they would understand... ack, forget it, I could rant all day and get nowhere.

  6. #6

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