AS3 loading external swf on button click - actionscript-3

In AS3 (Flash), I want an external swf-file to load when I click a specific menu button. I also want the swf to disappear when I click another button, so that another swf can load. This code shows my swf, but it just "loops". And also it covers my menu (on the top of the page) so I can't click anywhere. What could I change in the code to fix this?
var loader_mc : Loader = new Loader();
addChild(loader_mc);
bilder_btn.addEventListener(MouseEvent.CLICK, buttonClick)
function buttonClick(e:MouseEvent):void
{
try{
loader_mc.unloadAndStop();
} catch(e:Error) {
}
var urlRequest : URLRequest = new URLRequest("flickr.swf");
loader_mc.load(urlRequest);
}

Change loader_mc's X and Y so that the loaded SWF does not cover your menu.
You could do better with reinitializing that loader_mc to load another SWF, that is, create a new Loader(), removeChild() the old loader, addChild() the new one, then load.

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.

External SWF loads, but does not display

So this is similar to a problem I had a few days ago, but not quite the same. If I try to load an external SWF, it loads all right, but I can't see anything! There are buttons in this external SWF, and I can click the buttons and they work, so the SWF is there and functional, I just can't see any of it (the screen is completely white). Also if I click the button, IT appears (with the active frame of its animation) but nothing else does. The audio also plays on its cue. Everything seems to work except that it's invisible.
This is the relevant code:
private var myLoader:Loader = new Loader();
private var url:URLRequest = new URLRequest("../lib/Introduction.swf");
private var introdummy:Sprite;
(different method)
introdummy = new Sprite();
addChild(introdummy);
myLoader.load(url);
myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, introLoaded);
function introLoaded(event:Event):void
{
introdummy.addChild(myLoader.content);
}
OK, you all are going to laugh your butts off when you see what was wrong...
My main SWF has a white background. My external SWF has a black background, and all the text on it was white. When I imported the SWF in, the background showed the main SWF's background. So all the text showed as white on a white background, so it was invisible. :/
add your swf file like this:
introdummy.addChild(myLoader);

Scene remain on background after navigate to new file

I cant stop the existing SWF after load another SWF. New SWF is run on top of the old SWF. I would like to know the simplest way to link all this up.
Scene 1
File 1: MainMenu.SWF (Consist of 2 Picture File)
- Subject1.JPG (Click and Load Subject1MainMenu.SWF)
- Subject2.JPG (Click and Load Subject2MainMenu.SWF)
Scene 2
File 2: Subject1MainMenu.SWF
Scene 3
File 3: Subject2MainMenu.SWF
Below is my code,
import flash.events.*;
var Subject1 = new Loader();
a.addEventListener(MouseEvent.CLICK,onMouseClick);
function onMouseClick (event:MouseEvent)
{
subject1.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaderComplete);
Subject1.load(new URLRequest("Subject1MainMenu.SWF"));
}
function onLoaderComplete(Event)
{
var mainmenu = subject1;
addChild(mainmenu);
}
There is no one size fits all to all this.
You can hide your main menu swf while you load and show child swf
you can remove your main menu completely while you show your child swf and later in some other flow, you reload your main menu again
you can also use module manager to manage the swf loading and unloading

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.

AS3 ScrollPane dispatch event to content

i've loaded external swf into ScrollPane and i need to dispatch click event to this external swf. is it possible? ScrollPane.content.dispatchEvent(new MouseEvent(MouseEvent.CLICK,true)); doesn't work. this is obvious cuz ScrollPane.content is an DisplayObject and it have not CLICK event...
I can't use MovieClip as container for external swf cuz external swf is a documents converted to swfs using openoffice and it doesn't want to load inside MovieClip but perfectly loads inside ScrollPane and react on mouse clicks,but i need to simulate mouse click on it.
so you're saying that the following won't work or you haven't tried it?
var exSWF:MovieClip = MovieClip( ScrollPane.content );
or
var exSWF:Sprite = Sprite( ScrollPane.content );
Not sure to understand what you mean when you say that your external SWF won't load into a MovieClip.
Do you know what version of Actionscript was used for the external SWF, you can check that in debug mode by looking at the properties of the ScrollPane.content?