Skip navigation
Currently Being Moderated

Preloader for SWF

Apr 26, 2012 2:32 PM

Tags: #template #swf #preloader #actionscript_3

Okay so I'm working on something in Flash CS5.5 with actionscript 3.  I need a preloader for it and for the time being I'm happy with the basic 'preloader for swf' template flash provides.  Unfortunately it doesn't seem as simple as I would've thought to integrate that into my project.  I tried copying and pasting everything from the template file into the first frame of my existing project and that doesn't work.  I also tried taking my existing project within a movieclip and adding it to the second frame of the template file in place of the 'add large content here' frame and still no luck.  In both instances I've never received an error message or any indication that there's trouble but when hit ctrl + enter twice to simulate the loading all I get is an ugly white screen with ticking dots...which I assume is flash's default loader...cuz it appears with or with out the preloader template added. 

 

The whole point of these templates is supposed to be that they're easy to use so I imagine my issue here isn't anything to complicated, I just can't seem to figure out what the issue is.  Any help here would be great.

Thanks in advance.

 
Replies
  • Currently Being Moderated
    Apr 26, 2012 4:47 PM   in reply to mentalcase129

    Please post the preloader code you have.

     

    You definitely don't want to put the content you're loading INTO your preloader. Your preloader should only contain the code/graphics necessary to load another SWF and provide some visual progress.

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 27, 2012 6:22 AM   in reply to mentalcase129

    Let's ditch the template.

     

    Any time you want to load something, AS3 has a few classes depending on what type of content and how you want to load it.

     

    You want to load a SWF so I'll keep it briefly to that. All I see your template doing is updating a textfield with a percent loaded and then going to the next frame. We'll do the same.

     

    This is called a stub in short terms. A very small SWF file that loads instantly which loads another SWF.

     

    Open a new AS3 document. On frame 1 of layer 1, click the frame. Go in the Actions panel. Put this in there, and I'll comment it so you know what I'm doing.

     

    // importing necessary classes

    import flash.display.Loader; // this actually loads the SWF and is also a displayable object which will contain the SWF

    import flash.events.Event; // this event lets us know when loading is complete

    import flash.events.ProgressEvent; // this event gives us feedback on progress while preloading

    import flash.text.TextField; // this lets us make a TextField but we'll be using the users OS fonts (not embedding one)

    import flash.net.URLRequest; // this makes a request to a specific file or url (the SWF)

     

    // first create a text field and align it to the center of the screen 200px wide

    var myText:TextField = new TextField();

    addChild(myText); // add it to the display list so it's visible

    myText.width = 200;

    myText.x = Math.round((stage.stageWidth - 200) / 2); // position in center horizontally

    myText.y = Math.round((stage.stageHeight - myText.height) / 2); // position in center vertically

     

    // now lets create our Loader which will do the loading of the SWF

    var myLoader:Loader = new Loader();

     

    // tell flash what events this loader needs to let us know about, progress and complete

    myLoader.contentLoaderInfo.addListener(Event.COMPLETE, onComplete); // listen for complete

    myLoader.contentLoaderInfo.addListener(ProgressEvent.PROGRESS, onProgress); // listen for, you guessed it, progress

     

    // start the load process

    // NOTE: the functions defined below will fire off as soon as this next line is run to handle progress and complete

    myLoader.load(new URLRequest("somefile.swf")); // replace the filename with your SWF or a path/to/your.swf

     

    // progress event listener onProgress

    function onProgress(e:ProgressEvent):void

    {

         myText.text = "Loaded: " + Math.round((e.bytesLoaded / e.bytesTotal) * 100); // overwrite text in textfield with percent loaded

    }

     

    // complete event listener which will actually add the SWF to the stage

    function onComplete(e:Event):void

    {

         // clean up the progress text, we don't need it

         removeChild(myText);

         myText = null;

     

         // display the new SWF

         addChild(myLoader);

    }

     

     

    It may seem like a lot but remove the comments and it's just a tiny bit of code. Like so:

     

     

    import flash.display.Loader;
    import flash.events.Event;
    import flash.events.ProgressEvent; 
    import flash.text.TextField; 
    import flash.net.URLRequest; 
    
    var myText:TextField = new TextField();
    addChild(myText); 
    myText.width = 200;
    myText.x = Math.round((stage.stageWidth - 200) / 2); 
    myText.y = Math.round((stage.stageHeight - myText.height) / 2); 
    
    var myLoader:Loader = new Loader(); 
    
    myLoader.contentLoaderInfo.addListener(Event.COMPLETE, onComplete); 
    myLoader.contentLoaderInfo.addListener(ProgressEvent.PROGRESS, onProgress); 
    
    myLoader.load(new URLRequest("somefile.swf")); 
    
    function onProgress(e:ProgressEvent):void
    {
         myText.text = "Loaded: " + Math.round((e.bytesLoaded / e.bytesTotal) * 100); 
    }
    
    function onComplete(e:Event):void
    {
         removeChild(myText);
         myText = null; 
         addChild(myLoader); 
    }
     
    |
    Mark as:
  • Currently Being Moderated
    Apr 27, 2012 10:16 AM   in reply to mentalcase129

    I added the TextField for you, you don't need to do anything but paste that code in frame 1 and change the name of the SWF.

     

    Paste back the code that you're using, all of it, using http://pastebin.com/ please. Set the code type to ActionScript 3 also. Then post the URL here.

     

    You seem to be getting errors on assigning the listeners to contentLoaderInfo but that is the correct way to do it. You can see the same code in this Adobe example here:

     

    http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/fla sh/display/Loader.html#contentLoaderInfo

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 27, 2012 10:40 AM   in reply to mentalcase129

    Ah, crap, I hand typed it.

     

    Change addListener to addEventListener.

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 27, 2012 2:17 PM   in reply to mentalcase129

    Use the TextFormat class.

     

    e.g.

     

    // add this to the imports

    import flash.text.TextFormat;

    import flash.text.TextFormatAlign;

     

    // then below myText:Textfield = new TextField() add:

    var myFormat:TextFormat = new TextFormat("Arial", 14, 0x999999); // set to Arial, size 14, darkish gray

    myFormat.align = TextFormatAlign.CENTER;

    myText.defaultTextFormat = myFormat;

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 27, 2012 4:33 PM   in reply to mentalcase129

    The numbers are hex, just like HTML. In Photoshop you can choose any color in the color picker and at the bottom it tells you the hex color for it.

     

    Hex color is in RGB, or Red Green Blue. The order is important. The 0x just lets flash know it's going to be a hex value. After that it expects 6 characters. 2 for Red, 2 for Green, 2 for Blue, in that order. So 0xRRGGBB.

     

    10-base goes 0,1,2,3,4,5,6,7,8,9. Hex-base goes from 0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f. Just a FYI. 0 being least, F being most.

     

    Some examples..

     

    Pure red: 0xFF0000;

    Pure green: 0x00FF00;

    Pure blue: 0x0000FF;

     

    Mix colors according to how you learned in grade school .

     

    Red plus green = yellow. Or 0xFFFF00.

     

    You should get the general idea.

     

    Also the Flash color picker tells you the same hex colors, it just doesn't put 0x in front of them. So when it tells you a color is, say 1F9C7D you would tell Textformat that it's 0x1F9C7D.

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 28, 2012 4:21 AM   in reply to mentalcase129

    You're welcome

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points