As3 Preloader (Blank until 100%) - actionscript-3

I have an FLA file with two frames. On the first frame I have nothing but a textfield and some code to do the preloading. After it is done preloading it does gotoAndStop(2)
On frame 1 I have:
stop();
stage.scaleMode = StageScaleMode.SHOW_ALL;
//Import the required assets
import flash.display.*;
//Stop the playhead while loading occurs
this.stop();
//Create a listener to call the loading function as the movie loads;
this.loaderInfo.addEventListener(ProgressEvent.PROGRESS, PL_LOADING);
this.loaderInfo.addEventListener(Event.COMPLETE, PL_FINISH);
function PL_LOADING(event:ProgressEvent):void
{
var pcent:Number = event.bytesLoaded / event.bytesTotal * 100;
//Display the % loaded in textfield
txt.text = int(pcent) + "%";
//If the movie is fully loaded, kick to the next frame on the main timeline
}
function PL_FINISH(event:Event):void
{
removeChild(txt);
gotoAndStop(2);
}
On frame 2 I have nothing except:
var game:Game = new Game();
addChild(game);
In my publisher settings I have export to frame 2.
My problem is that the preloader won't display until 100%. Does anyone know why?
P.S. For testing I put a large image file on the stage in frame 2 and the result is the same.

This normally happens if you haven't deselected "Export in frame 1" in each of the library symbols that you are exporting for ActionScript.
You'll need to make sure that you create reference to these objects (so that ActionScript can access them) by placing them onto the stage in frame 2 (out of sight).
What's happening is that all of the symbols are being loaded before the preloader itself has loaded.
If this isn't the issue, then please provide some code so I can better assess your issue.

Have you tried putting something static on frame one? Just because there is a preloader, that doesn't mean that your swf will be displaying at all...
I know that I had one swf once which simply took a minute to actually get the Flex preloader to even show up because of network latency. It could be that your swf isn't displaying until 90% of it has already loaded.

I´m dealing with similar problems, and there is something i found out: my antivirus contains a kind of "browser protection" feature, which sort of checks all files in advance before it allows the browser to actually display them. If this antivirus feature has not been installed on the system, the preloader works beautifully. I´ve checked some other web-sites with flash content, and they also behave "wrong" under these circumstances. Now, some people seem to have found a way to stop the antivirus from messing around, but i don´t know how they do it... not yet...

Related

as3 Air - AddChild works but is slooooooooooow

