Ian,
That does help, (alot actually). I'm still an oo newb so, I really appreicate
the help and the simple-approach sample code. I wasn't sure how to deal with
the lack of hashMap.
Thanks everyone for help with this, I greatly appreciate it. I'll take a stab
at converting the java to AS 2.0... and may be back with more noob questions on
the conversion.
mga
Ian Thomas <[EMAIL PROTECTED]> wrote: You don't need any special hash class for
this; since you're using
string keys, you can simply use the Object class, like so:
class ChannelInput {
private handlerMap:Object = null;
function ChannelInput() {
handlerMap = new Object();
}
public function addKey(key:String, handler:KeyHandler):Void {
handlerMap[key]=handler;
}
public function processKey(key:String):Void {
var handler:KeyHandler = KeyHandler(handlerMap[key]);
if (handler == undefined) {
trace("ERROR: undefined handler for key \"" + key + "\"");
} else {
handler.handleKey(key);
}
}
HTH,
Ian
On 6/7/06, Bart Wttewaall wrote:
> The open-source Vegas framework at osflash.org makes use of a hashmap.
> Since every Object extends of CoreObject, a hashcode is added to every
> object. A nice solution, I think.
>
> static private var _initHashCode:Boolean =
> HashCode.initialize(CoreObject.prototype) ;
>
> 2006/6/7, Kevin Aebig :
> > Well the code that your friend is suggesting is pretty straightforward and
> > you could port it over to AS2 if you wanted. Instead of a hashMap, why not
> > just use an associative array? I'm pretty sure that I've seen a hash class
> > ripping around from either Brandan Hall or Samuel Wan based off of an
> > associative array.
> >
> > All in all, the code could be ported over with minimal effort... but there
> > would be some area's that you would need to be creative with.
> >
> > Cheers,
> >
> > Kevin
> >
> > -----Original Message-----
> > From: [EMAIL PROTECTED]
> > [mailto:[EMAIL PROTECTED] On Behalf Of black59mga
> > Sent: June 6, 2006 4:09 PM
> > To: Flashcoders mailing list
> > Subject: RE: [Flashcoders] How do real developers do key handling?
> >
> > I have 10 apps that will controlled with a TV remote control (no mouse).
> > There are only 12 keys on the remote, so the app that has focus will will
> > have to do very different things with a given key press, depending on
> > context.
> >
> > A java dev. friend suggested an approach using a hashMap, but we don't have
> > hashMap in AS 2.0. And I'm a bit confused by his sample code:
> >
> > public class ChannelInputTest {
> > public static void main(String argv[]) {
> >
> > // Create a new ChannelInput object and add a few keys to its map
> > ChannelInput ci1 = new ChannelInput();
> > ci1.addKey("1", new SimpleKeyHandler());
> > ci1.addKey("2", new AnotherSimpleKeyHandler());
> >
> > // Now handle some keys with this puppy
> > ci1.processKey("1");
> > ci1.processKey("2");
> >
> > // Create a SECOND ChannelInput object, and add some different
> > keys/handlers
> > ChannelInput ci2 = new ChannelInput();
> > ci2.addKey("3", new AnotherSimpleKeyHandler());
> > ci2.addKey("4", new SimpleKeyHandler());
> >
> > ci2.processKey("3");
> > ci2.processKey("4");
> >
> > // Finally - should get an error if a ChannelInput gets unmapped key
> > ci2.processKey("1");
> > }
> > }
> >
> > import java.util.HashMap;
> >
> > public class ChannelInput {
> > private HashMap handlerMap = null;
> >
> > ChannelInput() {
> > handlerMap = new HashMap();
> > }
> >
> > public void addKey(String key, KeyHandler handler) {
> > handlerMap.put(key, handler);
> > }
> >
> > public void processKey(String key) {
> > KeyHandler handler = (KeyHandler) handlerMap.get(key);
> >
> > if (handler == null) {
> > System.out.println("ERROR: null handler for key \"" + key + "\"");
> > } else {
> > handler.handleKey(key);
> > }
> > }
> >
> > public class SimpleKeyHandler implements KeyHandler {
> > public void handleKey(String key) {
> > System.out.println("SimpleKeyHandler Key " + key + " was pressed");
> > // Action for this key goes here
> > }
> > }
> >
> > interface KeyHandler {
> > public void handleKey(String key);
> > }
> >
> > <><><>
> > Kevin Aebig wrote: Perhaps if you told us what you're
> > trying to accomplish, we could offer
> > suggestions around it, but as far as catching specific keys, I'm pretty sure
> > you're doing it the only way possible.
> >
> > !k
> >
> > -----Original Message-----
> > From: [EMAIL PROTECTED]
> > [mailto:[EMAIL PROTECTED] On Behalf Of black59mga
> > Sent: June 6, 2006 3:01 PM
> > To: flash Coders
> > Subject: [Flashcoders] How do real developers do key handling?
> >
> > How do real actionscript developers do key handling? If you have several
> > (like 10) apps that are loaded into MCs in 'Main.swf'... What's a good way
> > to code the key handling? Right now I'm drowning in multiple key.GetCode()
> > switch statements that all live in my 'Main.as'. How do smart people do
> > this??
> >
> > Any suggestions, sample code, links to tutes etc., very greatly appreciated.
> >
> > -mga
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
_______________________________________________
[email protected]
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com