0

ステージで動的テキスト フィールドに入力しようとしていますが、上記のエラーが発生します。アクセスしようとしているテキスト フィールドはフレーム 180 のステージに存在します。このクラスは SimpleButton を拡張したもので、このクラスを拡張する 5 つのサブクラスを持つスーパー クラスです。

また、既存のテキストにアクセスする代わりに新しいテキストを作成しようとしましたが、同じエラーが発生しました。

package  OOPGame{

    import flash.display.SimpleButton;
    import flash.display.MovieClip;
    import flash.events.MouseEvent;
    import flash.text.*;

    public class Taste extends SimpleButton{

        internal static var sweetNum = 0; 
        internal static var bitterNum = 0; 
        internal static var sourNum = 0; 
        internal static var saltyNum = 0; 
        internal static var umamiNum = 0;
        internal static var recipeNum = 0;
        internal static var ingNum = 0;
        internal static var recipe:Array = new Array(3);
        internal static var recipeOne:Array = new Array;
        internal static var recipeTwo:Array = new Array;
        internal static var recipeThree:Array = new Array;
        internal static var recipeFour:Array = new Array;
        internal static var recipeFive:Array = new Array;
        protected var taste = "";
        protected var rOiO:TextField;

        public function Taste() {
            addEventListener(MouseEvent.CLICK, onClick);
        }

        public function getRecipe(obj:Object, num:int):void{
            recipe[num] = obj;
            trace("recipe: " + recipe);

        }

        protected function onClick(event:MouseEvent){
            if (DraggableIngredient.level == 4)
                MovieClip(root).gotoAndStop(76, "Scene 2");
            else {MovieClip(root).gotoAndStop(180, "Scene 2");
                  ++recipeNum;
                 // trace(getRecipe());
                       if (recipeNum == 1){
                           for (var i:Object in recipe){
                                recipeOne[ingNum] = recipe[i];
                                ++ingNum;}
                           recipeOne[ingNum] = taste;}
                       else if (recipeNum == 2){
                           for (i in recipe){
                                recipeTwo[ingNum] = recipe[i];
                                ++ingNum;}
                           recipeTwo[ingNum] = taste;}
                       else if (recipeNum == 3){
                            for (i in recipe){
                                recipeThree[ingNum] = recipe[i];
                                ++ingNum;}
                           recipeThree[ingNum] = taste;}

                trace("this's your first recipe "+recipeOne);
                //trace(recipeTwo);
                //trace(recipeThree);

                rOiO = parent.getChildByName("r1i1") as TextField;// here is the error
                //rOiO.text = recipeOne[0].toString();
                //rOiO = new TextField;
                 //parent.addChild(rOiO);
                }


        }


    }

}

関数 getRecipe は、配列レシピを埋めるためだけに別のクラスで呼び出され、機能しています。次の出力が得られます。

this's your first recipe [object Orange],[object Apple],[object Tomato],Sweet
TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at OOPGame::Taste/onClick()
    at OOPGame::Sweet/onClick()

私のサブクラスは次のようになります。

package  OOPGame{

    import flash.events.MouseEvent;

    public class Sweet extends Taste{

        public function Sweet() {
            // constructor code
        }

        override protected function onClick(event:MouseEvent){
            ++sweetNum;
            taste = "Sweet";
            super.onClick(event);
        }
    }

}

前もって感謝します。どんな助けでもいただければ幸いです

4

1 に答える 1

0

あなたのコメントは、which extendsr1i1からアクセスしようとするとエラーが発生することを示しています:SweetTaste

rOiO = parent.getChildByName("r1i1") as TextField;

問題は、そのコード行が実行される前に、Sweet存在する現在のフレームから移動していることです。

MovieClip(root).gotoAndStop(180, "Scene 2");

Sweetシーン 2 のフレーム 180 の表示リストに のインスタンスが存在しないと仮定すると、 parentnull になります。したがって、エラーは実際には ではなく、親を参照していr1i1ます。

この問題を解決するには多くの方法があります。リストの上位にあるのは、シーン/フレームを使用して表示リストを制御しないことです。あなたのアプリケーションの残りの部分がどのように見えるかわかりませんので、それについて説明するのは難しいでしょう.

推奨されませんが、この問題に対処する 1 つの方法は、親クリップへの参照を保持し、アクセスするr1i1代わりにそれを使用することです。parentそれができない場合は、より迂回したアクセス方法を見つけることができるはずです(おそらくr1i1を介して)。root

于 2014-08-16T21:00:40.393 に答える