-
1. Re: Preloader for SWF
sinious Apr 26, 2012 4:47 PM (in response 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.
-
2. Re: Preloader for SWF
mentalcase129 Apr 26, 2012 9:31 PM (in response to sinious)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);
};
-
3. Re: Preloader for SWF
sinious Apr 27, 2012 6:22 AM (in response 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); } -
4. Re: Preloader for SWF
mentalcase129 Apr 27, 2012 10:10 AM (in response to sinious)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 37 1061: Call to a possibly undefined method addListener through a reference with static type flash.display:LoaderInfo. Scene 1, Layer 'Layer 1', Frame 1, Line 39 1061: 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.
-
5. Re: Preloader for SWF
sinious Apr 27, 2012 10:16 AM (in response 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:
-
-
7. Re: Preloader for SWF
sinious Apr 27, 2012 10:40 AM (in response to mentalcase129)Ah, crap, I hand typed it.
Change addListener to addEventListener.
-
8. Re: Preloader for SWF
mentalcase129 Apr 27, 2012 10:47 AM (in response to sinious)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
-
9. Re: Preloader for SWF
sinious Apr 27, 2012 2:17 PM (in response 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;
-
10. Re: Preloader for SWF
mentalcase129 Apr 27, 2012 4:12 PM (in response to sinious)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.
-
11. Re: Preloader for SWF
sinious Apr 27, 2012 4:33 PM (in response 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.
-
12. Re: Preloader for SWF
mentalcase129 Apr 27, 2012 5:16 PM (in response to sinious)Awesome, thanks for the help dude
-
13. Re: Preloader for SWF
sinious Apr 28, 2012 4:21 AM (in response to mentalcase129)You're welcome


