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

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.

Related

How to properly add "Replay" functionality to a self-preloaded Flash movie?

I made a Flash project in FlashDevelop to create an Ad.
The Preloader is setup by making use of the Additional Compiler argument:
-frame=NameOfLabel,NameOfMainClass
My main class is simply called "Main", at the top / default package level.
So frame #1, being the Preloader portion of the SWF, has:
Very few bitmaps, vector-graphics and text (to stay under 50kb);
A YouTube video player in the center (does not count in the filesize limit);
The frame #2 has everything else (the Main class basically embeds all it's dependencies). This includes:
Assets from precompiled SWF (Bitmaps, Symbols, Fonts, XML data);
All classes imported (this is recursive for every classes importing other classes);
Now my big problem is, my client requested the "replay" functionality long after I've completed 99.9% of the project.
I have the project more-or-less broken into different states (Intro, Ready, SlideMenu, etc.), but I'm not sure how I can easily reset the Flash movie back to the very beginning (where it was preloading and showing the YouTube video).
The easy solution would be to simply call an ExternalInterface JavaScript method that would refresh the Flash container, BUT I don't think I have control over what's going on the HTML / JavaScript side.
Is there an easy way to invoke a replay function from AS3?
Would not simply going back to frame 1 do the trick ?
The following seems to do the trick!
private function onReplayClick(e:MouseEvent):void {
var theStage:Stage = this.stage; //Temporarly store the stage.
//Kill any animations happening:
TweenMax.killAll(); //3rd party, may not be applicable for you :P
//Remove ALL containers / child DisplayObjects
SpriteUtils.recursiveRemove(theStage); //custom-made
// (this object is no longer attached to the stage at this point)
//Nullify any Singleton / Static variables:
Main.INST = null;
// Load the 'bytes' of the current SWF in a new Loader, then...
// add it to the stage
var swf:Loader = new Loader();
swf.loadBytes( theStage.loaderInfo.bytes );
theStage.addChild( swf );
}
By doing a deep recursive cleanup of the DisplayObjects, and any static variables (like Singleton instances), it leaves you with a blank stage.
After that, you can instantiate a new Loader that will load the SWF itself via the current LoaderInfo's bytes property.
Add the Loader to the Stage, and you're up and running again!

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!

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);
}

ActionScript Frame metadata not working

I know there exist a lot of posts about the Frame metadata tag in ActionScript,
but I still didn't find an answer to my question.
The problem is, I have specified a Frame metadata tag above my Main class header:
package gameUI {
...
[Frame(factoryClass="gameUI.MyPreloader")]
public class Main extends Sprite {
public function Main()
{
if (stage)
init();
else
addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{ ...
I also have a preloader class in the same package (gameUI) called MyPreloader.as
package gameUI
{
// imports..
public class MyPreloader extends MovieClip
{
// private vars ..
public function MyPreloader()
{
trace('preloader START');
...
In my IDE, i don't get any errors or warnings. It compiles without problems.
But when I run it, it just skips over the Frame meta tag and just runs the code of the Main class. (The trace in the constructor of MyPreloader is never printed out).
I'm thinking maybe it has something to do with some arguments specified in the compiler settings. I've tried some couple of things, aditional compiler arguments but it never worked.
Does anyone know why he ignores the Frame metatag?
I'm really frustrated right now..
I'm using Flash Builder 4.6.
Compiling with Flex SDK 4.6.
thanks.
Your Main Class is a Sprite, so it can't have two frames. That may be part of your problem.
However, it might be that you can use a process that's easier to debug to accomplish the ultimate goal. For example, this is one approach. I personally usually just set my default export frame as 10, put a spinner on frame 1 and issue a play().
The only assets that need to be compiled on Frame 1 are in the spinner. It will then pause while the spinner spins and the assets load (because it has to load everything compiled there before it can proceed). Once all the assets have loaded, it will naturally advance to Frame 10. I will have an instance on the timeline at Frame 10 that has an associated getter and setter, so when the setter triggers, I know that loading is complete.
I only check "Export in Frame 10" for assets that absolutely have to be loaded at startup (for example, sounds or visual objects that are never used on the timeline).
The advantage of this approach is that you can get to the real content earlier (because only assets compiled on your default frame have to load before you can show anything), but the disadvantage is that it makes no sense to show % progress, since you have no idea what percent of the movie is compiled on frame 10 and needs to load before it will start.
So, in summary:
Try making the Base Class for your fla a MovieClip
If that doesn't work, consider changing approach

As3 Preloader (Blank until 100%)

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...