When the game of an external swf is finished, remove the external swf - actionscript-3

I am creating a series of mini games and animations and plan on bringing them all into one flash file. Each of the games need to disappear when the player has finished them. I don't know what to put into the section in the external swfs when the game is done. I currently just have a trace ("finished").
In my main swf you should click start and a game swf appears and when you are finished it, it should disappear.
I have looked up tutorials and they something about accessing variables in the external swfs but I got nowhere with them.

in place of trace("finished");, dispatch something like a complete event:
dispatchEvent(new Event(Event.COMPLETE));
Then listen for that event in the container swf and respond appropriately.
Here is an example for the host (parent) swf:
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,loadDone, false,0,true);
loader.load(new URLRequest("temp2.swf"));
function loadDone(e:Event){
trace("CONTENT LOADED");
addChild(loader.content);
loader.content.addEventListener(Event.COMPLETE,go,false,0,true); //listen on the capture phase (third parameter true) if you're complete event is NOT on the main timeline of your child swf
}
function go(e:Event):void {
trace("Content is all done");
removeChild(loader.content);
loader.unloadAndStop(); //tries to free the app of all memory used by the loaded swf
loader = null;
}
Another way, is in place of trace("finished"), you could have the child swf remove itself (as per a comment on the question). I would consider this a less desirable solution (though more encapsulated) as it lessens re usability and will likely still leave the swf in memory:
stop();
if(this.parent){
this.parent.removeChild(this);
}

Related

Unload SWF File in a Same Button that Load the External File to Minimize Loading Time

I have two SWF files. First one is home.swf and the second one is menu.swf. Each SWF file has a button that load external SWF file.
This is the code inside home.swf file:
stop();
menu_btn.addEventListener(MouseEvent.CLICK, func_menu);
function func_menu(e:MouseEvent):void {
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
loader.load(new URLRequest("menu.swf"));
}
function onComplete(e:Event):void {
var movie:* = LoaderInfo(e.currentTarget).content;
stage.addChild(movie);
}
This is the code inside menu.swf file:
stop();
home_btn.addEventListener(MouseEvent.CLICK, func_home);
function func_home(e:MouseEvent):void {
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
loader.load(new URLRequest("home.swf"));
}
function onComplete(e:Event):void {
var movie:* = LoaderInfo(e.currentTarget).content;
stage.addChild(movie);
}
If I click the buttons numerous times, the SWF files gets longer time to load. I notice that those code doesn't unload files. What code do I need to unload the previous SWF files?
I'm new in actionscript. Please go easy on me.
I understand from the question that it's highly likely that you'll switch between the SWFs often (loading home and unloading menu, and vice versa).
I suggest you create a container SWF, which will load both SWFs, and save references to the loaders / their content on load complete.
Once both are loaded, simply call addChild() and removeChild() to swap them.
In case you want to trigger the swap from within the SWFs, you can dispatch an event when clicking on the buttons created in them, and listen to dispatched events in the container SWF.
That way you can swap them as many times as you'd like in an instant without having to reload them every time, plus you avoid constantly allocating and deallocating memory.

AS3: add event listeners to loaded AS2 SWF

What I am trying to do is - simply load an external SWF into my AS3 code.
I then want to show it on my stage - and be able to catch the 'ROLL_OVER', 'ROLL_OUT' and 'MOUSE_CLICK' events that happen with the SWF, meaning - I want to know when the user hovers over the loaded SWF and when he clicks on it.
If I load an external AS3 SWF - it all works fine, and I can trace the events successfully.
If I load an external AS2 SWF - in some types of AS2 banners I can catch the events, and in some - I can't.
It is important to note that I cannot control the loaded SWFs and I cannot code them in a different manner.
The way I load the external SWFs is like this:
.
var loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoaded);
loader.load(new URLRequest(externalSwfURL));
function onLoaded(evt:Event):void
{
// The reason I don't create the MovieClip like this is because I need to support
// both AS2 and AS3 that will be loaded, and loaded AS2 cannot be casted to 'MovieClip'
//var mc:MovieClip = MovieClip(evt.target.content);
// This method allows me to load both external AS2 and AS3 SWF files
var mc:MovieClip = new MovieClip();
mc.addChild(loader);
// Add the events that I want to track
mc.addEventListener(MouseEvent.ROLL_OVER , onMouseEnterSWF);
mc.addEventListener(MouseEvent.ROLL_OUT , onMouseLeaveSWF);
mc.addEventListener(MouseEvent.CLICK , onMouseClickSWF);
mc.x = 100;
mc.y = 100;
stage.addChild(mc);
}
.
What I have found out is that if the loaded AS2 SWF has a transparent button on top of it (a lot of them have that) - then the mouse events aren't fired back up to my AS3 code ! They are somehow 'swallowed' inside the loaded AS2 SWF and not bubbled up.
If I try to load an AS3 SWF that has a transparent button as the top layer - it works, and still bubbles up the mouse events to my AS3 code.
Can anyone tell me why this happens ?
PS - if I load an AS2 SWF that doesn't have a transparent button as a top layer - than the mouse events ARE bubbled up to my AS3 code.
Here is a link to an AS2 SWF file that has the 'transparent button' that blocks the events from bubbling up to the AS3 code:
link to AS2 SWF
ActionScript 2 runs in a different virtual machine and is not compatible with ActionScript 3. Communication between the two SWFs ist not even not easy, but works only with a local connection. ActionScript 2 also doesn't have an event system so this would be the second part to take care of.
-> it can't work as you expect it to.

