Archived entries for Hints & Tips

Static initializers in AS3

Recently, several people have asked whether it’s possible to create some kind of constructor for static classes in ActionScript 3, and the answer is: yes!

The solution is  a static initializer. This is a block of code, surrounded by curly brackets but no function or var declaration, that runs the first time a property or method of a static class is called. Static initializers are called before any property value is returned or function executed, in the same way a constructor always runs before you can access the properties or methods or a regular class.

package
{
    public class MyStaticClass
    {
        // This is the static initializer
        {
            trace("Running static initilizer!");
            staticInitializer();
        }

        // We're calling a secondary method so we can use local variables
        static private function staticInitializer():void
        {
            var now:Date = new Date;
            baseColor = now.hours > 16 ? 0x000066 : 0x0000FF;
        }

        static public var baseColor:uint;
    }
}

Although it’s not strictly necessary to call a secondary method, Flash Player will throw an error if you declare local variables within the static initializer itself.

To prove that the static initializer is running first, simple trace out the baseColor property:

trace(MyStaticClass.baseColor);

If it’s after 4pm, this will result in the following output in the console window:

Running the static initializer!
102

Essential Eclipse plug-ins for Flash Builder 4

With the recent relase of Adobe Flash Builder 4, we thought we’d flag up the update URLs for a few of our favourite Eclipse plug-ins to save you having to hunt around for them.

To install plug-ins:

  1. Copy the appropriate URL to your clipboard
  2. In Flash Builder, select Install New Software… from the Help menu
  3. Click Add, paste URL in the Location field and click OK
  4. Select the plug-ins to install and click Finish

If you’ve spotted anything we’ve missed, add a comment or  tweet it to @mesmotronic.

Eclipse.org

This URL contains links to most of the plug-ins hosted on Eclipse.org, allowing you to add functionality like ANT, JDT (Java Development Tools) and PDT (PHP Development Tools). You’ll also need to add this URL to Flash Builder to enable you to install other plug-ins, like soapUI:

http://download.eclipse.org/releases/galileo/

Subclipse (SVN)

If you’re a regular user of version control, and you should be, the subclipse plug-in is essential:

http://subclipse.tigris.org/update_1.6.x

soapUI

For anyone who uses Web Services, soapUI offers a great way to view and test your services:

http://www.soapui.org/eclipse/update/site.xml

Android SDK

Now that Adobe are launching AIR for Android, ADT (Android Developer Tools) isn’t exactly essential, but still worth a look if you’re hoping to develop mobile apps for something other than an iPhone:

https://dl-ssl.google.com/android/eclipse/

Creating a simple HTTP proxy using PHP

Cross domain policy files, or rather a lack of them, are the bane of Flash and Flex developers’ lives. Whether it’s the Twitter API, data from Yahoo! Finance or one of any number of other data sources, the moment your SWF makes it to the web you’re faced with the same problem: Security Error!

The solution is to create a simple proxy on your server that can load the data for you and pass it to your application. If you’re using PHP, then this is about as simple as it gets:

<?=file_get_contents($_REQUEST["url"])?>

That’s it. Just paste that into a text file, save it as proxy.php and upload it to your server. You can then access any data you like simply by passing it a URL. Best of all, it works with both GET and POST, for example:

http://www.mydomain.com/proxy.php?url=http://blog.mesmotronic.com/index.php/feed

Happy proxy-ing!



Copyright © 2004–2009. All rights reserved.

RSS Feed. This blog is proudly powered by Wordpress and uses Modern Clix, a theme by Rodrigo Galindez.