Skip to main content
Known Participant
April 3, 2013
Question

Why does flash acknowledge the timeline but not the code

  • April 3, 2013
  • 2 replies
  • 971 views

Alright so I have posted a similar question before, but it was never truly answered, only fixed.  I mean, I recieved incredible amounts of help and a solution that works, but not one that makes sense to me.

After adding a preloader with this code:

    

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 {

          //trace("Do Not Disturb");

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

}

package cbhCode

{

          import flash.display.MovieClip;

          import flash.events.Event;

          import com.rw.GameClock

          import flash.events.MouseEvent;

          public class Main extends MovieClip

          {

                    //TOYS

                    public var bouncyBall:BouncyBall = new BouncyBall();

                    //BROKE TOYS

                    public var brokeBouncyBall:BrokeBouncyBall = new BrokeBouncyBall();

                    //This array will hold the names of all the safe toys so when they spawn, I can simply reference a number of this array

                    public var safeToys:Array = new Array(bouncyBall);

                    //This is the same as the safeToys array, but for broken toys

                    public var brokenToys:Array = new Array(brokeBouncyBall);

                    //This array will hold any toys, safe or broken, that are on the stage

                    public var stageToys:Array = new Array();

                    //This variable will be used to hold the object in the array chosen at random

                    public var chooseToy:int;

                    //These two variables hold the length of safeToys and brokenToys

                    public var brokeToysNum:int = (brokenToys.length -1);

                    public var safeToysNum:int = (safeToys.length -1);

                    public function Main()

                    {

                              // constructor code

                              initGame();

                    }

                    public function initGame() {

                              trace("hello");

                              safeToys[0].x = 0;

                              safeToys[0].y = -50;

                              stage.addChild(safeToys[0]);

                              stageToys.push(safeToys[0]);

                              brokenToys[0].x = 0;

                              brokenToys[0].y = -1000;

                              stage.addChild(brokenToys[0]);

                              stageToys.push(brokenToys[0]);

                              addEventListener(Event.ENTER_FRAME, checkToyLocation);

                    }

                    public function checkToyLocation(e:Event)

                    {

                              trace(currentFrame);

                              for (var i:int; i < stageToys.length; i++)

                              {

                                        //once a toy reaches the end of the stage, it needs to be removed

                                        if (stageToys.x > stage.stageWidth)

                                        {

                                                  trace("later");

                                                  stage.removeChild(stageToys);

                                                  stageToys.splice(i,1);

                                        }

                              }

                    }

          }

}

This topic has been closed for replies.

2 replies

Inspiring
April 6, 2013

Try to add objects to the Main class dispaly list - not stage.

Do you see anything when you use the following class?

package cbhCode

{

          import flash.display.MovieClip;

          import flash.events.Event;

          import com.rw.GameClock

          import flash.events.MouseEvent;

 

          public class Main extends MovieClip

          {

                    //TOYS

                    public var bouncyBall:BouncyBall = new BouncyBall();

                    //BROKE TOYS

                    public var brokeBouncyBall:BrokeBouncyBall = new BrokeBouncyBall();

                    //This array will hold the names of all the safe toys so when they spawn, I can simply reference a number of this array

                    public var safeToys:Array = new Array(bouncyBall);

                    //This is the same as the safeToys array, but for broken toys

                    public var brokenToys:Array = new Array(brokeBouncyBall);

                    //This array will hold any toys, safe or broken, that are on the stage

                    public var stageToys:Array = new Array();

                    //This variable will be used to hold the object in the array chosen at random

                    public var chooseToy:int;

                    //These two variables hold the length of safeToys and brokenToys

                    public var brokeToysNum:int = (brokenToys.length - 1);

                    public var safeToysNum:int = (safeToys.length - 1);

 

                    public function Main()

                    {

                              // constructor code

                              initGame();

                    }

 

                    public function initGame():void

                    {

                              trace("hello");

 

                              safeToys[0].x = 0;

                              safeToys[0].y = -50;

                              addChild(safeToys[0]);

                              stageToys.push(safeToys[0]);

 

                              brokenToys[0].x = 0;

                              brokenToys[0].y = -1000;

                              addChild(brokenToys[0]);

                              stageToys.push(brokenToys[0]);

 

                              addEventListener(Event.ENTER_FRAME, checkToyLocation);

 

                    }

 

                    public function checkToyLocation(e:Event):void

                    {

                              trace(currentFrame);

                              for (var i:int; i < stageToys.length; i++)

                              {

                                        //once a toy reaches the end of the stage, it needs to be removed

                                        if (stageToys.x > stage.stageWidth)

                                        {

                                                  trace("later");

                                                  removeChild(stageToys);

                                                  stageToys.splice(i, 1);

                                        }

                              }

                    }

 

          }

}

Inspiring
April 3, 2013

your point being...?

rt_wiseAuthor
Known Participant
April 3, 2013

ugh sorry, I accidently posted this and so I went back to edit it but after spending 10 minutes writing the rest, I was informed that I am not allowed to edit.

The point is that ever since I added something to the timeline, I can't add anything to the stage using Main.as, even though the .swf still reads through it. 

For example, when I test the game, output displays "hello," then "2" a bunch of times.  Then a few seconds later it displays "later" twice, since the BrokeBouncyBall class and BouncyBall class automatically move to the right every frame.  Meanwhile, nothing appears on the stage, it remains blank.

I just don't understand why this is happening or how to bypass it.  It seems like, from what I have read so far, it is normal to leave the timeline empty and put everything in Main doc class.  Now I am beginning to think that is impossible if you want to include a preloader and everything in one .swf or anything else is on the timeline.  It's frustrating because if that's the case, I have to relearn loads of stuff I already know.

Inspiring
April 3, 2013

the constructor of your document class is processed before the main timeline (usually root) is added to the stage. That is the reason why most programmers I know of avoid putting any code on the timeline. The moment you are having a document class and timelinecode, things can get really ugly.

The easiest way is imo to separate the preloader and your main.swf completely otherwise you will have to uncheck all the MovieClips export in first frame checkbox in your Library you link with actionscript and place them on stage before your preloader ends.

There are thousand of tutorials how to do this (separating your preloader from your main.swf), the only thing you have to consider that in the constructor of your document class you will have to wait for the main.swf to be added to the stage to access all your Library Symbols

which can be done with these LOC:

public fúnction Main(){

  if(stage!=null){

       initGame();

       }

  else{

  addEventListener(Event.ADDED_TO_STAGE,initGame);

       }

}

public function initGame(e:Event = null) {...