PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
As3: keyBoard eventListener working slow
A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: As3: keyBoard eventListener working slow

  1. #1
    Member
    Join Date
    Nov 2008
    Posts
    49

    As3: keyBoard eventListener working slow

    Hello mates,


    I am trying to move an object on stage with arrow keys. I am using this code

    Actionscript Code:
    stage.addEventListener(KeyboardEvent.KEY_DOWN, moveCatcher);

    function moveCatcher(event:KeyboardEvent):void {

        if(event.keyCode == Keyboard.LEFT && catcher.x >= 0)
            catcher.x -=8;
        else if(event.keyCode == Keyboard.RIGHT && catcher.x < 600)
            catcher.x +=8;
       
    }

    The problem is that it do not start to move immediately after I press the key and it takes few moments to move. I have uploaded the swf file here http://www.evaic.com/catcher202kkles...oqwnlssewe.swf

    Please suggest some solution if you figured out the problem,


    Regards
    Last edited by just_started; 03-27-2011 at 07:14 PM.

  2. #2
    Senior Member
    Join Date
    Jun 2008
    Posts
    549
    You need to detect both key up and down press. Try something like below.

    var rightClick:Boolean = false;
    var leftClick:Boolean = false;

    stage.addEventListener(KeyboardEvent.KEY_DOWN, CatcherMove);
    stage.addEventListener(KeyboardEvent.KEY_UP, CatcherNoMove);
    stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);


    function CatcherMove(e:MouseEvent):void{
    switch(e.keyCode){
    case Keyboard.LEFT: leftClick = true; break;
    case Keyboard.RIGHT: rightClick = true; break;
    }
    }


    function CatcherNoMove(e:MouseEvent):void{
    switch(e.keyCode){
    case Keyboard.LEFT: leftClick = false; break;
    case Keyboard.RIGHT: rightClick = false; break;
    }
    }


    function enterFrameHandler(e:Event):void{
    if(rightClick && catcher.x >= 0) catcher.x -=8;
    if(leftClick && catcher.x < 600) catcher.x +=8;
    }

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