DateChooser / DateField from Flex to Flash
After searching in vain a whole day for a dateChooser/dateField/calendar component for Flash CS3/CS4 that would work with Actionscript 3 (something like the one there was for AS2) i decided to follow a different route.
Since Flex Builder has exactly what i was looking for i decided to import it. The operation proved to be simpler than i initially thought. So only after half an hour here's what happened.
In Flex all you have to do is add a dateField (or dateChooser, or any other Flex component for that matter) on the stage, give it an "id" and build it. Then take the SWF file to load it into flash.
The Flex code:
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="90" height="20" backgroundAlpha="0"> <mx:DateField id="dateField" height="20" width="90" showToday="true"/> </mx:Application>
In Flash things are a bit more tricky:
var loadedMC:MovieClip;
var flex:*; // inside the loadedMC
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderCompleteHandler);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, loaderIOErrorHandler);
loader.load(new URLRequest("DateChooser.swf"));
function loaderCompleteHandler(e:Event):void{
loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, loaderCompleteHandler);
loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, loaderIOErrorHandler);
addChild(loader);
loadedMC = loader.content as MovieClip;
loadedMC.addEventListener(Event.ENTER_FRAME, flexLoadedHandler);
}
//tricky part - wait for flex to initialize
function flexLoadedHandler(e:Event):void{
if(loadedMC.application != null){
//stop/cleanup
loadedMC.removeEventListener(Event.ENTER_FRAME, flexLoadedHandler);
flex = loadedMC.application;
flex.dateField.formatString = "DD/MM/YYYY";
flex.dateField.addEventListener(Event.CHANGE, dateChangeHandler);
}
}
function loaderIOErrorHandler(e:IOErrorEvent):void{
trace("dateChooser swf not found");
}
function dateChangeHandler(e:Event):void{
trace(e.target.selectedDate);
}And that's it!!
Of course there is a "small" problem with this approach. The DateChooser.swf file alone is 200KB.
An empty Flex project is 150KB on it's own. And adding the dateField component takes this to 200. So essentially the 150KB is just "crap" to us (Flex Framework, preloader, etc...).
So, next job is to find a way to tell Flex to compile only what we need.
update:
I built the Flex application again, this time setting the Flex Framework Libraries to be Shared Runtime Libraries (SRL) and that took me down to 50 KB on my SWF! And flash works with that file alone.
But i am afraid Flash needs the extra 2 files that contain the Flex Framework (which are cached for me now, since i have opened a Flex app in the past) so i will put those along with the SWF.
August 20th, 2009 - 02:35
Hello i´m having problems this do not work with me, i´ve just copy & paste and it loads but when i click the datefield it gives me an error, this one:
TypeError: Error #1009: Não é possível acessar uma propriedade ou um método de uma referência de objeto nula.
at mx.controls::DateField/displayDropdown()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\controls\DateField.as:2161]
at Function/http://adobe.com/AS3/2006/builtin::apply()
at mx.core::UIComponent/callLaterDispatcher2()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\core\UIComponent.as:8628]
at mx.core::UIComponent/callLaterDispatcher()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\core\UIComponent.as:8568]
HELP,,
Thanks
November 4th, 2009 - 12:06
Hi, this working fine. Thanks for help