using callback functions with SwfLoader
I am working on a Air application where swf files are loaded with SwfLoader and where I want them to be able to write to disk. You can do this with events but I thought I'd look at using callback functions, and I must say I like it, it works pretty nice.
First the Air application. When the swf is loaded we tell the loaded swf wich function to use using the SetCallback function that must exist in the swf.
<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:SWFLoader id="loader" source="LocalSaveTest.swf" complete="initSwf()"></mx:SWFLoader>
<mx:Script>
<![CDATA[
import flash.filesystem.File;
import flash.filesystem.FileStream;
import flash.filesystem.FileMode;
public function initSwf() : void
{
(this.loader.content as MovieClip).SetCallback(AirFunc);
}
public function AirFunc(text:String) : String
{
SaveXmlToFile(text);
return "saved text to disk";
}
public function SaveXmlToFile(xml:String) : void
{
var file:File = File.applicationStorageDirectory.resolvePath("testfile.txt");
var stream:FileStream = new FileStream();
stream.open(file, FileMode.WRITE);
stream.writeUTFBytes(xml);
stream.close();
}
]]>
</mx:Script>
</mx:WindowedApplication>
And the swf file looks like this;
public var Callback:Function;
public function SetCallback(callback:Function) : void
{
this.Callback = callback;
}
case MouseEvent.CLICK:
//call the callback
var result:String = this.Callback(text);
break;
Also read following article
http://www.jadbox.com/2009/02/as3-optional-callback-method-warning/
I hope you liked this post, you can leave comments using your twitter credentials using OAth.
Bart
0 comments
Leave a comment...

