Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Preloader for SWF

Explorer ,
Apr 26, 2012 Apr 26, 2012

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.

TOPICS
ActionScript
3.9K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Apr 27, 2012 Apr 27, 2012

Ah, crap, I hand typed it.

Change addListener to addEventListener.

Translate
LEGEND ,
Apr 26, 2012 Apr 26, 2012

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 26, 2012 Apr 26, 2012

I've been playing around a bit and realized that myself.  The question though now is how to make the preloader open the other swf when it finishes loading it.  The code in the preset template is set to load the second frame on the timeline so presumably I need to make an adjustment so that it loads another swf.  I can see in my main swf how to set the preloader...however if I use it as it is now it flashes frame 2 of the preloader swf before switching to the main swf...and if I hit ctrl+enter again to simulate the load it just gets stuck on a white screen...so the preloader function doesn't even appear to be working this way.

Here's the code in the preloader swf...it's just taken from the template, what is it I need to change?

stop();

this.loaderInfo.addEventListener(ProgressEvent.PROGRESS, onLoading);

this.loaderInfo.addEventListener(Event.COMPLETE, onComplete);

function onLoading(evt:ProgressEvent):void {

    var loaded:Number = evt.bytesLoaded / evt.bytesTotal;

    percent_txt.text = (loaded*100).toFixed(0) + "%";

};

function onComplete(event:Event):void {

    this.loaderInfo.removeEventListener(ProgressEvent.PROGRESS, onLoading);

    this.loaderInfo.removeEventListener(Event.COMPLETE, onComplete);

    gotoAndStop(2);

};

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 27, 2012 Apr 27, 2012

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); 
}
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 27, 2012 Apr 27, 2012

Wow, thanks dude.  It's still not fully working for me though.  I read through all the comments...am I just supposed to create the text box and change swf name?  Or was I supposed to modify more?

I get these errors when I try to export it:

Scene 1, Layer 'Layer 1', Frame 1, Line 371061: Call to a possibly undefined method addListener through a reference with static type flash.display:LoaderInfo.

Scene 1, Layer 'Layer 1', Frame 1, Line 391061: Call to a possibly undefined method addListener through a reference with static type flash.display:LoaderInfo.

I mostly do animation in flash and am pretty new to Actionscript 3 so sorry if this is a dumb question.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 27, 2012 Apr 27, 2012

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/flash/display/Loader.html#content...

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 27, 2012 Apr 27, 2012

Here's the pastebin link.

http://pastebin.com/zE5bzTPu

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 27, 2012 Apr 27, 2012

Ah, crap, I hand typed it.

Change addListener to addEventListener.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 27, 2012 Apr 27, 2012

Ya that did it...it works now.  How do I go about customizing the font and stuff?  Or is that a whole other ballgame?  lol

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 27, 2012 Apr 27, 2012

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;

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 27, 2012 Apr 27, 2012

Awesome, that was easy!  One last question on this...for the numeric colours...anyway to figure out what number for what colour?  Is there a way in flash?  The numbers in the colour picker don't totally jive the _x_______, unless there's some math I need to do.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 27, 2012 Apr 27, 2012

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 27, 2012 Apr 27, 2012

Awesome, thanks for the help dude

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 28, 2012 Apr 28, 2012
LATEST

You're welcome

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines