Copy link to clipboard
Copied
Hello I was hoping someone may be able to help me with a problem I have been having and unloading external .swfs, Ill try to explain this as best I can.
I have two .swf files, both of which are connected to an external .as file. One is the main, the other is the child being loaded into the main .swf file. The user clicks a button in the main .swf firing off a function contained in the .as file which loads the child .swf.
Now I have a boolean variable in the child .swf, that gets set to true at a certain part of the movie, this variable is then sent to the .as file, and caught firing off a function via ENTER_FRAME to unload the movie(unloadAndStop()). The problem I think I am having is the sprite container(which is being created in the .as file), is not being recognized as a child, thus will not unload. However if I create a CLICK MouseEvent, and aim it towards the sprite the unloadAndStop() function works fine, but would like to avoid this if possible. This is what I have in the .as file:
(endFind is the boolean value being set in the child .swf, and the loadSWF() is being called by a button in the main .swf)
import flash.events.Event;
var url:String = "objectSearch.swf";
var _Req:URLRequest = new URLRequest(url);
var _swfLoader:Loader = new Loader();
var _swfContent:Sprite = new Sprite();
var endFind:Boolean;
function loadSWF():void {
_swfLoader.load(_Req);
_swfContent.addChild(_swfLoader);
addChild(_swfContent);
setupListeners(_swfLoader.contentLoaderInfo);
}
function setupListeners(dispatcher:IEventDispatcher):void {
dispatcher.addEventListener(Event.COMPLETE, addSWF);
}
function addSWF(event:Event):void {
event.target.removeEventListener(Event.COMPLETE, addSWF);
_swfContent = event.target.content;
}
function unloadSWF():void {
_swfLoader.unloadAndStop();
trace("unloadSWF: ",_swfLoader.unloadAndStop());
//!!!!!!removeChild is throwing ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.//!!!!!!
//removeChild(_swfContent);
//_swfContent = null;
}
//Adding a mouseEvent to the sprite seems to work ok
_swfContent.addEventListener(MouseEvent.CLICK,endCheck);
function endCheck(e:MouseEvent){
trace("click");
unloadSWF();
}
//This does not because of the unloadAndStop() method.
//addEventListener(Event.ENTER_FRAME,endCheck);
/*function endCheck(e:Event){
if(endFind==true){
unloadSWF();
}
}*/
if you need any more clarification please let me know. I greatly appreciate everybodys help and feedback.
A better way to manage children is to just listen for them to tell you something and then take an action. What that means is essentially having the child just dispatch an event and have the parent listening for it so that it can take action. So to do that you need to assign a listener to the object after it has loaded....
function addSWF(event:Event):void {
event.target.removeEventListener(Event.COMPLETE, addSWF);
_swfContent = event.target.content;
MovieClip(_swfContent).addEventListener(
...Copy link to clipboard
Copied
Is the intention of this scheme to have the child unloaded when the child is finished showing what it needs to? If so, there are better ways to accomplish this.
The problem with using the ENTER_FRAME may have to do with the child being trmoved but the ENTER_FRAME not stopping (that's its nature) and continuing to try to remove an object that's no longer a child.
Copy link to clipboard
Copied
yeah that's basically what I would like to do, would you care to elaborate, or steer me in the right direction on some of the methods that have worked for you? Thanks Ned
Copy link to clipboard
Copied
A better way to manage children is to just listen for them to tell you something and then take an action. What that means is essentially having the child just dispatch an event and have the parent listening for it so that it can take action. So to do that you need to assign a listener to the object after it has loaded....
function addSWF(event:Event):void {
event.target.removeEventListener(Event.COMPLETE, addSWF);
_swfContent = event.target.content;
MovieClip(_swfContent).addEventListener("unloadMe", unloadSWF)
}
and adjust the unloading function to be the event handler....
function unloadSWF(evt:Event):void {
...etc
}
and in the child swf just have it dispatch the event your listener is waiting for when it needs to be unloaded....
dispatchEvent(new Event("unloadMe"));
Copy link to clipboard
Copied
Way more logical! big help as always thank you for helping me understand.
Copy link to clipboard
Copied
You're welcome.
One major benefit of this approach is that the child file remains independent from the parent should you ever need to run/test it by itself. The child is not going to have an error trying to interact with a parent file that is not around - it just makes its announcement and if no one is listening...meh.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now