Refurbishing antiques with LocalConnection

This post was written by jimrobson on February 18, 2008
Posted Under: Flash, Flex

There’s a room in my house that has a pair of old-fashioned wingback chairs set in front of a large Scandinavian bookshelf unit. I like the way this arrangement looks; the graceful curves of the Queen Anne style chairs contrast nicely with the hard, sleek lines of the bookcase. And it would seem that I’m not alone; lots of people like to juxtapose classic older styles with contemporary ones. In order to do this successfully, of course, the older item often needs a bit of work: hardware replacement, scratches and dings filled in, or perhaps a complete refinishing job. But the result is often more than worth the effort. And this exercise is not only aesthetically pleasing, it helps to minimize waste by putting an old item to use that might otherwise have been destined for the rubbish pile.

We now have a somewhat analogous situation in the Flash development world - especially those of us who have been working with the technology for several years. We all have old Flash assets lying around that are potentially useful, but they don’t have all of the functionality we need, and they can’t be imported into ActionScript 3 projects. So what do we do with them?

One answer is to use the LocalConnection class to enable these antiques to communicate with our current projects. With LocalConnection, any two (or more) SWFs running in the end user’s Flash player can send messages to each other regardless of what version the SWFs are compiled to or which version of ActionScript they use.

Suppose, for example, that you are asked to build an application that has a clickable U.S. map. When the user selects a state, a pie chart displays the relative amounts spent on various types of candy bar in that state. Flex Charting can take care of the pie chart pretty easily, but what about the map? Suppose you have an old interactive map built in Flash 5 laying around, and you want to use that rather than building a new one from scratch. Well, that is precisely what was done here:

ChartMap application screen shot

This application uses a Flash 5 map of the U.S. loaded into a Flex 2.0 application. Just a few lines of LocalConnection code added to the version 5 FLA were sufficient to enable this perfectly useful old asset to communicate with its ActionScript 3 host. Apart from the developer, the only people who could tell the difference between this application and one built completely from scratch are the people who pay the bill - they could see it in the money they save.

As an aside, you’ll also notice that the Flex project included in this application illustrates a few interesting things in addition to the LocalConnection code:

  • The use of a custom renderer component to build a custom legend
  • Using a custom color palette with a Flex chart
  • Using the interpolate effect to animate a pie chart
  • An AS3 helper class to read, manipulate, and generate XML data

You can run the complete application here. When the application opens, right-click on it to view and download the source.

Note: The XML data file used for this application is a work of fiction. Any resemblance to actual sales figures or percentages, living or dead, is entirely coincidental.

Enjoy!

Reader Comments

very nice work. i like the custom legend you built.

regards
valley

#1 
Written By valley on March 11th, 2008 @ 3:28 am

valley - thanks for the kind words; they are much appreciated.

#2 
Written By jimrobson on March 11th, 2008 @ 9:27 am

This rocks.

We’re developing something similar for our dashboard here, aimed at Workforce Development, Hurricane Evac response, etc.:

http://www.dpe.edu/accsdashboard/Home.aspx

#3 
Written By Jeff (no, the other one) on March 21st, 2008 @ 1:12 pm

@Jeff: Very cool - thanks for the link!

#4 
Written By jimrobson on March 23rd, 2008 @ 7:53 am

Thanks for the info! The chartmap looks sharp!

#5 
Written By antique clocks on July 24th, 2008 @ 1:43 am

thanks!

now i am developing a flex aplication, but sometime i need flash to use, like u’r map

but… i’am trying to export all my proyects to PureMVC…

how do you manage the LocalConnection Class using PureMVC? if u have an advice, please reply me!

#6 
Written By JoeX on July 25th, 2008 @ 8:40 am

@JoeX -

First of all, let me apologize for the delay in posting and responding to you. Somehow your comment was waylaid by my spam filter.

Anyway, you raise an interesting question. Here’s what I would do: I would write a Proxy class to represent the functionality of the Flash asset. I would put the LocalConnection code inside the Proxy class, and put the Proxy class in the view/components directory. From the rest of the application, then, your Flash asset looks just like any other Flex component. Only your Proxy class knows the truth. ;-)

If you’re not familiar with Proxy classes, or if you have other specific questions, feel free to use my contact form to contact me.

Thanks for the good question!

- Jim

#7 
Written By jimrobson on August 5th, 2008 @ 9:57 pm

Thanks you Jim for your reply!
i had subscribed to the RSS feed for comments on this post to check if u reply me…. hehehe

proxy clases was my idea first, just like u advice me… but
i found a better solution, even if the flash project is made on PureMVC too!

this method use some like i call “Weak Cast”, ex:

public var aaa:*;

and with this u can access to the flash, as a flex component!
if u have a foo() function in your timeline of your flash, and then you embed this to a canvas in flex, you can call:

canvas.foo();

just that easy!,….

i wanted to contact you in your contact form, but i think that this is a not minor problem, and i hope this help other people like us…

here is an example:
http://seantheflashguy.com/flex/FlexFlashCS3CommExample/FlashCS3ToFlexCommunicationExample.html

and here is it’s source!
http://seantheflashguy.com/flex/FlexFlashCS3CommExample/srcview/index.html

good bye!!!

source blog:
http://seantheflexguy.com/blog/2007/09/27/flash-cs3-and-flex-2-communication-using-actionscript-30/

PD: sorry for my bad english… i am from Chile, bye!

#8 
Written By JoeX on August 6th, 2008 @ 5:27 pm

Can you please tell me how to download the source of the above application ? As u’ve mentioned, right click on the application after it gets open it’s doesn’t allow me to download.

#9 
Written By pradeep on April 21st, 2014 @ 7:48 am