Lets say you have swf which (for the sake of our example) simply has 1 line of code:
Of course the file may contain pretty much anything, functions, graphics etc like any other swf. Small file allows just to list code better here.PHP Code:var testing="1234567890";
The swf is then exported under name "loadedswf.swf". Now instead of loading it directly, we will convert it into byteArray object to be used in our larger project.
This code loads file "loadedswf.swf" and traces its bytes. You now need to copy/paste the data from trace window and save it. I have attached file ba.txt containing the traced data.PHP Code:bytes = new ByteArray ();
var ldr:URLLoader = new URLLoader();
ldr.dataFormat = URLLoaderDataFormat.BINARY;
var req:URLRequest = new URLRequest("loadedswf.swf");
ldr.addEventListener(Event.COMPLETE, completeHandler);
ldr.load(req);
function completeHandler(e:Event):void {
bytes=ldr.data;
var s:String="";
for (var j : int = 0; j < bytes.length; j++){
var n:String=bytes.readUnsignedByte().toString(16);
if(n.length<2){
n="0"+n;
}
s+=n;
}
trace(s);
}
Now you need to load byteArray from text file and convert it into usable swf file again in your main movie:
This code first loads txt file, once its loaded the data is converted into byteArra object and Loader object is used to load swf from byteArray. The example finally traces value of variable "testing" from loaded swf ("1234567890").PHP Code:var ldr1:URLLoader = new URLLoader();
var req:URLRequest = new URLRequest("ba.txt");
ldr1.addEventListener(Event.COMPLETE, completeHandler1);
ldr1.load(req);
function completeHandler1(e:Event):void {
var bA =new ByteArray ();
var data1:Array=ldr1.data.split("");
var data2:Array=[];
for (var i : int = 0; i < data1.length; i +=2){
data2.push("0x"+data1 [i]+data1[i+1]);
}
for (var j : int = 0; j < data2.length; j ++){
bA[j] = data2[j];
}
ldr = new Loader ();
ldr.contentLoaderInfo.addEventListener (Event.COMPLETE, completeHandler);
ldr.loadBytes (bA);
}
function completeHandler(e:Event):void {
trace(ldr.content.testing);
}
Instead of loading byteArra from external file, you could also include it straight into main file:
Here the variable "loadData" holds exactly same data as saved in text file in previous example. Because the string is so long, FK wont display it properly so copy/paste the data from attached file into code example. The Loader object and loadBytes method is used same way.PHP Code:var loadData:String="(please copy the data from txt file)";
bytes = new ByteArray ();
var data1:Array=loadData.split("");
var data2:Array=[];
for (var i : int = 0; i < data1.length; i +=2){
data2.push("0x"+data1 [i]+data1[i+1]);
}
for (var j : int = 0; j < data2.length; j ++){
bytes[j] = data2[j];
}
ldr = new Loader ();
ldr.contentLoaderInfo.addEventListener (Event.COMPLETE, completeHandler);
ldr.loadBytes (bytes);
function completeHandler(e:Event):void {
trace(ldr.content.testing);
}
For anyone decompiling the main swf or looking at the loaded text file, the content of loaded.swf remains only visible as bunch of unhelpful bytes.
Sorry for not using proper classes and FCS3 here, examples are made with F9Alpha, you can simply copy/paste the code into Flash and test the movie.




Reply With Quote