- public
function
removeListener
(listener
:
Object)
:
Void
- {
- for
(var
i
:
Number
=
0;
i
<
listeners.length;
i
++)
- {
- if
(listeners
[i]
==
listener)
listeners.splice
(i,
1);
- }
- }
The removeListener method does the exact opposite of
your addListener function. It too takes an Object as its
argument, but it removes the Object if it finds it in
the listeners array. Flash does not have a remove method
for arrays, but you can simulate a remove function by
using the splice method:
- listeners.splice
(i,
1);
- public
function
addCombination
(name
:
String, keyCode1
:
Number, keyCode2
:
Number)
:
Void
- {
- key_combinations.push
(arguments);
- }
This method takes in the three arguments that make up
your multiple key combination. Notice how the arguments
are being added to the key_combinations array. Instead
of pushing them into the array one variable at a time,
the keyword 'arguments' is passed in instead.
The arguments keyword captures all of the data passed
into your method. You can use this approach when you
can't accurately predict the amount of arguments that
you might get passed into a method. For example, you may
have key combinations that require more than two
keyboard presses such as Ctrl + Shift + N, for example.
The final format of the arguments will be (String,
Number, Number). When you pass in, for example,
Key.Control, the ASCII-code equivalent is passed in
instead. A sample trace of the arguments path for Ctrl +
Y would be:
redo,17,89
Let's now take a look at how your key_combinations
actually stores your data. If you were to add Ctrl + Z
and Ctrl + N, as your combinations, you would use the
following code in your FLA:
- keyDet.addCombination("undo",
Key.CONTROL,
90);
- keyDet.addCombination("redo",
Key.CONTROL,
89);
Each of the arguments you made to the addCombination
method is stored in the key_combinations array as a
nested array item. Your array basically resembles the
following structure:
[[undo,17,90],[redo,17,89],[new,17,85]]
- private
function
invokeOnKeyCombination
(combo_name
:
String )
:
Void
- {
- for
(var
i
:
Number
=
0;
i
<
listeners.length;
i
++)
- {
- listeners
[i].onKeyCombination
(combo_name);
- }
- }
This function takes in the name of your combo,
combo_name, as the argument. It then cycles through all
of your listener objects and passes the combo_name
argument to the onKeyCombination method of each listener
object. If you recall, the onKeyCombination method is
defined in your FLA.
More code explanations on the
next page!