PhxFPUG Assets
If you’re coming to the Phoenix Flash Platform User Group meeting tonight (and I hope you are!) or you are there now, or maybe you heard about how cool it was then these things might be helpful for you.
The two tools we’ll be talking about are RobotLegs and Swiz. Here are the links to their respective locations on the interweb.
If you’re playing along then you might want to download the projects I’ll be demoing. Please keep in mind that these projects attempt to demonstrate how these tools work; NOT NECESSARILY BEST PRACTICES. They are only meant to give you a basic understanding of the principals.
If you just want to download the .swc files (which are included in the projects as necessary) then you can grab them here.
Swiz 0.6.4 (for Flex 3)
And if you want to “borrow” my Facebook App key and secret you can use these values (they are going to get changed after tonight and it’s a sandbox app anyway so you won’t be able to do anything fun with it anyway).
API Key: abcdefghijlkmnopqrstuvwxyz
Secret: nowiknowmyabcsnexttimewontyousingwithme
Lastly, if you care to follow along with my slides you can download those here. You probably won’t get much without me standing in front of them being awesome, but you are welcome to them if you want ‘em.
FlashVar manager using Swiz or RobotLegs
I use FlashVars a lot. Sometimes I need to open or launch something from my application on startup. Sometimes I just need to know some authentication credentials and that’s the way they are passed to me. Something that I have found that has made this easier for me has been to create a FlashVarManager for my projects.
FlashVars are gleaned from a LoaderInfo object and it is just a dynamic Object. (loaderInfo.properties to be exact). But you probably knew that. Being a fan of strongly typedness I usually create some getters on my manager that will fetch a specific flashVar from the .properties object and if there isn’t one give me a default. Since I always did this for Flex projects the most straightforward way to grab what I needed was through the Application instance. My manager would look something like this.
{
public class FlashVarManager
{
private static var:_instance:FlashVarManager;
private var _flashVars:Object;
public static function getInstance():FlashVarManager
{
if(!_instance)
{
_instance = new FlashVarManager();
_flashVars = Application(Application.application).loaderInfo.properties;
}
return _instance;
}
public function get flashVars():Object
{
return _flashVars;
}
public function get specificFlashVarValue():String
{
if(flashVars[‘specificValue’] != undefined)
return flashVars[‘specificValue’];
else
return "someDefaultValue";
}
}
}
Something like that.
So if ever my flashVars had to change I could just change this class. I will often perform logic in here too; sometimes create a faux “flashVar” that is based on a number of flashVars. I find it pretty handy.
But I’m trying to eliminate Singletons wherever I can. Not necessarily Singletons in the “one instance exists” sense, but more in the .getInstance() sense. RobotLegs and Swiz are really helping me out with that. But how to construct my FlashVarManager with those tools? How do you get to the loaderInfo of the app (without using the messy Application() hack)?
Read more
Signals may spell the death to my Commands as I know them
Back when I wrote my Command library I was doing a lot of calling of commands. It actually stems from the Commands/Delegates I created for the Facebook AS3 API (which have since been removed; which I think is a shame). Essentially the greatest benefit I got from my commands with the ability to do this:
function onComplete(commandInstance:CommandClass):void
{ // do stuff }
instead of this:
function onComplete(e:Event):void
{
var commandInstance:CommandClass = e.target as CommandClass;
commandInstance.removeEventListener(Event.COMPLETE, onComplete);
//NOW I can do stuff
}
That got rid of a lot of code for me and made me very happy.
Boss: Jason, how many lines of code did you write today?
Me: (proudly) Negative two-hundred-sixty-nine!
Just kidding. My boss has never asked me how many lines of code I wrote.
Read more
My Swiz Tryout
I’ve just given Swiz a little test-run. I’m not going to write about how Swiz works or anything here. There’s plenty of that all over the place by people much more skilled than myself. But so far my experience has been very positive. I’ve converted a medium-sized project into using it instead of a hand-rolled solution. I’m still using a lot of the original code but since it was already in an MVC pattern it was easy to change the important bits. I’m a big fan of my command library and have a lot of code written that depends on it. I’m sure there are a dozen just like it out there and probably something just like it rolled into Swiz (I haven’t found out yet, mine worked without change so I didn’t.) I was very pleased to see that they fit so smoothly into this framework.
Joe Rinehart’s post Swiz in 20 minutes was very instrimental in getting me up to speed very quickly. I don’t often opt for video presentations over the written work (searching, rewinding, etc is so much harder) but this was so straightforward I was able to watch it once and then just flip open Eclipse and do it. The minimal documentation on the Swiz page was helpful too of course since my memory only lasts about as long as it takes to switch from one tab to another.
But Swiz is Flex-only. One of the benefits of Swiz is that it augments the abilities of Flex instead of replaces them. I’m already using Flex right? Might as well . . . use it. But not everything I do is in Flex so I want to see if I can become familiar with a framework that will work in Flash as well and still stay out of my hair. It’s gotta be “micro” or I just can’t bring myself to care.
Thinking of giving RobotLegs the same treatment. It’s about to hit 1.0 and that always has a nice sound to it. Do a quick project conversion to see how it works in a real-world situation. Maybe Mate; I’ve got peeps that seem to like it a lot. Any suggestions?
Tracing Bitmaps in AS3
Recently I was tasked with creating a tool so that a user could draw some polygons in a Flash tool. It was for a map; simple polygons marked interact-able areas. The tool that users were using was old and kludgy and just didn’t work very well. Ok, easy enough. I built a simple polygon editor.
But in the application users will sometimes create HUNDREDS of polygons for a map. The polygons are all traced over a bitmap that’s loaded and shown in the background of the editor. For demonstration purposes lets say a bitmap to be traced looks like this:
While a tool letting a user click on each corner defining the points on the polygon works, that could become tedious work creating and adjusting hundreds of polygons with many potential points for each one. Wouldn’t it be a lot easier if the user could just click on the polygon in the bitmap and let the application figure out where the points are? (Click here to see that proof-of-concept in action.)
Our users sure thought so. So here’s how I went about doing that in ActionScript: