Recently in ActionScript Category

AS3 JSON library now a little less strict

| No Comments | No TrackBacks
I've just checked in revision 83 of as3corelib that adds a strict flag to the AS3 JSON library.  In the past the library would generate an error if the JSON string was deemed invalid according to the syntax rules.  Now, based on the strict flag setting, the parser does a better job at allowing "valid" input even if the input doesn't match the JSON spec.

I've actually had this code completed since MAX 2008.  I wrote it on my flight from San Fransisco to Harrisburg, but haven't had the time to get it ready for check-in until now.

Here is the commit message:

  • Added throwing error if entire string cannot be consumed (when there are remaining characters left in the input stream)
  • Added "strict" flag to decoding process.  Default is true for backwards compatibility.  When false, relaxes input format rules.  Fixes #34, Fixes #41, Fixes #71:
    • Allows for hex specification for numbers, ex: { "myNum": 0xFFCC33 }
    • Allows for NaN to be parsed as a number, ex: { "myNum": NaN }
    • Allows for trailing commas in arrays, ex: [1,2,]
    • Allows for trailing commas in objects, ex: { "p1":true, "p2":false, }
    • Does not throw error if there were characters left in the input string
  • Added tests for strict flag
  • Added tests to better exercise comments
  • Updated tests to use proper assert types
  • Some minor cosmetic code cleanup
Usage is exactly the same as before, with the additional option of specifying the strict flag as part of the JSON.decode( string, strict ) call.  Example:

// Set strict mode to false to allow for trailing commas to not throw an error
var o:Array = JSON.decode( "[0,1,]", false ) as Array;
assertEquals( 0, o[0] );
assertEquals( 1, o[1] );

// Same usage as before, strict mode will throw a runtime error if the string is not valid
var a:Array = JSON.decode( "[0,1,]" ) as Array;

I still need to address #35 to allow for unquoted keys in objects to be parsed in non-strict mode.  This is a bit more complicated of a change because it changes how the tokenizer recognizes what is in the input stream.  It's coming though!

Note that a new .swc file was not yet posted.  If you want to experiment with these changes, you'll have to pull the latest from source.  I'll work with Mike to get the download .swc updated soon.

New Chart Annotation added to FlexLib

| No Comments | No TrackBacks

I've added a new chart annotation, HorizontalAxisDataSelector, to the FlexLib project.

The HorizontalAxisDataSelector is a relatively simple chart annotation that allows a user to select a value along the horizontal axis of the chart. When the uses mouses down on the chart, the selector dispatches a change event and draws a vertical line at the nearest data point along the x-axis.

The selector contains a dataValues property that is an array containing the y value of each chart series at the given x-axis location. You can use these values to present a clear view to the user of all of the y-values for a given x-value rather than forcing them to rely on mousing over data tip points.

You can view the example here, along with the example source code.

Integrating the chart annotation basically just involves adding the following to your chart:

<mx:annotationElements>
	<flexlib:HorizontalAxisDataSelector id="dataSelector"
		change="updateDetails();"
		selectorColor="0x000000" />
</mx:annotationElements>

This is something I wrote a really long time ago, probably on the order of 6 months or so (6 months is like an eon or two in internet-time). It's nothing special by any means, and is still rough around the edges. I had wanted to re-visit it and make some improvements, but just haven't found the time to work on it. As such, I figured it was best to just publish it since it's actually proven itself useful a few times, rather than hold on to it waiting until I have the time to get around to updating it.

In its current form, you can only use this annotation on horizontal axes that are numeric (or date-driven), and that are in ascending order. Kind of a bummer, I know. If you need to use this on a category axis, feel free to modify the source and contribute it back into FlexLib once you get it working!

Note: Right now this is only available in SVN. Doug and I are still sorting out when the best time is to create new packaged FlexLib .swc releases.

On a related note, this annotation isn't nearly as cool as some of the annotations Ely has come up with. His Chart Sampler is amazing. My meager annotation barely scratches the surface of what is possible in the charting framework. To this day I'm still amazed at just how flexible and extensible the charting framework is...

Announcing as3cards Google Code Project

| 4 Comments | No TrackBacks

I'm pleased to announce that I've got the as3cards Google Code Project up and running. The as3cards project provides a skinnable ActionScript 3.0 card engine for creating playing card games, with an example implementation of Klondike Solitaire.

You can view the example implementation of Klondike here.

This is essentially a continuation of my over-a-year-and-a-half old ActionScript 3.0 Klondike game, that was showcased on labs back in the early days when we still had Flash Player 8.5.

Originally I had included this in IFBIN. When IFBIN disbanded I wasn't sure what I wanted to do with the code. I spent a lot of time writing it and wanted to try to get something out of all the time that I put into it, so I didn't want to just blindly release it as open source.

Time passed, and I become involved in a lot of other work as my consulting business really picked up. The Klondike code was left to gather dust on my hard drive. After mostly forgetting about it, I've been contacted by a few people recently that put it in the front of my mind again. I figured that since it's been so long and I haven't done anything with the code yet, I might as well just put it out there as open source and let others run with it.

So, I went through the (very old) code base and updated it to work with the latest build of Flash Player. I've committed the source code into the as3cards Google Code Repository, and released it under the MIT license.

Also, I've built a simple Flex 2 shell that loads the ActionScript-only game and plunks it on the stage. You can use as3cards in both ActionScript-only (plain ol' Flash) projects, or you can inject the card engine into a Flex application.

This project was somewhat hard to part with because of my time invested in it, and the potential market of selling it to people that want Flash poker sites... but in the end, I think this is the right decision and I hope that you find it useful. If you'd like to help contribute (with maybe a FreeCell implementation, or a new skin, or anything), let me know. Donations (and/or ad clicks) are welcome, of course, but not expected.

Enjoy!

Over the past few weeks, we've been silently moving the Open Source ActionScript 3.0 Libraries from Adobe Labs to Google Code. I'm happy to announce that we put the finishing touches on everything last night, and we're now open for business.

As Mike Chambers mentioned, the reason for the move is because Google Code offers a better infrastructure for the projects. We now have a mailing list, wiki, and issue tracker for each of the individual libraries. More importantly, other developers will find it easier to contribute. For example, Owen van Dijk recently added Uploading support into the Flickr API.

The projects now on Google Code are:

I'm very excited about this move. I hope it can breath new life into the libraries as more people get involved and more transparency is introduced. If you are interested in contributing to any of the projects (either writing code, doing testing, writing documentation on the wikis), sign up on the appropriate project mailing list and offer to help. Also, feel free to log any bugs or issues you may run into on the issue trackers (but search before you post, so we can avoid duplicates!).

Thanks to Mike, Mike, and Charles for helping to make this move a success!

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!

Compile ActionScript in ActionScript

| 1 Comment | No TrackBacks

If you haven't seen the Tamarin news by now, what rock do you live under? Did you catch the part about the new ActionScript compiler?

I don't want to rehash what's already been said. JD does a great job at the above link to send you to various places for background information, impact, etc. Instead, I want to point out something that could easily be missed in the announcement: Adobe has released a "self hosting" open source ECMAScript compiler.

As Tinic mentions, the AVM2 code does not include a compiler. Adobe has a Java compiler instead (which you can download free via the Flex 2 SDK) that was built to help support Flex Builder 2. The separate compiler keeps the AVM2 code size (and therefore, the Flash Player size) smaller, but it also means that "eval" doesn't work. The eval function takes an arbitrary string representing code, and executes it, such as eval( "x = 10; alert( x );" );

To combat this, enter the ECMAScript compiler, written in ECMAScript (ActionScript 3). Take a look at the code in this directory, and in particular the parser itself. It's an interesting piece of work, but is only partially complete.

It's exciting to see this in development. As this code matures, we should have available to us a runtime compiler that can interpret ActionScript 3 code on the fly. I'm not sure that I'd use it much in my daily Flex work (I haven't had the need thus far anyway), but I appreciate the effort behind this and am looking forward to seeing it in action.

Tags: , ,

Multiple Inheritance in ActionScript 3

| 11 Comments | No TrackBacks

Did you know ActionScript 3 supports multiple inheritance? Here's how...

Multiple inheritance is actually possible with AS3, despite popular belief that it's not supported. It's not really multiple inheritance in the true sense of the word going by the strict definition... but there's a way to get the job done in AS3 that behaves almost like the real thing.

