0

ちょっとピープス!ステージの下部に合わせたいこのフッター画像がありますが、エラーが発生します。

ご覧のとおり、コンストラクター関数に ADDED_TO_STAGE リスナーがあります。

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

}

}

26 行目をコメント アウトすると、ムービーは正しく動作します (もちろん、Y を 0 にしたくはありません)。

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

出力ウィンドウに表示されるエラーは次のとおりです。

TypeError: エラー #1009: null オブジェクト参照のプロパティまたはメソッドにアクセスできません。src.display::Frame/init()[/Users/lgaban/Projects/Player/src/display/Frame.as:26] で


アップデート

私自身の質問に答えました。ここで修正してください

4

2 に答える 2

1

カスタムイベントを使用するのは少しやり過ぎです。特に、ステージに追加するリスナーがある場合はなおさらです。私はこのようにします:

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

    }
}
于 2009-11-17T16:07:37.037 に答える
1

それが完全な答えではありませんが、そのエラーはステージがnullであることを示しています。

于 2009-11-16T22:35:07.493 に答える