I am creating an app where when a button is pressed a very large picture is added to the stage (it is larger than the screen but can be dragged around by the user)
When the button is pressed the picture (well, movieClip) does come up and it able to be dragged fine, buttons inside it work.
The problem though is that there is a pause of about 6 seconds between the button press and the image appearing. I am using one .fla file for publishing and compiling (let's just call it Main.fla for now), and another one to hold all the graphics. The graphics are then added with this embed command:
[Embed (source = "assets/graphic.swf", symbol = "Content")]
private var Content:Class;
private var _content:*;
I have these lines where all the variables are declared (in between the class definition and the constructor function) I was under the impression that embedding it like this was equivalent to loading it at compile time. Is this true? What could be causing this lag when the button is pressed?
If I can't cut out the lag, another idea I had was to make some spinning circle or something to tell the user, "hey, don't worry. It's loading!"
If the slowness is at addChild you can add the asset to the stage much earlier and set it's visiblility to false, then when the button is clicked set it back to true. Obviously this is a small hack but might be sufficient for what you are doing.
var asset:MovieClip;
private function init():void
{
asset = new Content();
assset.visible = false;
addChild(asset);
button.addEventListener(MouseEvent.CLICK, onMouseClick);
}
private function onMouseClick(e:MouseEvent):void
{
asset.visible = true;
}
Embedding your SWF is probably not what is causing the delay.. or rather it would not likely be better if you imported the SWF into your FLA. Is this on a device? Chances are you would either have to devise a different way of loading your asset, or be satisfied with a loading animation.
If the main K size is coming from a large image, you could consider loading it in segments, starting with the part that is initially visible.

AS3 preloading external swf won't reach 100%

I have a flash interface file and I'm trying to load external swfs with a preloader. My problem is that the preloader never reaches 100% before trying to play the swf. The preloader will display, count up to a percentage and disappear. Then the swf just hangs for a few seconds on the first frame(some times over 10 seconds!) while it finishes loading. It does eventually start playing. The percentage the loader reaches is completely random. It might reach 40% one time and only 7% the next.
I'm loading the external swfs into an empty movieClip on stage called "visloader".
Here is my loader code:
loader= new SWFLoader("nameOfSlide.swf", {container:visloader, autoPlay:false, onProgress:progressHandler, onComplete:completeHandler});
loader.load();
And here are my progress and complete functions:
//--------------PROGRESS HANDLER----------------------------------------------------------------
function progressHandler(e:Event):void{
//set alpha of loading animation to 1 so it is visible
progClip.alpha = 1;
progClip.loadPct.text = "" + Math.floor((loader.bytesLoaded/loader.bytesTotal)*100) + "%";
}
//--------------COMPLETE HANDLER-----------------------------------------------------------------
function completeHandler(e:Event):void{
//hide the preloader animation
progClip.alpha = 0;
//myClip was created in the variables section above to hold the loaded swf
//must use 'rawContent' to get control over the swf (play/pause, etc...)
myClip = e.target.rawContent;
myClip.play();
}
"progClip" is a movieClip on stage with a spinning circle and a text field called "loadPct".
The external swfs being loaded are pretty large. Some over 5 MB. Not sure if that matters. I've used the exact same preloader for a different client with slightly smaller files and had no issues. I'm at a complete loss.
I don't think this has been asked before. I searched the forums and came up with nothing. Hopefully someone here will know what I'm talking about.
Thanks in advance!
If SWFLoader is firing the complete event too early, and you're can't see why by looking at the class itself, you could try checking - either in the progressHandler, with EnterFrame or a Timer - for when loader.bytesTotal == loader.bytesLoaded. When it does, remove relevant listeners and go from there.
Bit of a hack I know but if SWFLoader isn't working properly it's that, repair the class or get another library. I'm gonna have a look at it myself now, for the meantime hope this helps/works!

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.

Loading SWF in AS3 but flash keeps repeating constructor function, what should I do?

I am importing several external files using the Loader-class, and one of them is an swf-file. When doing so (I had done it successfully before, so did not expect any issues), I ran into all sorts of errors, and finally Flash crashed.
I put down a trace in the constructor function, and it didn't trace just once, but kept on tracing, suggesting that the constructor was stuck on loop. I guess the loading of too many of the same swf is what causes flash to eventually crash.
Here is my code (the swf im loading is now a simple test-file which contains an image and no code):
private var slides:Loader = new Loader();
public function DocumentClass()
{
trace(1)
slides.load(new URLRequest("Resources/Slides.swf"));
slides.contentLoaderInfo.addEventListener(Event.COMPLETE, SlidesComplete);
}
public function SlidesComplete(evt:Event):void
{
slides.contentLoaderInfo.removeEventListener(Event.COMPLETE, SlidesComplete);
addChild(slides);
}
This traces "11111111111..." and everything dies in the end.
HELP!
Try putting a stop() action at the top of the swf you load in (either in actionscript, or on the timeline). It's possible that the swf is being loaded in and is running a play and running on loop in the mean time (hence your code running over and over).
I would do a progress watch until the swf is fully loaded, then jump to your display frame:
Create a section for loading (your choice if you want to use a preloader)
Create another section (set of keyframes) for loaded content. I use keyframes because it's easy, but you could also wait to instantiate classes until loading is complete.
Below is a snippet I occasional build from:
// stop the playhead from moving ahead
stop(); // you can also use gotoAndStop("loading"); if you want
function loaderProgressHandler(event:Event):void {
// switch the framehead to main which will show your content
if(event.bytesLoaded >= event.bytesTotal) {
event.target.removeEventListener(Event.PROGRESS, this.loaderProgressHandler);
this.gotoAndStop("main");
}
}
this.loaderInfo.addEventListener(Event.PROGRESS, this.loaderProgressHandler);
Hope that helps!
I was just stuck on this same problem.
In my case it turned out that the problem was down to the swf having the same document class name as the swf that was loading it.
eg. Main.as was loading another swf that also had its document class called Main.as - Changing this to anything else solved the infinite loop.

AS3: Getting information from an externally loaded SWF

So I have an SWF that I've made and I need to appened some instructions to the beginning of the project. I figured the easiest way to do this was to make the animation in a separate SWF then import it to the start of the first one. My problem is that I can't find a reliable way to tell when the first SWF is finished playing. I've googled the heck out of this but I can't seem to find anything that works. For some bizarre reason, no matter what I do the program seems to think that the external SWF only has 2 frames, if I put an ENTER_FRAME listener and trace externalSWF.currentFrame I get "1, 1, 2, 2, 2, 2, 2, 2..." My code looks something like this.
var ldr = new Loader();
ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, loaded);
ldr.load(new URLRequest("Instructions.swf"));
function loaded(e:Event){
trace("Loaded");
var extSwf = ldr.content as MovieClip;
addChild(extSwf);
trace(extSwf.totalFrames);//Returns 2
}
Has anyone else had similar problems with external SWFs?
Also, for the record, the external SWF plays properly when I add it as a child. The problem is removing it from the stage when it's done playing. It's interactive so I can't just do a frame count.
Edit: So I tried doing a getQualifiedClassName() call on extSwf and I got "Instructions_fla::MainTimeline_Preloader_" which could explain the frame discrepency. How can I have access to the actual timeline?
To SWFs can communicate with each other via SharedObject.
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/SharedObject.html
So I found the solution to the problem. Like I said in the edit, I was getting "Instructions_fla::MainTimeline_Preloader_" as the QualifiedClassName so I googled that and found someone saying that Flash CS5 exports your SWFs with a preloader by default so that was what was being attached to extSwf and not the MovieClip itself. The way to fix it is to go into File>Actionscript Settings>Library Path, and change Default linkage to "Merged Into Code". This appears to only happen in CS5. I found the solution here: http://blog.flash-core.com/?p=142