A Simple & Quick Preloader

August 22, 2008 – 3:30 pm

Often in my work then I have to chuck something online quickly for clients or whoever to be able to see, and it’s usually nice to have a preloader. It doesn’t exactly take long to create one from scratch, but as I’ve been having to do a lot of it recently, I decided to write a generic AS3 preloader. The difference with this one is that the SWF doesn’t need to be touched - all the necessary parameters are dealt with by the FlashVars. Using this, it takes literally a few minutes to add the preloader.

You can have a loading bar and/or percentage tracker, and you can format the colours, sizes and position of them. The full code is in my code wiki. It’s not much of a wiki, really, as I don’t let anybody else edit it, but it’s just a simple way of making it all readily accessible. All the details about usage and parameters etc are in the wiki.

As always, take anything you like, and don’t worry about copyright, but a link back would be nice if you feel so inclined.

Time conversion using the proxy class

August 1, 2008 – 12:18 pm

I’m quite a fan of neat, sparse code, especially if efficiency isn’t so important. Following on from my date stuff earlier, I had to have a few conversion utils, including time conversions. Everyone’s been through the boredom of creating the methods for millisecondsToSeconds, secondsToMinutes, minutesToHours and so on, and then making composite functions to jump from seconds to hours or whatever, but it’s a pretty dull process.

Using a simple implementation of the proxy class, the following takes whatever combination you like of unitToUnit from milliseconds, seconds, minutes, hours, days and weeks, and automatically works it out. So while, due to the nature of extending proxies, it’s by no means efficient, it’s definitely elegant, and could be easily adapted for similarly incremental units, eg bytes & bits.

package com.utils {

 /**
  * @author chris@acleveraddress.com
  */

 import flash.utils.Proxy;
 import flash.utils.flash_proxy;

 dynamic public class TimeUtil extends Proxy {

  private var units : Array = [
   ["weeks",7],
   ["days",24],
   ["hours",60],
   ["minutes",60],
   ["seconds",1000],
   ["milliseconds"]
  ];

  public function TimeUtil() {
  }

  override flash_proxy function callProperty(name : *, …args : *) : * {
   var arr : Array = String(name).toLowerCase().split(”to”);
   var from : uint = indexOfMulti(units, arr[0]);
   var to : uint = indexOfMulti(units, arr[1]);
   var i : Number = args[0];

   while (from != to) {
    if (to > from) {
     i *= units[from++][1];
    } else {
     i /= units[--from][1];
    }
   }

   return i;
  }

  private function indexOfMulti(arr : Array, str : String) : int {
   for each (var a : Array in arr) {
    if (a[0] == str) return arr.indexOf(a);
   }
   throw(’Function badly named’);
  }
 }
}

Usage:

var tu : TimeUtil = new TimeUtil();
trace(tu.millisecondsToWeeks(604800000));

PHP Date in AS3

July 28, 2008 – 10:37 am

I had to do some date formatting for a project recently, and realised I missed the PHP Date function, so I recreated it in Actionscript 3. While I was doing it I noticed it’s been done a few times before, but there tend to be quite a few of the options missed out, or mistakes made, especially when it comes to the ISO stuff. The only function I couldn’t replicate properly was the Timezone identifier (e) as AS simply doesn’t have access to that information, so it could only be faked.

The example shows lovely slashes being used in the string but in the actual code you’ll have to escape (double) them or use a dollar symbol. Usage: DateUtils.date(’c', new Date(2008, 01, 01)); The date defaults to the (client side) date if it’s null.

Source here.