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
Related
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);
}
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 some questions with sharing/using/accessing variables/functions between loaded swf files.
my prj consists of main.swf file and 2 swf's which I load on first init of the main.swf.
my questions are:
1.how can I use variables from 1.swf in 2.swf (function is running in 2.swf)
2.how can I call a function from 2.swf in 1.swf
here is the code I'm using to load the swf's:
var playerMc:MovieClip = new MovieClip();
var dbMc:MovieClip = new MovieClip();
var m2Loader:Loader = new Loader();
var mLoader:Loader = new Loader();
startLoad();
function startLoad()
{
//var mLoader:Loader = new Loader();
var mRequest:URLRequest = new URLRequest("./_player/player.swf");
mLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadMc);
mLoader.load(mRequest);
addChild(mLoader);
//var m2Loader:Loader = new Loader();
var m2Request = new URLRequest("./_db/db.swf");
m2Loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadMc2);
m2Loader.load(m2Request);
addChild(m2Loader);
}
function loadMc(event:Event):void
{
if (! event.target)
{
return;
}
playerMc = event.target.content as MovieClip;
mLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, loadMc);
}
function loadMc2(event:Event):void
{
if (! event.target)
{
return;
}
dbMc = event.target.content as MovieClip;
dbMc.x = -400;
m2Loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, loadMc2);
}
You have to stick with application domain.
In most cases you should load another swf in another application domain, but it's not really related to your question.
From loader, you must access to applicationDomain and then getDefinition. From there, you can get classes and use them in your main swf. Yes, you can read static properties.
If you need instances you should access loader#content. It is pointing to a root of loaded SWF. Root of loaded is SWF – is the instance of main class of the loaded swf.
Create a variable with no definition such as
public var MyClass;
as you can see i didnt add
public var MyClass:Class;
then in another function write
this.MyClass = this.mLoader.contentLoaderInfo.applicationDomain.getDefinition("NameOfClass") as Class;
i dont know much about this myself.. im having problems figuring out if you can only access Public static variables or if its possible to access normal public variables and possibly private variables because it is creating a new instance of the same class or however you want to word it..?
also after your write the above code .. when you want to change a varaibles this usually works for me
this.MyClass.RandomVariableName = this.MyClass.RandomVariableName + 1;
something like that..
I have 2 swf files. One is loaded and loading an externally loaded swf into it.
Here is the code in the first loaded swf: #1
var logo:Loader = new Loader();
logo.load(new URLRequest("images/Logo.png"));
logo.contentLoaderInfo.addEventListener(Event.COMPLETE, LoadLogo);
function LoadLogo(e:Event):void
{
addChild(logo);
}
/// The SWF I AM LOADING
var Beau:Loader = new Loader();
Beau.load(new URLRequest("Beau.swf"));
Beau.contentLoaderInfo.addEventListener(Event.COMPLETE, LoadBeau);
function LoadBeau(e:Event)
{
addChild(Beau);
}
NOW HERE IS THE CODE FOR THE BEAU SWF LOADED: #2
import flash.display.MovieClip;
bird.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler);
function fl_MouseClickHandler(event:MouseEvent):void
{
//// I WANT TO BE ABLE TO ADD CODE TO CONTROL MOVIECLIPS
//// AND CHILDREN form this externally loaded swf. How do
//// I do that? Below code does not work.
var logo:MovieClip;
var ControlLogo:MovieClip = MovieClip(logo.content);
ControlLogo.alpha = .3;
}
EDIT
ok your code and tutorials are great. I am having major SANDBOX issues now when I make the clip load from an external URL. I am also coding for an ANDROID using FLASH CS5.5. Its not allowing me to use Security.allowDomain("*");
BOTTOME LINE IS THE NEWLY LOADED SWF can not access the PARENT WITHOUT PERMISSION BUT I DON'T KNOW HOW TO GIVE IT PERMISSION.... using the AIR/ANDROID PLAYER.
use property parent
parent gives loader
parent.parent gives swf#1
So you can write function as given below
function fl_MouseClickHandler(event:MouseEvent):void
{
var swf1:Object = parent.parent;
var logo:Loader = swf1.logo;
var ControlLogo:Bitmap = Bitmap(logo.content); // because logo loads an image
ControlLogo.alpha = .3;
}
make sure that logo is not a local variable, but global and public in swf#1
public var logo:Loader = new Loader();
EDIT:
logo:Loader gets added to SWF#1. Loads image, so content is Bitmap.
beau:Loader gets added to stage. Loads swf (SWF#2), so content is MovieClip.
so now
root is SWF#1
parent of beau is SWF#1
parent of SWF#2 is beau and parent of beau is SWF#1
so for SWF#2, SWF#1 is parent of parent so parent.parent
If you are not creating public variables, you can manage this by using property name.
var logo:Loader = new Loader();
logo.name = "logo";
...
...
var beau:Loader = new Loader();
beau.name = "beau";
...
...
Then anywhere in swf#2
var swf1:Object = parent.parent;
var logo:Loader = Loader(swf1.getChildByName("logo"));
....
....
For accessing the content it is recommended to use type casting as I have shown
var ControlLogo:Bitmap = Bitmap(logo.content); // because logo loads an image
so that you can check as shown below and avoid runtime errors
var ControlLogo:Bitmap = Bitmap(logo.content);
if(ControlLogo){
}
If you want to so something irrespective of content do as shown below
var ControlLogo:Object = logo.content;
if(ControlLogo && ControlLogo.hasOwnProperty("alpha")){
ControlLogo.alpha = 0.4;
}
I have been searching the web and all of the codes I have found are to play external swf files that have a timeline. The file that I am trying to load does not have a timeline. I am using the Flixel framework for this project and the file that I want to play is also made in Flixel(don't have the source file just the swf file).
Most of the code I have is from a cutscene template that I found on the Flixel forum. Here is what I have so far:
package
{
import org.flixel.FlxState;
import org.flixel.FlxG;
import flash.display.MovieClip;
import flash.media.SoundMixer;
import flash.events.Event;
public class SponsorsState extends FlxState
{
//Embed the cutscene swf relative to the root of the Flixel project here
[Embed(source='assets/DirtPileLogo.swf', mimeType='application/octet-stream')] private var SwfClass:Class;
//This is the MovieClip container for your cutscene
private var movie:MovieClip;
//This is the length of the cutscene in frames
private var length:Number;
override public function create():void
{
movie = new SwfClass();
//Set your zoom factor of the FlxGame here (default is 2)
var zoomFactor:int = 2;
movie.scaleX = 1.0/zoomFactor;
movie.scaleY = 1.0 / zoomFactor;
//Add the MovieClip container to the FlxState
addChildAt(movie, 0);
//Set the length of the cutscene here (frames)
length = 100;
//Adds a listener to the cutscene to call next() after each frame.
movie.addEventListener(Event.EXIT_FRAME, next);
}
private function next(e:Event):void
{
//After each frame, length decreases by one
length--;
//Length is 0 at the end of the movie
if (length <= 0)
{
//Removes the listener
movie.removeEventListener(Event.EXIT_FRAME, next);
//Stops all overlaying sounds before state switch
SoundMixer.stopAll();
//Enter the next FlxState to switch to
FlxG.state = new PlayState();
}
}
}
}
When I run this I get this error: Type Coercion failed: cannot convert SponsorsState_SwfClass#fb5161 to flash.display.MovieClip., all I want to do is play the swf file for a set frame count then move onto the next state.
Any ideas on how to do this?
Try to replace the following:
[Embed(source='assets/DirtPileLogo.swf', mimeType='application/octet-stream')]
private var SwfClass:Class;
//This is the MovieClip container for your cutscene
private var movie:MovieClip;
into
//Mark your symbol for export and name it => MyExportedSymbol
[Embed(source='assets/DirtPileLogo.swf', symbol = "MyExportedSymbol")]
private var SwfSymbol:Class;
//Make sure that MyExportedSymbol base class is MovieClip
private var movie:MovieClip = new SwfSymbol;
Basically, you mark your symbol for export, give it a name and use that in the embed code. You will embed that symbol only.
You are incorrectly setting a mimeType on your embed. Remove the mimeType and it should work properly. See the docs on embedding assets for more information.
I believe the solution you are looking for is to use the Loader class.
[Embed (source = "assets/DirtPileLogo.swf", mimeType = "application/octet-stream")]
private var content:Class;
private var loader:Loader;
public function Main():void
{
var data:ByteArray = new content();
loader = new Loader();
addChild( loader );
loader.loadBytes( data, new LoaderContext(false, new ApplicationDomain() ) );
// ... add listener to loader if necessary, etc...
}