English 中文(简体)
Getting error when trying to use stage.height to place a graphic
原标题:

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 --- √"+"
");
        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

最佳回答

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 --- √"+"
");
            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);
        }

    }
}
问题回答

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





相关问题
Disable button tooltip in AS3

I want to disable the tooltip on certain buttons. The tooltip manager seems to be an all or nothing solution. Is it possible to disable the tooltip for just one or two buttons?

Sorting twodimensional Array in AS3

So, i have a two-dimensional Array of ID s and vote count - voteArray[i][0] = ID, voteArray[i][1] = vote count I want the top 3 voted items to be displayed in different colors, so i have a 2nd Array -...

Virtual Tour using sketch up, ajax, flash technologies

I want to know if there are existing technology that make your 3d models in sketch into virtual tours, using either Ajax or Flash for web presentation. If there s none, which will be a good approach ...

AS3 try/catch out of memory

I m loading a few huge images on my flex/as3 app, but I can t manage to catch the error when the flash player runs out of memory. Here is the what I was thinking might work (I use ???? because i dont ...

Red5 Security Tutorial

I am looking for a step by step tutorial on securing Red5 from intrusion. This seems to be a question that comes up alot in a google search, but is never really answered in a way that makes sense to ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

visible property of DisplayObject

For example I have a hierarchy of movie clips. mc1 is a child of mc, and mc2 is a child of mc1. Turns out that when I set mc1.visible = false; mc2.visible stays true. Is that supposed to happen?...

热门标签