Jump to content
Search Community

loading site "pages" that refer to single document class

berilac test
Moderator Tag

Recommended Posts

Hi,

 

I want to believe I'm not the only one to have had this issue (it is usually the case :)).

 

I had built my site with each "page" as a movie clip on it's own layer on timeline (within a content_mc).

Each page_mc contains its relevant other movie clips.

 

Everything is on frame one. Most actionscript is within a document class and other classes.

 

I was having crazy trobule with splitting the pages into separate swf's, so I thought I would start simple instead.

 

I have created a preloader.swf which loads the "complete" site main.swf

 

The (initial) problem

 

Even using this method, I am running straight away into trouble as the code within my document class (gets loaded with main.swf) refers to mc's that I have built using the flash IDE.

 

However, because all of these are now within another display object, or something, all of my references are now incorrect (I guess).

 

Does this sound correct, and thus I need to go through and alter _all_ of my class code, or am I perhaps missing something beautiful and simple that can make all my problems go away? :)

 

Thank you - please ask questions; I am having trouble getting my head around this (as everything worked beautifully before I separated things out to use with LoaderMax) and thus have not necessarilly explained things that well...

 

Peace

Link to comment
Share on other sites

How are you referencing your MovieClips? You shouldn't need to alter ALL of your code, no. But it's tough to say what you need to change without seeing what you've got. Can you create a really simple example and post the FLA(s) here? No need to post your production files.

 

You're not using "root" references or "stage", right? Keep in mind that those don't exist initially until the DisplayObject is on the stage. So you might need to restructure your code a little bit so that instead of just running a bunch of code right away that relies on stuff being on the stage or root, you instead add an ADDED_TO_STAGE event listener to your document class that then calls a method which has all your instantiation code.

Link to comment
Share on other sites

Thank you. Part of the issue may well be to do with code running before mc's are present on the stage; I will make sure to check through my code.

 

Yeah, sorry for not posting some examples... this is the first flash site I've built, and in part it might be a little convoluted :) I've been learning techniques and best practices throughout...still very much am.

 

If I can think of way to simplify and post an example file, I will. If not, would you be ok to look over my production code if I send to you over pm perhaps?

I want to keep this as last resort, mostly because though my code is commented, it's perhaps not done that well for anyone else other than me in places :)

 

Back to the issue - I have been experimenting and trying to solve problems. I have just noticed that because I am now compiling the "pages" all separately, I am seeing just how reliant they are on my documentClass.

Further, I _just_ discovered that I am therefore compiling the documentClass separately for each page swf...which means any edits to that class means I then have to recompile all pages again...I think I could have done this website a bit better than I have done :) though it's been an excellent learning experience.

 

Would I be correct in saying that I should remove any reliance of each page upon the documentClass, so that they compile as just (badly) displayed content? Then ammend my documentClass to contain ALL of my code (apart from supplementary external classes) - as well as listen for appropriate ADDED_TO_STAGE to be safe?

^ At least this is what I am feeling should be step 1.

 

Then perhaps once that's cleared, I won't need to edit much else within my code :) (at least regarding loading)...

 

[for the record to give you an idea what I am working on, you can check out the current state of this website by going to http://www.kama.is - it should load quick enough on good connection]

 

edit:

I should perhaps point out, at the moment, I am simply seeking to load all pages together (the mcContent) instead of separate pages (that will come after - sensible?)

A further question or two - At the moment I have a separate preloader class (document class for preload.swf) which loads a swf with mcContent in it. Should I place the documentClass (currently the document class for the mcContent swf (and ultimately the "page" mc's) into the preloader document class (well, combine them, I guess)?

This way, all code will be within the preloader...is that good idea?

I'm guessing I would then have to make sure my code checks for the successful loading (adding to stage) of all necessary parts (currently just mcContent, though ultimately all separate page swf's) - before it runs any code...

 

Thanks again :)

Link to comment
Share on other sites

I guess a simpler question might be, should I have a document class only for my preloader? Or should it be ok to load a swf that has it's own document class?

 

(in my case ultimately, load a swf with its own document class, as well as other swf "pages" that have mc's linked to other classes that reference the main document class as a MovieClip - ie. I have scrollbars on several pages that reference the document class in order to add content via xml to mc's on the stage)

 

nb. I am now realising that my problems could be because I am not having completely isolated classes (such as scrollbar, navbutton, etc) -- I have kind of hard-coded links between them and my mc's on stage...

Could this be the heart of the matter?

Link to comment
Share on other sites

I'm not entirely sure I follow your question or how you're setting things up, but I'll offer a few pointers:

 

1) You can have a document class for each swf, sure. That's no problem but I would recommend naming them distinctly to avoid conflicts when they're loaded into the same ApplicationDomain.

 

