Copy link to clipboard
Copied
Alright well I've been trying for days now to put a preloader in my code. Right now, this is what I am trying:
This is the preloader template that adobe gives you. I have it in frame one on three layers.
stop();
import flash.events.ProgressEvent;
import flash.events.Event;
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);
};
This is my Main class:
//Contructor method in which I can initiate the menu
public function Main()
{
menuLoop();
trace("why hello there!");
}
/*Here I can add buttons. The Play button will pull
the script out of the menu loop and into the game loop*/
public function menuLoop()
{
trace("And how are you today?")
menu.x = 200;
menu.y = 240;
stage.addChild(menu);
playBttn.x = 175;
playBttn.y = 300;
stage.addChild(playBttn);
playBttn.addEventListener(MouseEvent.CLICK, initGame)
}
unfortunately, none of my children actually get added to the screen even though the output displays the friendly conversation I traced.
I have my "Export on frame:" at 2. I have "Default Linkage" set to "merged into code."
I added a second AS frame to the timeline and added "stop();" to the actions. All that did was leave me with a blank screen.
I am so desperate to make this work! Any advice is appreciated
So, when you define playBtn=new play_button;
This has to run before the constructor. As do all your other statements where you're defining new someLibraryObject(). Therefore, by the time the code even gets to your preloader, you have one of two situations (I'm not sure which, because I code on the timeline, in large part to amortize compilation of the library symbols throughout the swf, reducing the need to even have a preloader):
Copy link to clipboard
Copied
that's good.
are you seeing "and how are you today?"
if yes, make sure menu and playBttn are visible and make sure no code executes after menuLoop that could be adding other children that cover menu and playBttn.
Copy link to clipboard
Copied
Before when I ran the code, the AS stopped at menuLoop. It couldn't progress any further because to do so requires that the playBttn is clicked.
Yes, "And how are you today?" was tracing.
Yours is the code I expected to work, but for some reason it didn't. The strange thing is that it's doing basically exactly what Amy's code does as far as I can tell, but hers worked.
So Amy I did what you said created mcMain (sorry no camel case on that one since it's not in my external .as), set the base class to Main and slipped it into frame 2 with stop(); in the layer above it.
Works like a charm.
Haha I will admit I feel a little dirty doing it that way, right now in the very beginning of my AS career I believe heavily in a blank stage and a book worth of code. In all the advice I found before coming here, every one of them said it should work without anything else on the timeline, but it didn't. With the ease of this technique after spending days trying to solve what I wish had been an easy problem, I will have to study the timeline a bit more vehemently.
Both of you are amazing, staying with me and helping me through a problem that I would never have been able to solve on my own. I thought it was time to throw in the towel and start a completely new project after spending weeks polishing this one off. Both of you have taught me things about Flash and AS well outside the realm of preloaders, and it is information that I plan to use often in upcoming projects.
If there is anything I can do for either of you, please let me know.
Copy link to clipboard
Copied
FWIW, I wouldn't have done it that way either, but it was a quick way that would work. I figured the stop() in frame 2 was at least somewhat cleaner than all the stuff you were doing in frame 1
.
For more on the timeline approach, you may want to check out:
Copy link to clipboard
Copied
Don't add any children to stage. Add them to document class display list.
Copy link to clipboard
Copied
By that do you mean addChild(Object) instead of stage.addChild(Object)?
I changed them to the former. I only added them to the stage actually to see if that would make a difference. It did not haha
Thank you!
Copy link to clipboard
Copied
Why do you must have the Preloader inside your Main document?
Make a seperate Preloader (_preloader.fla/xfl ->Preloader.swf);
var l:Loader = new Loader();
l.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loop);
l.contentLoaderInfo.addEventListener(Event.COMPLETE, done);
l.load(new URLRequest("Main.swf"));
function loop(e:ProgressEvent):void
{
var kBytesLoaded:int = Math.round(e.bytesLoaded/1024);
var kBytesTotal:int = Math.round(e.bytesTotal/1024);
var perc:Number = kBytesLoaded / kBytesTotal;
trace("PERCENT:"+perc);
}
function done(e:Event):void
{
addChild(l);
}
then you don´t have to worry in your Main.as about how to make it work with the Preloader.
and in your Main.as write a constructor that waits until Main.swf is added to the DisplayList:
public function Main(){
if (stage == null)
{
addEventListener(Event.ADDED_TO_STAGE, init);
}
else
{
int();
}
}
private function init(e:Event = null):void
{
//do all the adding here
addChild(playBttn);
addChild(menu);
}
Copy link to clipboard
Copied
I appreciate the template and advice, and it may become very helpful to me in the future. For now though, I would really like to upload my file to certain sites that only allow one .swf to upload, so I don't think I could use this.
Thank you very much for your help!
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more