Flex’s FileReference.save() can only be called in a user event handler — how can I get around this?

Adobe does this as a sort of security measure to ensure users are the ones messing with files rather than potentially harmful code. My understanding is that they enforce this by only allowing handlers of (click?) events that originate from UI components to execute the FileReference methods, so generating your own events programmatically will not … Read more

Static Actionscript code analysis possibilities

Update Nov 2018: It would appear that Structure101 (new download page) no longer has an ActionScript variant. Original answer, links outdated: Download Structure101g and select the Actionscript flavor after installing the software. I’ve confirmed that it is able to map out class level and even function call dependencies in Flex/AS3 projects, and generate a visual … Read more

Using ‘File’ to save scene object locations to rebuild later in AS3

You are trying to write a DisplayObject into the file directly, this is prevented by Flash engine due to the way Flash handles default serialization of any object. In order to save a DisplayObject into the external resource, you need to employ IExternalizable on that object’s class and any class of objects you will plan … Read more

RegEx to match comma separated numbers with optional decimal part

This: \d{1,3}(,\d{3})*(\.\d\d)?|\.\d\d matches all of the following numbers: 1 12 .99 12.34 12,345.67 999,999,999,999,999.99 If you want to exclude numbers like 123a (street addresses for example), or 123.123 (numbers with more than 2 digits after the decimal point), try: (?<=\s|^)(\d{1,3}(,\d{3})*(\.\d\d)?|\.\d\d)(?=\s|$) A little demo (I guessed you’re using PHP): $text = “666a 1 fd 12 dfsa … Read more

How to deal with Number precision in Actionscript?

This is my generic solution for the problem (I have blogged about this here): var toFixed:Function = function(number:Number, factor:int) { return Math.round(number * factor)/factor; } For example: trace(toFixed(0.12345678, 10)); //0.1 Multiply 0.12345678 by 10; that gives us 1.2345678. When we round 1.2345678, we get 1.0, and finally, 1.0 divided by 10 equals 0.1. Another example: … Read more

Flex 3 – how to support HTTP Authentication URLRequest?

The syntax is a little different for URLRequest, but the idea’s the same: private function doWork():void { var req:URLRequest = new URLRequest(“http://yoursite.com/yourservice.ext”); req.method = URLRequestMethod.POST; req.data = new URLVariables(“name=John+Doe”); var encoder:Base64Encoder = new Base64Encoder(); encoder.encode(“yourusername:yourpassword”); var credsHeader:URLRequestHeader = new URLRequestHeader(“Authorization”, “Basic ” + encoder.toString()); req.requestHeaders.push(credsHeader); var loader:URLLoader = new URLLoader(); loader.load(req); } A couple of … Read more

What is the best way to get the minimum or maximum value from an Array of numbers?

The theoretical answers from everyone else are all neat, but let’s be pragmatic. ActionScript provides the tools you need so that you don’t even have to write a loop in this case! First, note that Math.min() and Math.max() can take any number of arguments. Also, it’s important to understand the apply() method available to Function … Read more