2) If your goal is to minimize file size (speed up the load), be careful about reusing the same classes in every sub-swf because the same class(es) will get compiled into each swf which is rather redundant. Not to say that's always evil but it can eat up kb. You could subload a swf with a common class. SWFLoader has a getClass() method that might be useful.

 

3) If each sub-swf must reference something in the main/root swf, it'd probably be wise to have the parent swf call a method that you create in the sub-swf's document class and then feed data through that method. Be very careful about hard-coding references to the root because if your subloaded swf happens to not be in the display list at the time you try to reference "root", you'll get a null object reference error. That has nothing to do with LoaderMax or anything - it's just how Flash works for any DisplayObject.

 

Hope that helps (at least a little).

Link to comment
Share on other sites

Hi,

 

Thanks again for the info. Much appreciated as always.

 

Yeah, apologies for the complicated explanation. I'm still getting used to coding/working with flash and actionscript, and likely my code reflects that :).

(for posterity, a current test version of the site can be viewed at kama.is/test.html, to give you at least an idea of what I'm working with)

 

Re your pointers:

 

1) I would ultimately only have one (homepage) swf using my documentClass; I should note that my subpages have some classes linked via objects on the stage, such as my scrollbars - these instantiate "documentClass(root)" as an mc in order to locate and affect appropriate content. I think this is where one problem lies. This breaks the compile of a few of my subpages, as they are obviously trying to reference a clip that does not exist...(solved the problem in my current test, by loading all of my pages together)

Does that make any more sense? :)

 

2) I agree (though file size isn't _major_ issue in this case) re the redundancy of loading classes repeatedly in subpages.

I will definitely look into getClass(); sounds like a common 'class-container' swf would be sensible, as well as good coding practice imho - What is simplest way to compile such a swf? Should it have a doc class that "includes/imports" the shared classes?

 

3) The error you speak of is exactly the problem. It's just I've been having a little trouble figuring it out. Your response here definitely sounds like it could help.

 

I won't ramble on any longer, as I have couple of ideas worth trying first :)

 

