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
is Main your document class?
what text/trace statements do you see?
Copy link to clipboard
Copied
yes Main is my document class, and I see
"and how are you today?"
and then
"why hello there!"
Which seems very strange too
Copy link to clipboard
Copied
if menu and playBttn are not on the first frame you should be seeing errors or you're not displaying enough code to determine the problem.
Copy link to clipboard
Copied
I believe there are no errors because while they are not on the first frame, classes are not exported until the 2nd frame.
I now have the preloader and Actions in frame one. Then I placed the menu and playBttn instances in frame 2, and added the stop(); function. With the timeline set up that way, the preloader actually works and the game is stopped at the menu. However I do not how to bridge from that into the main class. Or more accurately, I don't understand why the Main class is not doing what it says it is.
Im not sure what other code to include, I could show you my imports or my variables, but I don't think that would make a difference.
One thing that I'm not sure about is maybe the fact that I don't have a package name set up. Still, that shouldn't make a difference since when I run the program the Main class still responds with the trace functions.
Thank you very much for your help so far!!
Copy link to clipboard
Copied
your document class still executes in the first frame.
so, if you're not seeing error messages (and menu and playBttn don't exist in frame 1 of your fla) you're not using the class file you displayed.
to confirm, change your
trace("why hello there!");
to
trace("why hello there! xxxxxxxx");
and retest.
do you see the xxxxxxxx?
Copy link to clipboard
Copied
yep
I went into Main.as and changed:
public function Main()
{
menuLoop();
trace("why hello there!");
}
to:
public function Main()
{
menuLoop();
trace("why hello there!xxxxxxxxxx");
}
and output displayed:
And how are you today?
why hello there!xxxxxxxxxx
in that order, even though "And how are you today?" is added in menuLoop(), after the constructor function.
Copy link to clipboard
Copied
Maybe those will help. The first is my timeline right now with only key frames in the first frame. The Actionscript is the same as the first AS I posted.
The second image just shows my Doc Class and AS settings. When I click the checkmark it displays the correct AS file.
The last image is my constructor function, menuLoop, swf, and output when I test the .fla. It stops at %100 and nothing happens. When I simulate download the screen is white until it shows up 😕
Copy link to clipboard
Copied
where's menu and playBttn?
Copy link to clipboard
Copied
In the code:
I don't have it on the timeline at all.
Oh I forgot to mention I changed the constructor function a little to add the loaderInfo event listener. I thought maybe the problem was that the code was getting ahead of itself, but it didn't seem to make a difference.
Copy link to clipboard
Copied
I don't understand why people go through all this crap for preloaders (except they've been told to by a long line of people who apparently never figured out the easy way to do this).
On Frame 1, put the word "Loading..." If you want, create a spinner graphic. Since your Classes are all set to export on frame 2, it will stay on frame 1 until they are loaded.
Put something in frame 2 an instance name, and use a getter/setter pair in your Document Class to detect that it has been added. Call stop() based on this information (I wouldn't out it directly in the setter, but how you do it is up to you) if appropriate.
Copy link to clipboard
Copied
where are those objects on your main timeline?
Copy link to clipboard
Copied
My timeline is empty except for the preloader. The AS in question is a game, all objects are added through the document class. That's how I've learned so far and don't know another way to do it, although now that I think about it there are probably lots of better ways to do it.
Copy link to clipboard
Copied
Have you run a size report to see where things are compiling? It's possible that since your Document Class directly references every Class you are using that everything is exporting on frame 1, before your preloader. What happens if you put trace statements in your preloader event handlers?
Copy link to clipboard
Copied
Just tried that Amy,
What happens is that the trace statement in the constructor class reads off first, then the trace statement in the preloader event handler goes off many times. Last, the trace in the menu loop goes off
Copy link to clipboard
Copied
again: if menu and playBttn are not on the first frame you should be seeing errors or you're not displaying enough code to determine the problem.
so, where are menu and playBttn defined?
that was my original question after confirming Main was your document class and you still have not answered that. and, as i mentioned in the original question: you're not displaying enough code to determine the problem. menu and playBttn must be defined by code somewhere if what you've reported is true.
Copy link to clipboard
Copied
Sorry, I didn't understand when you said "where's menu and playBttn," I thought you wanted to see where they were added, not defined.
They are defined on lines 43 and 44 in Main.AS, my document class.
Here they are in the library in the Menu Items folder:
Before I added the loading screen and actions, my game ran smoothly. Well, smoothly without loading I should say.
Sorry again for not showing where the two are defined earlier.
I also made a mistake when I told you I was showing my entire Document Class. The whole class actually includes many more variables and functions, in fact the game is almost completely finished. I wasn't smart enough to create the preloader before I created everything else, something I will certainly do in any future project.
Copy link to clipboard
Copied
to solve your problem, you need to
1. export your menuScreen symbol and play_Button symbols in frame 2:
right click each symbol, click properties and check their export frame.
2. delay execution of menuLoop() until frame 2 is played:
public function Main()
{
this.addEventListener(Event.ENTER_FRAME,enterframeF);
trace("why hello there!");
}
function enterframeF(e:Event):void{
if(this.currentFrame==2){
this.removeEventListener(Event.ENTER_FRAME,enterframeF);
menuLoop();
}
}
/*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)
}
Copy link to clipboard
Copied
Thank you very much kglad, that code makes a lot of sense to me and is straightforward. However, it still isn't working.
Here is the Action Script, stage, and timeline for PotN.fla
Here is the menuScreen properties which are identical to the play_Button properties:
Here is the new code of Main.as, my Document Class:
Here is what I believe is a size report, showing what's happening on frame 1 and 2 respectively:
I tried several variations in the frame-action script as far as progressing the timeline. I tried gotoAndPlay(2), gotoAndStop(2), and simply play();
I also wanted to say thank you again for your help so far. As frustrating as this is, it's much easier to go through it with help.
At this point I am wondering if I messed up something so fundemental it would just be easier to start fresh, as daunting as that seems.
Copy link to clipboard
Copied
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):
MPO is that it is probably #1 based on the symptoms you report.
So let's try this:
Notes:
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
I think they can't become visible because the assets that form the implementation that live in the library aren't available before frame 1, when the new() operator is called.
Copy link to clipboard
Copied
we already solved that problem.
Copy link to clipboard
Copied
I don't see that in the discussion. Are you guys talking over the phone or via email, separate from this discussion?
Copy link to clipboard
Copied
public function Main()
{
this.addEventListener(Event.ENTER_FRAME,enterframeF);
trace("why hello there!");
}
function enterframeF(e:Event):void{
if(this.currentFrame==2){
this.removeEventListener(Event.ENTER_FRAME,enterframeF);
menuLoop();
}
}
/*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)
}
Find more inspiration, events, and resources on the new Adobe Community
Explore Now