December 2006 Archives

ActionScript 3.0 Tip: Date Constructor

| 1 Comment | No TrackBacks
If you're doing a lot of work with dates, you might be able to make your life easier by taking full advantage of the Date constructor.

The documentation for the Date constructor lists the values that you're allowed to use when constructing a new Date instance. For the values of month and date, the valid range of values is limited to a small subset of positive integers. The month value can be 0-11 (0 = Jan, ..., 11 = Dec), and the date value can be 1-31, depending on the number of days in the month.

What the documentation doesn't tell you, however, is that you can pass pretty much any number for month and day, and still get a valid date back. This is really useful when trying to figure out some specific date values...

The following code block shows some different usages of the Date constructor, and the resulting Date that was created:

// This creates a date with a month value of 1 before January
// in 2006.  The resulting date is Dec. 1st, 2005.
var dt:Date = new Date( 2006, -1, 1 );
trace( dt ); // Thu Dec 1 00:00:00 GMT-0500 2005
				
// This tries to create January 0th, 2006, which is the last
// day of the prior month.  In this case, we get a date
// of Dec. 31, 2005.
dt = new Date( 2006, 0, 0 );
trace( dt ); // Sat Dec 31 00:00:00 GMT-0500 2005
				
// March 0th doesn't exist, so this will actually create a date
// that is the day before the 1st of March.  In this case, the date
// is the last day of February in 2008.  You can see by the output
// that this gives 29, indicating 2008 is a leap year.
dt = new Date( 2008, 2, 0 );
trace( dt ); // Fri Feb 29 00:00:00 GMT-0500 2008
				
// Create a date that is "a week ago from today"
dt = new Date();
dt.date = dt.date - 7;
trace( dt ); // Fri Dec 22 09:54:20 GMT-0500 2006

Using this concept, you can easily create a function to return the number days in any given month. To get the number of days in Feb. 2008 we simply asked for the "0th" day of Mar. 2008.

You can apply this same technique for time values as well. For example:

// The -1 for the hour gives us an hour before 0:00 on Jan 1st 2006, which is
// 11 pm on Dec 31 2005.
var dt:Date = new Date( 2006, 0, 1, -1 );
trace( dt ); // Sat Dec 31 23:00:00 GMT-0500 2005

This really isn't any new information and has been covered in the past in various other places. However, with the amount of new people coming into Flex 2 and ActionScript 3, I thought it was worth mentioning for those that might not be aware, especially because the documentation doesn't mention this at all.

I was talking to a friend this morning, and this technique made his day (pun intended!).

Happy Friday!

NeoSwiff adds Flash Player 9 support

| No Comments | No TrackBacks

NeoSwiff, the C# to .swf compiler, has added beta support for Flash Player 9. NeoSwiff applications now run up to 35 times faster.

The guys at GlobFX have been doing a great job with NeoSwiff from my view on the sidelines. I'm only a casual user at best, but have been impressed with the quality of their compiler. The exact same C# codebase can target Flash Players 7-8, or Flash Player 9 without any modifications. This means that you can offer the same NeoSwiff application to people with older versions of the player, but also offer a high speed supercharged version to those who've already upgraded to Flash Player 9.

They've even managed to keep their inline assembly code working, for the hardcore nerds out there. A sample snippet (for calling the native Security.loadPolicyFile while targeting both AVM1 and AVM2):

static unsafe void loadPolicyFile( string url )
{
  #if __VM1__
  __asm
  {
    push url
    push 1
    push "System"
    getv
    push "security"
    getm
    push "loadPolicyFile"
    callm
    pop
    }
  #else
  __asm
  {
    ldlex "flash.system.Security"
    ldarg url
    callp "loadPolicyFile", 1
    pop
  }
  #endif 
}

I'll still be using Flex 2 and ActionScript 3 for my daily work, but credit goes out to GlobFX for their continued work on a great product for C# developers.

Tags:

Animating Color Transitions in Flex 2

| 4 Comments | No TrackBacks

A thread surfaced on FlexCoders where someone was trying to animate colors using the AnimateProperty tag, but wasn't having any luck. I've done this type of thing before, and thought I'd share my solution with the Flex community.

Three years ago I wrote an entry about scripting color transitions in ActionScript 1. Using the same basic technique, I've updated the code to ActionScript 3.0 and made a new effect available in Flex 2 to make the process simple, namely AnimateColor.

Usage is as follows:

<ds:AnimateColor xmlns:ds="com.darronschall.effects.*"
		id="fadeColor" 
		target="{someTarget}"
		property="backgroundColor" isStyle="true" 
		fromValue="0xFF0000" 
		toValue="0x00FF00" 
		duration="4000" />

The AnimateColor tag behaves the same as the AnimateProperty tag. The idea is that you pass in a desired end color value, and optional start color value (that defaults to the current color), and a duration. The tag, then, calculates all of the intermediate colors and applies them to the target property. The result is a smooth color transition, rather than the flicker of colors that happens if you use AnimateProperty to animate color transitions.

I've uploaded a demo of AnimateColor in action, and made it View-Source enabled. Right click in the .swf after it loads to view the source and download the code in .zip file. Everything is released under the MIT License.

Enjoy!

Tags:

Batch Convert PDF files to FlashPaper 2

| No Comments | No TrackBacks

If you're looking to convert a large amount of PDFs into FlashPaper files, have I got the script for you...

I was in a situation where I needed to convert thousands of existing PDFs into FlashPaper documents so that they could be loaded into a Flex 2 application (see my previous entry on How to load FlashPaper Documents in Flex 2). A separate business process created new PDF documents daily and placed them in a particular directory. This directory needed to be monitored, and all changes (old files being updated / new files being added) needed to be reflected in FlashPaper to keep everything in sync.

This type of repetitive, automated task is a perfect reason to know a scripting language on the side. As far as scripting languages go, Python probably would've been a great fit here. But, to be honest, I've never had a need to learn the ins and outs of Python. Believe it or not, everything that I've ever needed to do scripting-wise I've done successfully with ANT.

Now, when you think about ANT, you typically think about it for automating build processes since that's where its roots are and because it does that job quite well. But, really, when you explore the multitude of available tasks you can see just how powerful of a general purpose scripting tool it is. Because of my familiarity with ANT, it was pretty easy to create a "build" script to perform the automated conversion process.

Below is my ANT script for converting an entire directory of PDF files into FlashPaper files:



About this Archive

This page is an archive of entries from December 2006 listed from newest to oldest.

November 2006 is the previous archive.

January 2007 is the next archive.

Find recent content on the main index or look in the archives to find all content.

Archives

OpenID accepted here Learn more about OpenID
Powered by Movable Type 5.02