AS3 – messaging between objects, a decoupled approach

Sunday, February 12th, 2012

I’m working on an interactive installation. I’m building a couple of AIR apps. A really useful object that I’ve worked with in the past is a Broadcaster. Think of it as the telephone operator of old…you know the kind at a switchboard that actually has to connect a wire between the two callers. Well, the Broadcaster is great for keeping objects decoupled from each other. Typically if you want to send message between objects, each object has to somehow know the existence of the other object. You do this by keeping a reference to it.

//This is inside the sender class
// save a reference to the receiver
// so we can tell it to do something later
var receiver;
receiver = {the_receiver_object};
receiver.reset_activity("fade_out");

With the Broadcaster class, you just tell the Broadcaster that you’re interested in a certain message. Any object in your application can fire that message. Once that is fired, you get an event and it’s up to your class to handle it.

// in the receiver class
// when we receive the "RESET" message,
// run the reset_activity() function.
Broadcaster.subscribe( "RESET", this.reset_activity );

function reset_activity ( e:RichEvent ) {
//do something
}

// in the sender class
// fire off the "RESET" message
// and also pass along some parameters
Broadcaster.fire( "RESET" , {transition:"fade_out"} );

 

Notice that neither class has any knowledge of the other class. They are really just ‘connected’ by the message that they are firing and listening to.

The beauty of this is that any class can fire a message and any class can listen(subscribe) to that message. The Broadcaster does the routing between sender and receiver just like the old telephone switchboard days!

Here’s the awesome minair library that has the Broadcaster class. If you just want to use this messaging architecture you’ll need to grab the Broadcaster, RichEvent, and the Log classes. Good luck!

Configuring git to ignore certain files

Friday, February 10th, 2012

I’ve been working with git on and off for about a month now. I’m on a Mac and git always picks up changes to the .DS_Store files within directories. Also when working with XCode, it picks up changes to the user workspace files, etc.

There are a few ways to tell git to ignore these files and not keep them under version control…mainly:

  1. putting a .gitignore file in a directory and listing filenames and/or filename patterns to ignore – this can be checked in just like any other file. The effect is that anyone that clones your directory also will ignore these files.
  2. entering the filenames and/or filename patterns to the .git/info/exclude file in your local repo – only affects your local repo
  3. running the command git config –global core.excludesfile ~/.gitignore_global (.gitignore_global is a file where you specify the ignore patterns) to globally ignore certain files (see the link above fore more details) – I haven’t tried this yet, but this seems great so that you don’t have to re-specify which files to ignore every time you work with a git repo