PixLib part 2. More configuration features and a ‘hidden’ gem.

After the previous success of PixLib dealing with my configuration needs, today brought a new challenge, creating instances at runtime from string identifiers (aka Class Factories).

To complete part of the project im working on in a nice way I want to be able to create instances of UI classes at runtime, using their fully qualified package names as identifiers. e.g. com.relivethefuture.ui.Stage.

The package names will be specified in the config file, but the class constructors need to be passed parameters which can’t be declared in the config as well.

IF the instances could be created without parameters then I could have used the config type called ‘class’. This would have meant that the framework would have done the hard work for me and created me an instance of the class specified.

When you are able to use this feature you can setup your config like this (note the quotes around the package name and any string arguments)

<config>
    <instance type="class">'com.mydomain.MyClass',23,12,'hi Mum'</instance>
</config>

The interesting thing is whilst investigating the source code to see how PixLib creates the class instance I found a handy little utility function tucked away in the HashCodeFactory class (com.bourre.core.HashCodeFactory).

This class has a static method just for building instances, it looks like this :

public static function buildInstance(sPackage:String, aArgs:Array)

Exactly what I need.

I can just specify the class name in my config file

<config>
  <stage>
    <classname type="string">com.relivethefuture.ui.Stage</classname>
  </stage>
</config>

and when the time comes I just call the buildInstance method (note the parameters are supplied as an array)

stage = HashCodeFactory.buildInstance(Config.getInstance().stage.classname,[mc.mainStage]);

Magic.

Now, for those of you who are going to be creating classes from package names remember one vital thing. You *MUST* reference the class at some point in your code for the compiler to include it into the swf, otherwise you wont be able to create an instance. This is slightly unfortunate, but its easy enough to deal with, just add

static var myClass:com.mydomain.MyClass

to your main class (or in a seperate class that is just used as a placeholder to ensure the classes are imported) and you’re good to go.

Technorati Tags: , , , ,

Bookmark on del.icio.us

Ant task for Haxe compiler

For those of us who use Ant to automate builds and other tedious tasks i’ve put together a task so you can now run the Haxe compiler from Ant.

It supports all of the relevant command line arguments, but if it doesnt do something you think would be useful please get in touch and i’ll see what I can do.

You can grab a zip here :

antHaxe.zip

Inside is everything you need, source, jar, samples and documentation.

Technorati Tags: , , , , , , ,

Bookmark on del.icio.us

MTASC and ANT : error running mtasc compiler

If you are trying to run MTASC from ant and are seeing the message ‘error running mtasc compiler’ this means that ant cant find the mtasc executable. There are 2 ways to remedy this.
The first is to add the folder with MTASC in to your system path. The second is to use the ‘mtasc’ attribute of Simon Wackers mtasc ant task to point it at mtasc.

If your mtasc executable is in D:\dev\tools\ then you will need to define the task something like this :

<mtasc mtasc=”D:\Dev\Tools\mtasc.exe” …./>

I’ve written a more in depth guide to using ASDT and MTASC with ant over at osflash.org

Technorati Tags: , ,

Bookmark on del.icio.us

last.fm recent tracks in flash

If you’ve been wondering when the code was going to return, well, here it is. Its a little app to show you your recently played tracks from Last.fm. Last.fm is a great site if you like music, and who doesnt like music? So whats it all about?

As they say ‘You get your own online music profile that you can fill up with the music you like. This information is used to create a personal radio station and to find users who are similar to you’

One really nice feature is that a lot of the data is freely available from the audioscrobbler.net site in a variety of formats. This has prompted some people to create handy little scripts that generate sigs which show what you have just been listening to. Most of them have been made using php and GD to generate a graphic, or just plain ol html. So i thought this would be a perfect opportunity to do the same in Flash, but using only open source tools and libraries (You dont need the flash IDE installed to build it..)

You can download a zip of the code from here.

To build it you will need swfmill and MTASC, and to make your life easier i’ve included an ant build file.
The build file requires the as2 ant tasks created by Simon Wacker which you can grab from here.

Also included in the zip is a lovely font created by the nice people at orgdot and a little php script that will cache the response from last.fm so you dont hammer their servers (they ask that you dont make more than 1 request a second..)

For now the implementation is very basic, im sure theres plenty of improvements and extras that can (and should) be added. Drop me a comment if you do something interesting with it.

and if you want to see it in action you can go here

Technorati Tags: , , , , , ,

Bookmark on del.icio.us