A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: Using MouseEvent and KeyboardEvent on the same function

  1. #1
    Senior Member
    Join Date
    Jul 2000
    Posts
    373

    Using MouseEvent and KeyboardEvent on the same function

    I currently have an AS3 fla with a couple of mouseEvent functions but now the client wants to add a KeyboardEvent as well to the same exact functions. Can I add another eventListener to that without having to create a second function? It would be a huge waste of code if I did. Here's what I currently have in truncated form:

    rightArrow.addEventListener(MouseEvent.CLICK, slide_right);
    leftArrow.addEventListener(MouseEvent.CLICK, slide_left);

    function slide_right(e:MouseEvent):void {
    // Code goes here
    }

    function slide_left(e:MouseEvent):void {
    // Code goes here
    }
    Adam Bell
    dzign@datatv.com
    --
    Over 90% of all websites
    suck......
    Join the minority.

  2. #2
    AS3 Mod
    Join Date
    Sep 2007
    Location
    O-H-I-O
    Posts
    2,385
    Yes. Just change your event arg datatype to the following:
    PHP Code:
    function slide_right(e:Event):void {
        
    // Code goes here
    }

    function 
    slide_left(e:Event):void {
        
    // Code goes here


  3. #3
    Senior Member
    Join Date
    Jul 2000
    Posts
    373
    OK, I'll try that. But where do I add the event listeners for the keyboard events? Which in this case would be the right and left arrow keys respectively for the slide_right and slide_left functions. Would I place those underneath the other two addEventListener objects or inside of the functions themselves? I would think underneath the other two event listeners but I just want to confirm it. Thanks.
    Adam Bell
    dzign@datatv.com
    --
    Over 90% of all websites
    suck......
    Join the minority.

  4. #4

  5. #5
    Senior Member
    Join Date
    Jul 2000
    Posts
    373
    Do I add it like this?

    stage.addEventListener(KeyboardEvent.KEY_DOWN, slideRight);
    stage.addEventListener(KeyboardEvent.KEY_DOWN, slideLeft);

    I've been reading through a bunch of different tutorials to figure out how to do this and this is the closest I've found although I'm still confused as to how to target the left and right arrow keys to enable the functions.
    Adam Bell
    dzign@datatv.com
    --
    Over 90% of all websites
    suck......
    Join the minority.

Tags for this Thread

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