What I'm about to show you is not something I would advocate as best practice. Nor is it something that should be (ab)used simply because its available. I'm not going to get into the debate about Composition vs. Inheritance and how Multiple Inheritance fits into the picture. If you're here, it's probably because you want to know how to use this technique. I assume you also know then the various repercussions (and if not, at least have the ability to google them). As they say, "with great power comes great responsibility".

Various disclaimers aside, here we go...

FC64 4 teh WIN!

| 2 Comments | No TrackBacks

Since the Flashfoward Film Festival is over, it's time to let loose the FC64 demo that netteed Claus and I a win for technical merit. Enjoy!

FC64 Demo

FC64 Demo Source Code

Fore more information about the demo, see my post here, and Claus' post here.

Sadly, neither Claus nor I could make it to Austin this year to pick up the award in person. I wish I could've been there to see the audience's reaction to the demo, and to accept the award live.

Since it was a joint project Claus will be the keeper of the orange rubber arrow, but I'm hoping to at least get visitation rights!

I'm pleased to announce FVNC, the rebirth of my FlashVNC project. The project is released as open source under the terms of the GPL.

I've updated the code to compile with the release version of Flex 2. Previously, the application was only functional in beta versions and stopped working when Flash Player 9 was officially released. I've also started to do some refactoring, but decided it was better to get the code out there as-is than keep it locked up while I tweak it. I've been beyond busy lately, and I don't see myself finishing this round of refactoring any time soon, so better to release now than wait a few weeks…

That said, keep in mind that FVNC isn't complete yet. There are a lot of items on my TODO list that need to get done before I would consider using this in a production application. The software is somewhere between alpha/beta quality right now, not entirely feature complete, rough around the edges, in need of some performance tuning, and in general still a work in progress.

If you're interested in contributing, please get in touch. Also, check out the mailing list if you're into that sort of thing. Everything you need to know about the project can be found (or will be found in the future) on the OSFlash FVNC page at: http://osflash.org/fvnc

Enjoy!

Why FlashVNC isn't in IFBIN

| 1 Comment | No TrackBacks

Big news today -- All of the IFBIN examples are now available as open source software! All of the examples, that is, except FlashVNC. Why?

The answer is multi-part, but the short of it is that FlashVNC has been a "dead" project of mine for longer than I care to admit. Bad Darron, I know. I don't want the code to be included with IFBIN because, well, it's busted, broken, kaput, faulty, defective, out of order, malfunctioning, etc. The last time FlashVNC was updated was to support the Beta 2 drop of FlexBuilder on labs. It may have been updated for Beta 3, but to be honest I don't really remember.

Needless to say, if you download the source and try to compile it, it's very broken and won't work in Flash Player 9 and won't build with the official release of Flex 2. I don't want people to struggle through that experience, so I thought it was best to take a different direction.

So, what's the plan then?

Well, I very much want to see FlashVNC released as open source software. In fact, I've contacted Aral about setting up a project on OSFlash, which will be the new home of FlashVNC.

I fully support IFBIN being open source and letting the community learn from the examples we've created. However, I wanted FlashVNC to live alongside my other open source projects, and I look forward to using OSFlash's Trac and Subversion support to foster a community around the project.

The plan is to update FlashVNC in two waves. The first will update the outdated codebase to support the official release of Flex so that there is finally a working binary (.swf) again. The second update involves some refactoring that I've been meaning to do but haven't had the time to devote to it. Once these are complete, I'll do an intial commit into the OSFlash Subversion repository.

In the next few weeks you can expect to see FlashVNC reborn, this time better than ever. Its fate as open source software is guaranteed, I just need to find some time to breath life back into it. Please bear with me though, my wife and I just moved, so most of my free time has been spent unpacking and getting settled in the new place.

My ultimate goal is to make FlashVNC the default VNC client for TightVNC, replacing the Java version with a Flash one. A little lofty maybe, but definitely something worth striving for...

[By the way, my ActionScript 3 Klondike implementation is not available in IFBIN anymore either. I'll be giving Klondike an overhaul as well, and will be releasing the source code once its been updated for the official release of Flex 2.]

Flex.org - The Directory for Flex

Archives