|
-
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
-
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.addEventListener( MouseEvent.CLICK, onKeyClick );
function onKeyClick ( e:MouseEvent ):void {
enterPlayerID_txt.appendText( DisplayObject( e.target ).name );
}
-
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.
-
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.appendText( DisplayObject( e.target ).name );
}
}
-
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|