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
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: