External Swf issues - actionscript-3

I am working with external swfs. A main class loads in many external swfs immediately. They are shown later on after user input.
Just one of the external swfs is an issue. I wrote this, it is a different interaction then the rest and the audio played is separate from the main class unlike the other swfs.
So my question is, in the external swf code, is there an event listener I can use to know when this external swf is being displayed in The main class? I don't want to change the main class code since it is setup for a template that this one swf can't and won't match. Any suggestions would be great Thanks!

I guess it depends on what you are doing with this child swf, it is a pity that you are not in a position to change your main class, I mean this is in charge of the children right.
I think you could possibly do the following in your child swf:
this.addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler, false, 0, true);
function addedToStageHandler(event:Event):void
{
var child:Movieclip = MovieClip(event.currentTarget);
// do something with child
}

Related

loading new external swf to stage and definitely removing previous one

I have three movies:
1) main movie
2) external #1
3) external #2
When the main movie is opened it loads into new container "MovieClip" one of the external swf (depends on variable). in the main movie I have also a button that switch the external SWF to another. Each external have some sounds located on their keyframes. When I want to change the external with the "button" in main movie it loaded new external correctly but the old one is still below the new one and the old one still plays their sounds. How can I delete the previous SWF when I loading new one. remove or clear or mute the sounds of this external? Below is the code I'm creating all.
var ladujFilm:String = "external#x.swf"; // the name comes with amfPHP so it's dynamic
var movieLoad:URLRequest = new URLRequest(ladujFilm);
movieLoader.load(movieLoad);
movieLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, movieLoaded_fn);
function movieLoaded_fn(event:Event):void {
addChildAt(movieLoader, 1);
movieLoader.x = 0;
movieLoader.y = 0;
}
the Loader class has the unloadAndStop method, you need to use it on the "old" loader.
docs
example
best regards

Root Reference After Loading External swf

I am loading a game swf into my swf. However the preloader of the game (which is from a library) has a problem with the root when it tries to reference it. I assume this is because the root is now my main swf rather than the preloader swf. Does anyone know of a way to load a swf and allow it to keep its own root? It is much easier for me to change my loading code than the game preloader code.
Thank you!
I think it's easier if you reference the explicitly reference the main.swf root with a name and not "root" and then use "this" for the preloader.
For example, say if you document class is called Main (like Main.as).
Then in your Preloader MovieClip code...if you are writing a class for the preloader, do something like:
public function Preloader() {
addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true);
}
private function init(e:Event):void {
var main = (root as Main);
// do stuff with "main here"
main.addEventListner(...); // reference the main.swf
this.addEventListener(...); // reference the preloader
}
Sorry but this was a problem that I never really solved and had to do a workaround for by altering the code in the swf I was loading. I could find no other way to get it to reference the correct data.
Thank you all for you help though :)

Loading and using SWF files

I'm new to AS3, and am trying to understand how externally loaded SWFs work in AS3. Since Flash 4/5, it was common to have one main SWF file in a Flash web project, and then load other SWF files into it, often for various "sections" of a website or web project. In the main file, we'd have masks animating the container movieclip(in which external sections/SWF files were loaded) and have animations and transitions play as the section finished loading and the loaded content was displayed.
In AS3, I've used the Loader class to load and display the external file, my main problem is in communicating with the loaded content, call it's functions, or call root functions from it.
In AS2, we could use someMovieClip.loadMovie("ExternalContent.swf") and the ExternalContent file would load inside someMovieClip. You could access functions on the "External.swf" main timeline using someMovieClip.function();. And inside the "ExternalContent.swf", we could use _root.function() to access functions in the main file ExternalContent was being loaded into. Doing this in AS3 seems bizarre and neurotic, and I feel like I'm missing something fairly basic here.
//Loading in ExternalContent.swf into the sprite
//ExternalContent has a movieclip called "boxes" on it's main timeline
//boxes has a boxesPrompt() function in it's timeline.
var sprite:Sprite = new Sprite();
addChild(sprite);
var loader:Loader = new Loader();
loader.load(new URLRequest("ExternalContent.swf"));
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoaded);
function onLoaded(event:Event):void
{
sprite.addChild(event.target.content);
sprite.boxes.boxesPrompt();
//Flash gives the following compiler error at the above
//Scene 1, Layer 'Layer 1', Frame 1, Line 21 1119: Access of possibly undefined property boxes through a reference with static type flash.display:Sprite.
//But when I comment out sprite.boxes.boxesPrompt() and use this, it works:
event.target.content.boxes.boxesPrompt()
}
The boxesPrompt() function inside the "ExternalContent.swf" just traces it's parent, grand-parent, and great grand-parent - trace(this.parent.parent.parent);. And when I call that function inside the onLoaded event-handler using "event.target.content.boxes.boxesPrompt()", it shows that the Boxes object(which was on the main timeline of External.SWF), has a parent movieclip, a grand-parent sprite, and a great grand-parent object mainTimeline.
I thought re-parenting the loaded content into the sprite would allow me to access the loaded content as easily as loadMovie() used to be - accessing loaded content like it was present directly inside the clip it was loaded in. But that doesn't work at all.
So to rephrase, my question is:
How do I communicate from the main "loader" SWF file, with the content that's loaded in. I don't want to communicate using event.target.content.{etc} because then I'd only be able to address the loaded content inside the Loader's event.complete event handler.
How do I "re-parent" loaded content, so I can place it inside some movieclip/sprite on the main timeline of the loader file, rather than using some really long convoluted way.
How to communicate from inside the loaded content, to the main/loader file. Previously, we'd use _root.functionName() to do stuff like play some animation transitioning from the current externally loaded "section" to another section. How'd I go about doing that.
AS2 & AS3 is vastly different. But you will have to swallow the fact that AS3 has been developed as an improvement over AS2. So any transition you make, is also for the better.
For eg : The _root in AS2 allowed global objects & variables to accessed & changed anywhere, which is a bad practice & leads to non maintainable code in a project.
Having said that, let me address your questions:
If you are able to get access to the loaded content with
event.target.content... you should save it inside a,say class
variable & may access it later elsewhere in the class.
You must understand that you will be able to access the content only
after loading it, so have to wait for it to complete anyway &
event.complete handler is probably your best bet.
I doubt if you can pick random content from a loaded swf & re-parent it into the current swf.As explained you might not have a long convoluted way.
Accessing the parent could be done in many ways. You can use .parent or actually call a function from the parent swf passing its reference to the child.
var sprite;
addChild(sprite);
var loader:Loader = new Loader();
loader.load(new URLRequest("ExternalContent.swf"));
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoaded);
function onLoaded(event:Event):void
{
sprite = event.target.content;
//This should work
sprite.boxes.boxesPrompt();
}
See this example for more info.

Get Child of loader from externally loaded swf

READY? Here it is:
var TheLoader:Object = parentObj.TheExit as Object;
This line gets me to the swf I loaded and I can now change the alpha, move it etc...
BUT HOW HOW HOW... do I get one of its children and control get that same control?
Example Code:
TheLoader.TheChild.alpha = .3; // Does not work!
5 days on this issue is WAY TOO LONG! Here is the 3rd post with the same issue but more detail. as3 externally loaded swf from network to control externally loaded swf from network
I just made this shorter to get attention to the ONE LINE I NEED!!!
THANKS!
If you look at the documentation for Loader, you'll see that it has a content property. This is how you access your loaded content. So, for example, if TheLoader is a Loader, and if TheChild was an instance on the timeline of the loaded SWF, you could do:
var child : Sprite = MovieClip(TheLoader.content).TheChild;
This child is not available until the content has been actually loaded, so be sure to listen to for the COMPLETE event on the Loader's contentLoaderInfo before accessing the content:
TheLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaded);
function onLoaded(event : Event):void
{
trace(MovieClip(loader.content).theChild);
}
Also note that you may run into security issues if the loader and loadee SWFs are not on the same domain. In this case, you will have to call Security.allowDomain in the SWFs to allow the cross-scripting.

When a SWF is loaded into another. Can the main SWF read the loaded one to retrieve its contents and modify them?

Give this scenario:
I have a cool graphic in Illustrator or Flash.
The graphic represents a figure, with various elements inside, shapes, lines, gradients, etc.
I export it to a swf file and I can view my nice graphic if I open it.
I have a (pure) as3 application, which loads swfs.
Then...
Can I manipulate the contents of the loaded swf. For example: Moving its contents, changing some elements inside, duplicating them. Deforming them with the transform matrix and things like that?
Can I, at least, read the contents and replicate them (the graphic data) inside the main application.
As far as I've been researching, I can only import the swf and use it as a whole display object, without any children, and I cannot modify it.
I want to, somehow, use the graphic information of the external swf to allow the main application deform it or use it to make new versions of the graphic.
No, sadly you can't retrieve or change compiled Graphics elements in detail, you can only use them as DisplayObjects and transform them through their public properties (scale, rotation, transform, etc...).
There is way of programmaticaly retrieve a single path coordinates by setting it as the "guide" of an object in the Flash IDE and within an onEnterFrame retrieve the object's position, but as you can imagine, its quite dirty.
You can replicate the clips by either setting them as classes within the loaded SWF (In Flash IDE: library>symbol>properties>export for AS) or using BitmapData to draw them as you please from the main movie.
Another solution would be to use a more open format, like SVG or FXG, but it can get quite complicated...
You can access loaded SWF's methods and properties if both are AVM2 movies.
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);
private function onLoadComplete(e:Event):void
{
var content:Object = LoaderInfo(e.target).content;
//assuming there is a public someMethod() defined in
//the document class of the loaded SWF, you can call it as:
content.someMethod();
//you can access any property you wish
content.someObject.graphics.beginFill(0x00ff00);
content.someObject.graphics.drawCircle(10, 10, 10);
content.someObject.graphics.endFill();
}
SWF content can be readed by new function added to Flash Player 11.6 and AIR 3.6:
flash.display.Grapics.readGraphicsData()
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/Graphics.html#readGraphicsData%28%29
Similar problem:
How to read data from shape/graphics object