A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: How to abstract multiple buttons to use the same function?

  1. #1
    Confounded Flash User
    Join Date
    Apr 2001
    Location
    Chicago
    Posts
    39

    How to abstract multiple buttons to use the same function?

    Not sure I phrased that right, but here's the deal. I'm building a demo that has a keyboard in it. Obviously, people will type stuff with it and it will go into a dynamic text field.

    So I have a full keyboard (a movie clip named "keyboardStandard") made up of button symbols. That is placed in the same frame as the text field. Then I wrote this code, using the Q key ("keyQ"):

    Code:
    keyboardStandard.keyQ.addEventListener(MouseEvent.CLICK, addQ);
    
    function addQ(ev):void {
    	enterPlayerID_txt.appendText("Q");
    }
    This works just fine, the Q goes into the text field. However, I sure as heck don't want to write 26 event listeners, then 26 more functions, even if it is copy paste. That's just gnarly. Plus, there are actually 3 keyboards - a numeric and special char one as well I need to handle.

    I seems passing a parameter from the event listener is not doable, so how do I approach this?

    TIA

  2. #2
    Senior Member
    Join Date
    May 2004
    Posts
    226
    You could rename all your buttons to their actual character, for example rename "keyQ" to "Q" then just use one click listener on keyboardStandard:

    PHP Code:
    keyboardStandard.addEventListenerMouseEvent.CLICKonKeyClick );

    function 
    onKeyClick e:MouseEvent ):void {
        
    enterPlayerID_txt.appendTextDisplayObjecte.target ).name );


  3. #3
    Confounded Flash User
    Join Date
    Apr 2001
    Location
    Chicago
    Posts
    39
    Unfortunately that won't work for the other keys on the keyboard, which are space, lower case (to swap uc/lc) and a special character key that swaps keyboards.

  4. #4
    Senior Member
    Join Date
    May 2004
    Posts
    226
    Well then, you got to put some logic in:

    PHP Code:
    function onKeyClick e:MouseEvent ):void 
        if( 
    e.target == keyboardStandard.toggleCase ){
            
    //....
        
    }else if( e.target == keyboardStandard.switchKeys ){
            
    //...
        
    }else{
            
    enterPlayerID_txt.appendTextDisplayObjecte.target ).name ); 
        }


  5. #5
    Confounded Flash User
    Join Date
    Apr 2001
    Location
    Chicago
    Posts
    39
    Thanks I'll give that a try unless there are any other suggestions, but it looks like a decent way to go. This is demoware, not production code, so it doesn't have to be perfect, but I always like to try and find the best way to do things.

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