Cheers (I'll likely be back :))

Link to comment
Share on other sites

Hi again,

before I get back to things, I have noticed something quite disturbing.

 

It seems that when I use preloader to load a pre-compiled swf (currently the site as whole) my FPS drops by about half...!

 

Any ideas why this could be?

The pre-compiled swf plays at perfect framerate when compiled from within it's original fla.

 

As it's not too heavy, I've attached my preloader code, in case you can see anything there:

 

package {

   import flash.display.MovieClip;
   import flash.display.StageAlign;
   import flash.display.StageScaleMode;
   import flash.text.TextField;

   import flash.system.Capabilities;

   import com.greensock.*;
   import com.greensock.easing.*;
   import com.greensock.events.LoaderEvent;
   import com.greensock.loading.LoaderMax;
   import com.greensock.loading.SWFLoader;
   import com.greensock.loading.data.SWFLoaderVars;
   import com.greensock.loading.display.ContentDisplay;

   import flash.events.*;


   public class preloader extends MovieClip {

       // SET UP MOVIE CLIP VARIABLE(S) //
       public var mcContent:MovieClip;// = new MovieClip();

       public var tlSpinner:TimelineMax = new TimelineMax( { repeat: -1 } );


       // CONSTRUCTOR FUNCTION //
       public function preloader() {

           // *** SET UP STAGE PROPERTIES *** //
           stage.align = StageAlign.TOP_LEFT;
           stage.scaleMode = StageScaleMode.NO_SCALE;

           // register to re-check when stage size changes
           stage.addEventListener( Event.RESIZE, onResize, false, 0, true );
           // - Initial Resize of mcContent to match screen resolution of user - //
           stage.dispatchEvent(new Event(Event.RESIZE));


           // *** LOADING CODE *** //

           var config:SWFLoaderVars = new SWFLoaderVars();
           config.container(this);
           config.x(0);
           config.y(0);
           config.alpha(0);
           config.visible(false);
           config.onComplete(_completeHandler); // Functions are at end of file
           config.onProgress(_progressHandler);
           // USE THIS? config.autoDispose(true);

           // LOAD MAIN SWF
           var swf:SWFLoader = new SWFLoader("swfs/main.swf", config);
           swf.load();

           // - Rotate Spinner - //
           tlSpinner.append(TweenLite.to(mcProgress.mcSpinner, 1.2, { rotation: 315, ease:SteppedEase.create(8) } ));

       }

       private function _progressHandler(event:LoaderEvent):void {
           // USE PROGRESS DATA TO UPDATE "LOADING..." MC

           var percent = int(event.target.progress * 100) + "%";
           mcProgress.txtProgress.text = percent;
           //trace("loading... " + event.target.progress);
       }

       private function _completeHandler(event:LoaderEvent):void {

           // - Add loaded Content into a variable - //
           var currentContent:ContentDisplay = event.target.content;

           mcContent = MovieClip(currentContent.rawContent.getChildByName("mcContent"));
           mcContent.alpha = 0;
           mcContent.visible = false;

    currentContent.rawContent.removeChild(currentContent.rawContent.getChildByName("mcContent"));

           // - Add loaded content to the stage - //
           this.addChild(mcContent);

           // - Hide progress bar, and show main content - //
           TweenLite.to(mcProgress, 0, { autoAlpha: 0, immediateRender:  false } );
           TweenLite.to(mcContent, 0, { autoAlpha: 1, immediateRender: false } );// delay: 1 } );

           tlSpinner.stop();
       }

       private function onResize( e:Event ) {
           checkStageSize();
       }

       // do the scaling here
       private function checkStageSize():void {
           var wid:Number = stage.stageWidth;
           var hei:Number = stage.stageHeight;

           var needToScaleDown:Boolean = (wid < 1366); // or some other test
           var scale:Number;
           var ratio:Number = 0.562;
           if ( needToScaleDown ) {
               if (wid > 768) {
                   scale = wid/1366;
                   this.scaleX = this.scaleY = scale;
               }
               else if (wid <= 768) {
                   scale = 768/1366;
                   this.scaleX = this.scaleY = scale;
               }
           }
           else {
               this.scaleX = this.scaleY = 1;
           }
           if (wid > 1366) {
               this.x = (wid / 2) - (this.width / 2);
               this.y = (hei / 2) - (this.height / 2);
           }
       }        

   }

}

Link to comment
Share on other sites

Ok, I have discovered that the above framerate problem stems from adding an eventListener for Event.RESIZE...

 

I would like to keep using this function, however; any ideas?

 

Thanks

 

 

edit:

 

The above is untrue. It seems in point of fact, that the problem is with the function that is called by the aforementioned eventListener.

If I edit out the listener, and put in a single call the checkStageSize, I get the same loss of FPS.

If I comment out that single call, the FPS is fine again..!

 

This is wierd, is it not?

 

Is the framerate collapsing simply because I have scaled _all_ of my content, thus it's recalculating scale at the beginning of each enterFrame???

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...