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

Why Won't Flash Read My AS?

New Here ,
Mar 14, 2013 Mar 14, 2013

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     

TOPICS
ActionScript
3.4K
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

Guide , Mar 16, 2013 Mar 16, 2013

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):

  1. Your Main Class can't create the assets at the time you tell it to,
...
Translate
Community Expert ,
Mar 14, 2013 Mar 14, 2013

is Main your document class?

what text/trace statements do you see?

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
New Here ,
Mar 14, 2013 Mar 14, 2013

yes Main is my document class, and I see

"and how are you today?"

and then

"why hello there!"

Which seems very strange too

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
Community Expert ,
Mar 14, 2013 Mar 14, 2013

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.

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
New Here ,
Mar 15, 2013 Mar 15, 2013

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!!

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
Community Expert ,
Mar 15, 2013 Mar 15, 2013

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?

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
New Here ,
Mar 15, 2013 Mar 15, 2013

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.

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
New Here ,
Mar 15, 2013 Mar 15, 2013

Screen Shot 2013-03-15 at 7.45.43 AM.png

Screen Shot 2013-03-15 at 7.42.32 AM.png

Screen Shot 2013-03-15 at 7.51.50 AM.png

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 😕

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
Community Expert ,
Mar 15, 2013 Mar 15, 2013

where's menu and playBttn?

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
New Here ,
Mar 15, 2013 Mar 15, 2013

In the code:

Screen Shot 2013-03-15 at 12.47.37 PM.png

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.

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
Guide ,
Mar 15, 2013 Mar 15, 2013

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.

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
Community Expert ,
Mar 15, 2013 Mar 15, 2013

where are those objects on your main timeline?

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
New Here ,
Mar 15, 2013 Mar 15, 2013

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.

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
Guide ,
Mar 15, 2013 Mar 15, 2013

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?

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
New Here ,
Mar 15, 2013 Mar 15, 2013

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

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
Community Expert ,
Mar 15, 2013 Mar 15, 2013

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.

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
New Here ,
Mar 15, 2013 Mar 15, 2013

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.

Screen Shot 2013-03-15 at 7.28.50 PM.png

Here they are in the library in the Menu Items folder:

Screen Shot 2013-03-15 at 7.30.26 PM.png

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.

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
Community Expert ,
Mar 15, 2013 Mar 15, 2013

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)

                    }

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
New Here ,
Mar 15, 2013 Mar 15, 2013

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

Screen Shot 2013-03-15 at 10.03.49 PM.png

Here is the menuScreen properties which are identical to the play_Button properties:

Screen Shot 2013-03-15 at 10.05.44 PM.png

Here is the new code of Main.as, my Document Class:

Screen Shot 2013-03-15 at 10.07.13 PM.png

Here is what I believe is a size report, showing what's happening on frame 1 and 2 respectively:

Screen Shot 2013-03-15 at 10.07.37 PM.pngScreen Shot 2013-03-15 at 10.07.45 PM.png

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.

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
Guide ,
Mar 16, 2013 Mar 16, 2013

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):

  1. Your Main Class can't create the assets at the time you tell it to, because they're not compiled in yet. or
  2. They get compiled in because you're referencing them in Frame 1, but because you've forced so much stuff to have to be loaded prior to frame 1, by the time you even see the loader screen, the movie is already loaded.

MPO is that it is probably #1 based on the symptoms you report.

So let's try this:

  1. Delete "Main" out of the Document Class field.
  2. Create a new MC in your library and set Main as its Base Class.
  3. Place an instance of it in Frame 2.
  4. You can continue with the overly complicated logic you already have, or you could go with the simple/elegant solution of just allowing the playhead to stop at the compiler frame and play into Frame 2, where you can put a stop() frame script.
  5. Go back to calling your mainLoop() directly from the constructor of Main.

Notes:

  1. The dominant convention in AS is to use camel case with the first letter capitalized. This applies just as much to library symbols.
  2. To generate a size report, click the "Generate size report" box in publish settings. It will make a text file next to the swf.
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
Community Expert ,
Mar 16, 2013 Mar 16, 2013

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.

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
Guide ,
Mar 16, 2013 Mar 16, 2013

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.

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
Community Expert ,
Mar 16, 2013 Mar 16, 2013

we already solved that problem.

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
Guide ,
Mar 16, 2013 Mar 16, 2013

I don't see that in the discussion. Are you guys talking over the phone or via email, separate from this discussion?

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
Community Expert ,
Mar 16, 2013 Mar 16, 2013

  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)

                    }

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