skip to Main Content

First I am still new to coding so take it easy on me. Bit of back tracking before I show the code. I got a book on how to make games with ActionScript 3, only thing is it mainly works under the assumption your working with Photoshop & Flash Builder together rather than adobe animate/flash. So it has you do things like embed images, and I’m not sure how to change the code to define the images, which is exactly the problem. However at the same time, the stage is coming up as undefined access! I really, I mean how can the stage be undefined… Well any way, Heres an example of one of the Undefined Properties:

///
class BetterScrolling extends Sprite 
    var ForegroundImage: Class;
    var foregroundImage: DisplayObject = ForegroundImage();
    var foreground = ForegroundImage;
///

Theres some code in-between.

///
function BetterScrolling(){
     foreground.x = -((foreground.width - stage.width) / 2);
    foreground.y = -((foreground.height - stage.height) / 2);
    }

///

The point of this code is to create a scrolling Background. However I’m kinda complicating it because I have an X and an Y variable, yet I also have a slowed background in addition to my foreground. I will now link to the complete code as is currently, http://pastebin.com/WHg9DGsB Every change in that code I made with a reason in mind, even if it is a stupid one… so please don’t be distracted by the other errors that I’m sure are in there… for now just the defining of my images.

2

Answers


  1. There a number of issues that will keep your code from compiling.

    1. Stage is not available when your class loads. Any code that is not inside a function will run when the application is initialized (before anything is even seen on the screen). Your error is because of these lines:

      stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
      stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandHandler);
      stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
      

      Those lines are floating in your class declaration when stage does not yet exist.

      What you want to do is move that code (and any code that isn’t a variable or function definition) into a function. If you want it to run immediately, put it in your constructor (the function named the same as your class). The stage however is also not always available yet in your constructor, so to be safe, you should do the following:

      function BetterScrolling() {
          if(!stage){
              this.addEventListener(Event.ADDED_TO_STAGE, stageReady);
          }else{
              stageReady();
          }
      }
      
      function stageReady(e:Event = null):void {
          stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
          stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandHandler);
          stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
      
          //any other code that needs the stage and should run as soon as possible
      }
      
    2. You have a bunch of brackets and code that don’t seem to belong anywhere. Lines 45 – 71 will not compile properly. Most likely, you want that code in your constructor/stage ready method as well, so adding that in, you would have something like this:

      function BetterScrolling() {
          if(!stage){
              this.addEventListener(Event.ADDED_TO_STAGE, stageReady);
          }else{
              stageReady();
          }
      }
      
      function stageReady(e:Event = null):void {
          stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
          stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandHandler);
          stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
      
          foreground.x = -((foreground.width - stage.width) / 2);
          foreground.y = -((foreground.height - stage.height) / 2);
      
          background.x = -((background.width - stage.width) / 2);
          background.y = -((background.height - stage.height) / 2);
      
          cloud.x = -((cloud.width - stage.width) / 2);
          cloud.y = -((cloud.height - stage.height) / 2);
      
          character.x = 370
          character.y = 320
      
          rightInnerBoundary = (stage.stageWidth / 2) + (stage.stageWidth / 4)
          leftInnerBoundary = (stage.stageWidth / 2) + (stage.stageWidth / 4)
          topInnerBoundary = (stage.stageHeight / 2) + (stage.stageHeight / 4)
          bottomInnerBoundary = (stage.stageHeight / 2) + (stage.stageHeight / 4)
      }
      
    3. You have vars that are blank classes, then you try to instantiate them

      var BackgroundImage: Class;
      var backgroundImage: DisplayObject = BackgroundImage();
      

      This will cause a runtime error because BackgroundImage is null/undefined. Most likely, you want to export a library object in FlashPro as a class? To do that, right/alt click the object in question in the library, and go to it’s properties. Check off ‘export for actionscript’ and give it a class name. In this case, call it BackgroundImage. Then you replace the above code with:

      var backgroundImage:BackgroundImage = new BackgroundImage();
      

      As an aside, it safer to only declare you class vars that reference other objects/classes and instantiate them in the constructor. To change it like that, change the above line to this:

      var backgroundImage:BackgroundImage;
      

      Then in your constructor, do:

      backgroundImage = new BackgroundImage();
      
    Login or Signup to reply.
  2. First, DisplayObject.stage property is defined only when the DisplayObject in question is attached to display list. As a general policy, never try to access stage in the constructors. You need to subscribe a certain event that will tell you when stage is really available.

    public function IWannaStage()
    {
        // WRONG
        x = stage.stageWidth >> 1;
        y = stage.stageHeight >> 1;
    
        // RIGHT
        if (stage) onStage();
        else addEventListener(Event.ADDED_TO_STAGE, onStage);
    }
    
    private function onStage(e:Event = null):void
    {
        removeEventListener(Event.ADDED_TO_STAGE, onStage);
    
        // Stage is really available from this point on.
        x = stage.stageWidth >> 1;
        y = stage.stageHeight >> 1;
    }
    

    Second, I think you’re trying to create instances incorrectly. If ForegroundImage is a class, then:

    A = ForegroundImage; // Wrong, you assign the reference to the class.
    B = ForegroundImage(); // Wrong, this actually is type casting.
    C = new ForegroundImage(); // Correct way to create a new instance of a class.
    D = new ForegroundImage; // If constructor does not need any arguments you can omit brackets.
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search