How to play two swf files in as3

hai i want play two swf files in my action script project.In this two files one swf file works on the detection face in front of the system.Other swf plays the flv file.when face is detected player must be stooped if not player must be plays the flv file.
I know how to load the swf file but i cant handle the functionality regarding starting and stoping player.
the snippet of code shows how can i load external swf file .and i will explain each line of code in comments
public function videos(view:Sprite)
{
this.box = view;//IT GETS Sprite object from other class because of need to display the player also.
request = new URLRequest("Untitled-1.swf");
currentSWF=new MovieClip();
loader= new Loader();
loader.load(request);
box.addChild(loader);
currentSWF = MovieClip(loader.content);
loader.addEventListener(Event.COMPLETE,loadComplete);
//addChild(loader);
currentSWF.gotoAndPlay(1);//when i put this line of code in comments it plays the external swf also.
}
I hope u understand my doubt .can any one explain how to handle my things .i am new to this action script.please help me
Loaded files automatically play, unless you tell them explicitely not to. You’ll have to listen to the Event.INIT event, and stop the movie there:
loader.AddEventListener(Event.INIT, initLoader);
function initLoader (event:Event)
{
MovieClip(event.currentTarget.content).stop();
}
This will stop the movie before it is attached to the stage, and before it starts playing—so it won’t do that unless you start it again.
Note that you shouldn’t access loader.content in any way before the INIT or COMPLETE events, as it’s very likely that the content isn’t loaded then. As such you should put all your manipulating actions into the COMPLETE event:
box.addChild(loader);
loader.addEventListener(Event.COMPLETE, loadComplete);
function loadComplete (event:Event)
{
// Now it’s safe to access the `content` member:
currentSWF = MovieClip(loader.content);
// Of course this one would play the movie again, so you probably want
// to call that later on a button click or something.
currentSWF.gotoAndPlay(1);
}

Externally loaded MovieClip won't Stop()

I have externally loaded a .swf asset that is an animated character with a timeline. I load the clip as follows:
m_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, LoaderComplete);
m_loader.load(new URLRequest("test.swf"));
private function LoaderComplete(e:Event):void
{
var movie:MovieClip = m_loader.content as MovieClip;
stage.addChild(movie);
movie.gotoAndStop(1);
}
But the animation just keeps on playing. Stop() also does not work. I'm not sure what I am doing wrong here, I feel like it has to do with being an externally loaded .swf file.
If you have library objects that are on the stage but have their own timeline, you'll need to make sure that all of them are stopped. What happens when you publish the movie.swf - does it keep running?

as3 loading screen

How do you create these "loading, please wait" splash screens with a loading bar and percentage for your swf? Does as3 have some built-in methods or should it be designed from scratch?
I mean, how do you detect that your swf file is being loaded into client's browser?
Oh and I have all my content stored in one frame only. pure as3 code.
If all of your code is on the timeline, then the easiest way is to make a second swf that is only used for loading your main swf. If your main swf is called main.swf, the code in your loading swf would be something like this:
//make a loader
var loader:Loader = new Loader()
//listen for loading progress
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgress);
//listen for when the load is finished
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
//load begins...
loader.load(new URLRequest("main.swf"));
function onProgress(event:ProgressEvent):void
{
//calculate how much has been loaded
var percentageLoader:Number = event.bytesLoaded / e.bytesTotal;
//use your percentage number here to drive a loader bar graphic
}
function onComplete(event:Event):void
{
//remove listeners now that loading is done
loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, onProgress);
loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onComplete);
//add loaded swf to the stage
addChild(loader.content);
}
As an aside, externalizing your assets opens up new preloading possibilities. Instead of having everything in your main swf, you can have your main swf load external assets, and preload those assets. Since your main swf would then be small, there would be no need to have a second swf just for loading your main swf.
There's a few different ways to do this.
Either you can have a two step process - create a swf file that simply loads in the main file, reporting progress (by listening to the progress-event on the loader) and then adding the loaded swf as a child.
Or, you can place all your content on frame 2, and stay on frame 1, checking the root.loaderInfo.bytesLoaded compared to root.loaderInfo.totalBytes values, reporting the percentage and then moving on to frame 2 when done.
Personally, I think method 1 is better, since you don't need to sully yourself with frames.
There is also a third way which uses some weird meta-data tags to simulate the second way, but without frames. It's a bit messy, but should be googleable.
Flash has its own component you can use to accomplish this:
http://help.adobe.com/en_US/ActionScript/3.0_UsingComponentsAS3/WS5b3ccc516d4fbf351e63e3d118a9c65b32-7fa4.html
Or you can build your own progress bar from scratch:
http://www.zedia.net/2008/the-right-way-to-do-a-preloader-in-as3/
There are lots of resources on the web that allow you to customize the progress bar- browse around and find one that works for you and your coding style.