Ultrashock Forums > Flash > Flash Professional
Preloader in AS3?

You are currently viewing our website as a guest which gives you limited access to forums, files and other resources.

Click here to join now for free, and start interacting with our members, download files and much more!

Click here if you are looking for our Flash files and other professional assets.
 
Post Reply | View first unread | Rate Thread Search this Thread | Thread Tools | Display Modes

#1
Bookmark and Share!
Preloader in AS3?
Old 2007-08-20

Hello,

I'm not exactly a Flash Professional, but I'm hoping most of you are. I've chosen to do a site in AS3 because I like Tween Class. I've run into a serious snag though. Preloaders and loadMovie. I've searched and searched google and I've gotten nothing but extremely vague results.

I have a main movie that I would like to preload. This movie will load one additional movie on top of it (nothing complex). But in AS3, it seems to be a chore.

Do any of you knowledgeable folks at ultrashock have a step by step tutorial or even willing to post the steps/code here?

I appreciate any information you have to offer. Remember, try to respond to this thread in the most basic of terms. I am not a coder, but I'm trying.

Thanks,

Cmoore
postbit arrow 9 comments | 361 views postbit arrow Reply: with Quote   
Registered User
cmoore is offline
seperator
Posts: 5
2007-08-20
seperator

Ultrashock Member Comments:
Nutrox's Avatar Nutrox Nutrox is offline Super Moderator Nutrox lives in United Kingdom 17 Creative Assets 2007-08-20 #2 Old  
Hi cmoore, welcome to Ultrashock.

The Loader class is what you need to be looking at really. It is the main class you will use to load external SWF and image files.

It is difficult to tell how much of this you have already picked up but here is a quick example of how to load an external SWF file and listen for progress/complete events.

ActionScript Code:
  1. // A URLRequest object pointing to the external movie.
  2. var mainURL:URLRequest = new URLRequest( "movie.swf" );
  3.  
  4. // A Loader object (use to load SWF, PNG, GIF, JPG).
  5. var mainMovie:Loader = new Loader();
  6.  
  7. // Add listeners to the Loader.contentLoaderInfo object.
  8. mainMovie.contentLoaderInfo.addEventListener( ProgressEvent.PROGRESS, progressListener );
  9. mainMovie.contentLoaderInfo.addEventListener( Event.COMPLETE, completeListener );
  10.  
  11. // Load the external movie using the URLRequest.
  12. mainMovie.load( mainURL );
  13.  
  14. // Invoked while the exteral movie is loading.
  15. function progressListener( event:ProgressEvent ):void
  16. {
  17.     trace( "progress = " + event.bytesLoaded );
  18.    
  19.     // This is where you would update the progress bar.
  20. }
  21.  
  22. // Invoked when the external movie has completed loading.
  23. function completeListener( event:Event ):void
  24. {
  25.     trace( "complete" );
  26.    
  27.     addChild( mainMovie );
  28.    
  29.     // It would be a good idea to remove the
  30.     // mainMovie.contentLoaderInfo listeners here as well.
  31. }
If you need any more help feel free to ask.

Reply With Quote  
cmoore cmoore is offline 2007-08-20 #3 Old  
Thank you so much for your help. I used the code and it works. The only thing that really confused me is where you said...

// This is where you would update the progress bar.

The way I used to do it was with a dynamic text field. Is this what you mean? Do you mean using a progress bar component? If you meant the text field, what would I call it?

Thank you again. I really appreciate it.

Cmoore
Reply With Quote  
Nutrox's Avatar Nutrox Nutrox is offline Super Moderator Nutrox lives in United Kingdom 17 Creative Assets 2007-08-20 #4 Old  
Originally posted by cmoore
...
Do you mean using a progress bar component? If you meant the text field, what would I call it?
...
It depends on what you want to do really. I added that comment into the progressListener() function because that is where you can grab the number of bytes loaded and do the percentage calculations etc, so it would be the easiest place to update your progress bar or text field.

ActionScript Code:
  1. function progressListener( event:ProgressEvent ):void
  2. {
  3.     trace( "progress = " + event.bytesLoaded );
  4.  
  5.     var percent:int = 0;
  6.  
  7.     if( event.bytesTotal > 0 && event.bytesLoaded > 0 )
  8.     {
  9.         percent = (100 / event.bytesTotal) * event.bytesLoaded;
  10.     }
  11.    
  12.     myTextField.text = percent + "%";
  13. }
Reply With Quote  
cmoore cmoore is offline 2007-08-20 #5 Old  
Worked great. Thanks for your time. Just curious, but are load levels now obsolete? If not, how are they determined? Is it by the order they are loaded? I'm just curious, because I might be loading a second swf into this mainmovie.

Take care and thanks again Nutrox.

Cmoore
Reply With Quote  
geo_speed geo_speed is offline geo_speed lives in India 2007-08-20 #6 Old  
Cool One Nutrox. And, How can i control the loaded swf?
I mean stop the movie or play the movie and those things.
Reply With Quote  
Nutrox's Avatar Nutrox Nutrox is offline Super Moderator Nutrox lives in United Kingdom 17 Creative Assets 2007-08-20 #7 Old  
@cmoore

Levels no longer exist in AS3, instead everything is displayed and arranged in display lists. You should take a look at the DisplayObjectContainer class (or the Sprite or MovieClip class) because it contains the functions you will need such as addChild() removeChild() getChildIndex() swapChildren() and so on.


@geo_speed

You need to access the loaded SWF file via the Loader.content property, so once the external SWF file has loaded you could then do something like this:

ActionScript Code:
  1. MovieClip(myLoader.content).gotoAndStop( 10 );
Where myLoader is your Loader object.
Reply With Quote  
geo_speed geo_speed is offline geo_speed lives in India 2007-08-21 #8 Old  
Thanks Nutrox, But it’s not working.

ActionScript Code:
  1. function completeListener( event:Event ):void
  2. {
  3.         trace( "complete" );
  4.        // trace(mainMovie);
  5.         addChild(mainMovie);
  6.         MovieClip(mainMovie.content).gotoAndStop( 100 );
  7.       // me add the line here
  8.         // It would be a good idea to remove the
  9.         // mainMovie.contentLoaderInfo listeners here as well.
  10. }

I used your code only.
Reply With Quote  
Nutrox's Avatar Nutrox Nutrox is offline Super Moderator Nutrox lives in United Kingdom 17 Creative Assets 2007-08-21 #9 Old  
Does the SWF file you are loading have 100 frames?
Reply With Quote  
geo_speed geo_speed is offline geo_speed lives in India 2007-08-21 #10 Old  
Yeah... sorry its working now.
Before that I tried with one video swf. I think there is the problem.

Very thanks Mr.Nutrox
Reply With Quote  
Thread Tools
Display Modes Rate This Thread
Rate This Thread: