var file:FileReference=new FileReference();
and then
trace (file.data);
works fine
after that I'm trying to embed received data into the scene but with no success
var ExtSWF:MovieClip;
ExtSWF = file.data.readObject() as MovieClip;
trace(ExtSWF);
returns null
but if I load it as remote file, with Loader - it works fine
var ldr:Loader = new Loader();
ldr.load(new URLRequest("ext.swf"));
......
ExtSWF = MovieClip(ldr.contentLoaderInfo.content);
Is it possible to just upload swf file and embed it into the scene, or Loader class is the only possibility to archieve this goal?
The Loader class is used to load SWF files or image (JPG, PNG, or GIF)
files.
But "load" really meaning "decode format" for display. So pass your file.data bytes through the Loader using Loader.loadbytes for decoding to a valid MovieClip object.
Try
//var ExtSWF : MovieClip = new MovieClip;
//ExtSWF = file.data.readObject() as MovieClip;
var ldr : Loader = new Loader(); //# declare outside of any functions (make as public var)
ldr.loadBytes(file.data); //#use Loader to auto-decode bytes of SWF
ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, on_swfLoaded );
And also have a handler function for the decoding completion...
function on_swfLoaded (evt:Event) : void
{
var ExtSWF : MovieClip = new MovieClip;
ldr.contentLoaderInfo.removeEventListener(Event.COMPLETE, on_swfLoaded );
ExtSWF = ldr.content as MovieClip;
trace(ExtSWF);
ExtSWF.x =50; ExtSWF.y = 50; addChild(ExtSWF);
}
Related
Actually i have a SWF that is loaded by another SWF.
This SWF that is loaded, itself load another SWF.
This may be weird, but it's in a context of an online game that let you develop SWF as plugins.
So, we have SWFA(the video game) ---> SWFB ----> SWFC
When i try to load the external SWFC, the COMPLETE event fire, but i am only able to add it to the stage by adding directly the Loader, because the loader doesn't have any content property.
public function load(){
loader = new Loader();
loader.contentLoaderInfo.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onIoError);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.NETWORK_ERROR, onIoError);
loader.contentLoaderInfo.addEventListener(AsyncErrorEvent.ASYNC_ERROR, onAsyncError);
applicationContext = new LoaderContext(true, ApplicationDomain.currentDomain);
loader.load(getRequest(),applicationContext);
}
private function getRequest():URLRequest {
Security.allowDomain("https://mock.com");
Security.loadPolicyFile("https://mock.com/crossdomain.xml");
var req:URLRequest = new URLRequest("https://mock.com/mock.swf");
req.method = URLRequestMethod.POST;
req.data = new URLVariables("name=kk");
var encoder:Base64Encoder = new Base64Encoder();
encoder.encode("user:pass");
var credsHeader:URLRequestHeader = new
URLRequestHeader("Authorization", "Basic " + encoder.toString());
req.requestHeaders.push(credsHeader);
return req;
}
Here is the onComplete function
public function onComplete (e:Event):void{;
this.addChild(e.target.content); // this is null
this.addChild(e.currentTarget.content); // null too
}
So, when i run my SWF from my local computer, everything goes well, e.target.content contain my external SWF.
But when i run the same within the SWF Loader inside the online game, i can't find any content property. Plus, childAllowParent property of LoaderInfo is always false.
Does someone have an idea of what's going on?
Thanks
I have an application in AIR, which is loading swf files and makes previews of them. When those swfs loads other content (eg. jpg) then i have an error, that url not found - i am convinced that is about jpg file. This problem shows only when i load swf by filereference using loadBytes(). When i load swf by Loader using load() - problem doesn`t exist. How can i fix this issue? Sorry for my english. I attach code fragment with loading swfs.
private static function loadFile(loadType:String, swfNum:Number = 0):void {
lc = new LoaderContext();
lc.allowCodeImport = true;
lc.allowLoadBytesCodeExecution = true;
currentSwfNum = swfNum;
var fRef:FileReference = DataBase.swfsList[currentSwfNum];
fRef.addEventListener(Event.COMPLETE, onFileReferenceLoad);
fRef.load();
}
private static function onFileReferenceLoad(e:Event):void {
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, ifComplete);
loader.loadBytes(e.target.data, lc);
}
private static function ifComplete(e:Event):void {
var mc:MovieClip = LoaderInfo(e.target).content as MovieClip;
mc.addEventListener("SHOOT", SwfSingleCapture.captureSingleShoot);
}
DataBase.swfList - it is an array of FileReference objects, which are selected by FileReferenceList and browse().
i have a flash file, containing 2 scene.
first scene have continuous looping of random selected external flv file, using flvplayback.
second scene contain code for 2 external swf file, play after another. continuously.
however, it seem dont work for the second scene. it just wont get into the function.
here, i also paste down actionscript code for scene 2.
stop();
var myLoader:Loader = new Loader();
var url:URLRequest = new URLRequest("loader.swf");
myLoader.load(url);
addChild(myLoader);
stage.loaderInfo.addEventListener(Event.COMPLETE,playnextswf);
function playnextswf(event:Event):void{
trace("in function");
removeChild(myLoader);
var myLoader2:Loader = new Loader();
var url2:URLRequest = new URLRequest("intro.swf");
myLoader.load(url2);
addChild(myLoader2);
}
EDIT
i do some editing, by change this line of code:
.addEventListener(Event.COMPLETE,playnextswf);
to
myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,playnextswf);
it seem to jump into function, but with error.
in function
in function
ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
at flash.display::DisplayObjectContainer/removeChild()
at praperkahwinan_fla::MainTimeline/playnextswf()
I think you are looking more for:
var myLoader:Loader = new Loader();
var url:URLRequest = new URLRequest("loader.swf");
myLoader.contentLoaderInfo.addEventListener( Event.COMPLETE, playnextswf );
myLoader.load(url);
addChild(myLoader);
You attached the listener to the stage loaderInfo which works a little differently, from adobe:
The LoaderInfo class provides information about a loaded SWF file or a
loaded image file (JPEG, GIF, or PNG). LoaderInfo objects are
available for any display object. The information provided includes
load progress, the URLs of the loader and loaded content, the number
of bytes total for the media, and the nominal height and width of the
media.
As we discussed in chat you are looking for something more like this:
var myLoader:Loader= new Loader();
myLoader.contentLoaderInfo.addEventListener( Event.COMPLETE, onLoaderComplete );
myLoader.load( new URLRequest( "loader.swf" ) );
var myLoaderSwf:MovieClip;
function onLoaderComplete( e:Event ):void {
trace( "first swf loaded" );
myLoaderSwf = e.target.content as MovieClip;
addChild(myLoaderSwf);
myLoaderSwf.addFrameScript( insert frame number, loadNextSwf );
myLoaderSwf.gotoAndPlay(2);
}
function loadNextSwf():void {
trace( "removing currently loaded swf -- loading next swf" );
removeChild( myLoaderSwf );
myLoader = new Loader();
myLoader.contentLoaderInfo.addEventListener( Event.COMPLETE, onIntroComplete );
myLoader.load( new URLRequest( "intro.swf" ) ); //change this to intro.swf
}
function onIntroComplete( e:Event ):void {
trace( "intro swf loaded" );
myLoaderSwf = e.target.content as MovieClip;
addChild(myLoaderSwf);
}
My LoaderInfo will return the swf itself rather than the image given via the FileReference and Loader. I had problems debugging it, as LoaderInfo.content returns [Object Main] (My document class). After investigation, I discovered LoaderInfo.content is a swf file, according to contentType.
The problem is, the file reference for the image is correct (It is an image, and it is not the swf).
My code:
private function onAction(e:MouseEvent){
if(e.currentTarget.name == 0){
myFileReference = new FileReference();
myFileReference.browse(getTypes());
myFileReference.addEventListener(Event.SELECT, loadedImage);
myFileReference.addEventListener(Event.COMPLETE, loadImage15);
}
}
private function loadedImage(e:Event){
var imgHolder:ImageHolder = Main.imageHolder;
while(imgHolder.numChildren > 0){
imgHolder.removeChild(imgHolder.getChildAt(0));
}
myFileReference.load();
}
private function loadImage15(e:Event){
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadImg2);
loader.loadBytes(myFileReference.data);
trace(myFileReference.type); // .JPG
}
private function loadImg2(e:Event){
var lInfo:LoaderInfo = (e.target as LoaderInfo); //e.target is indeed LoaderInfo
lInfo.removeEventListener(Event.COMPLETE, loadImg2);
trace(loaderInfo.contentType); //application/x-shockwave-flash
var newSprite:MovieClip = loaderInfo.content as MovieClip;
Main.imageHolde.addChild(newSprite); //Error as you can't addChild Main to Main
}
private function getTypes():Array {
return [new FileFilter("Images","*.jpg;*.jpeg;*.gif;*.png")];
}
EDIT
I originally had a very complicated answer - which was wrong...
You simply have an error in your program:
// here you reference the Loader's contentLoaderInfo
var lInfo:LoaderInfo = (e.target as LoaderInfo);
lInfo.removeEventListener(Event.COMPLETE, loadImg2);
// but from here on out, you reference your parent class' "loaderInfo" property!
trace(loaderInfo.contentType);
var newSprite:MovieClip = loaderInfo.content as MovieClip; // <- this is your Main class!
Main.imageHolde.addChild(newSprite); //Error as you can't addChild Main to Main
Change loaderInfo to lInfo, and you should be fine.
I ask about this issue before :
loading image by Loader.loadBytes(byteArray)
Noone knows :]
If You loadBytes of image with Loader , You will recive :
Loader.content is MovieClip
Loader.content.getChildAt(0) is Bitmap
[ Embed('Assests/ui/Main.swf', symbol='backgroundImage')]
private var background01:Class;
private function getBitmapData(className:String):BitmapData
{
var mc:MovieClip = (new background01) as MovieClip;
if (mc == null) throw new Error('mc is null');
var myBitmapData:BitmapData = new BitmapData(mc.width , mc.height);
myBitmapData.draw(mc)
return myBitmapData;
}
Hi there. I'm having a problem working this out. I want to return bitmapData from a symbol that is embedded in a flash swf file. background01 is the class name exported to as3 in the flash swf file. As you can see from above I'm finding the swf with no problem. and I'm pullin the data back from the swf file with no prob. I can type addChild(new background01) and the image will show up. But when I've tried to cast to a bitmap , bitmapasset and movieclip. It always returns null. I want to get access to the bitmapdata of this image/movieclip. It should be returning a vertex circle drawn in flash and created to be a movieclip symbol. So what I would expect to be returned would be a MovieClip.
Has anyone ever done this, and if so, could you provided some sample code..
Thanks
LinX
Try:
var mc:MovieClip = (new background01()) as MovieClip;
instead of
var mc:MovieClip = (new background01) as MovieClip;