views:

145

answers:

3

Hey peepz! I have this footer image, that I want to align to the bottom of the stage, however I'm getting errors.

As you can see I have an ADDED_TO_STAGE listener in the constructor function.

package src.display{

import flash.text.*;
import flash.display.*;
import flash.geom.Matrix;
import flash.events.Event;

public class Frame extends Sprite {
 private var footer:Sprite = new Sprite();

 // ☼ ------ Constructor
 public function Frame():void {
  this.addEventListener(Event.ADDED_TO_STAGE, tracer);
 }

 public function tracer(event:Event) {
  trace("Frame added to stage --- √"+"\r");
  this.removeEventListener(Event.ADDED_TO_STAGE, tracer);
 }

 // ☼ ------ Init
 public function init():void {
  footer.graphics.beginFill(0x000);
  footer.graphics.drawRect(0,0,800,56);
  footer.graphics.endFill();
  footer.y = (stage.height - footer.height); // <-- This Line

  addChild(footer);
 }

}

}

The movie will work correctly if I comment out line 26 (but of course I don't want Y to be 0):

footer.y = (stage.height - footer.height);

Here is the error in the output window I'm getting:

TypeError: Error #1009: Cannot access a property or method of a null object reference. at src.display::Frame/init()[/Users/lgaban/Projects/Player/src/display/Frame.as:26]


UPDATE

Answered my own quesiton, fix here

+1  A: 

Not that it is the complete answer, but that error is telling you that stage is null.

Guvante
A: 

My mistake, I forgot to make sure it was added to the stage in my Main class before I called the init function. Below I'm using my custom event class.

bg = new Frame();
bg.addEventListener("onComplete", initFrame);

function initFrame():void {
     bg.init();
}

I swear I just figured this out 2 minutes after I posted this question, but I feel I should leave it up for others?

The problem was in the Frame class's init function, I was trying to use stage before the class was completely added to the stage in my Main class. So now I have a listener in the Frame class that will dispatch a custom event to the Main class to let it know when it's ready to start taking code with stage in it.

Leon
+1  A: 

Using a custom event is a bit overkill, especially when you have the listener for added to stage already in there. I would do it like this:

package src.display{

    import flash.text.*;
    import flash.display.*;
    import flash.geom.Matrix;
    import flash.events.Event;

    public class Frame extends Sprite {

     // don't instantiate your sprite here, it's weird! :)
     private var footer:Sprite;

     // this is the same as in your example
     public function Frame():void {
      this.addEventListener(Event.ADDED_TO_STAGE, handleAddedToStage);
     }

            // i renamed this to reflect what it does
     private function handleAddedToStage(event:Event) {
      trace("Frame added to stage --- √"+"\r");
      this.removeEventListener(Event.ADDED_TO_STAGE, handleAddedToStage);
      init();
     }

     // this is also essentially the same, except for private since it shouldn't be called from the outside
     private function init():void {
      footer = new Sprite();
      footer.graphics.beginFill(0x000);
      footer.graphics.drawRect(0,0,800,56);
      footer.graphics.endFill();
      footer.y = (stage.height - footer.height);

      addChild(footer);
     }

    }
}
grapefrukt
Ah yeah, that's def a more cleaner way to do it.. In my main I was also calling the Frame.init function cause I needed to pass a var in, but I like this way a lot :)
Leon