1

そのため、SOをかなり検索しましたが、この質問に対する答えを見つけることができませんでした。私のシンボル ライブラリには、actionscript 用にエクスポートされたムービークリップがあり、そのためのカスタム クラスを作成しました。ムービークリップをステージに追加した後で、カスタムのプライベート プロパティにアクセスしようとする場合を除いて、ほとんどの場合はうまく機能します。以下に例を示します。

package {

    public class MyMovieClip extends MovieClip {

        private var _isEnabled:Boolean = false;

        public function MyMovieClip():void {

            trace(this);
        }

        public function set isEnabled( b:Boolean ):void {

            _isEnabled = b;
        }

        public function get isEnabled():Boolean {

            return _isEnabled;
        }
    }
}

そして、ムービークリップのインスタンスをループでステージに追加する別のクラスがあります。

package {

    public class MyOtherClass extends MovieClip {

        public var myMC:MyMovieClip;
        public var docClass:*;

        public function MyOtherClass( docRef:* ):void { // passing in a reference to the DocumentClass so I can access the stage
            docClass = docRef;
            init();
        }

        public function init():void {

            for(var i:int=0; i<6; i++) {

                var myMC:MyMovieClip = new MyMovieClip; // instantiate the movieclip which is exported for actionscript and has a custom class 
                //set a few native properties
                myMC.name = "myMC" + i; //setting the name so I can reference this movieclip after it's been added to stage
                myMC.y = myMC.height * i + 20;
                myMC.x = 20;
                myMC.alpha = .7;
            }

            dispatchEvent(new Event(MyOtherClass.MOVIECLIPS_ADDED)); // just to be safe, let's dispatch a custom event when all movieclips have been added
        }

        public function traceEnabled():void {

            trace(docClass.stage.getChildByName("myMC1").isEnabled); // this throws: 1119: Access of possibly undefined property isEnabled through a reference with static type flash.display:DisplayObject

        }
    }
}

最後に、ドキュメント クラス内で MyOtherClass をインスタンス化します。

package {

    public class DocumentClass extends MovieClip {

        public var myOtherClass:MyOtherClass;

        public function DocumentClass():void {

            addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
        }

        public function onAddedToStage(e:Event):void {

            myOtherClass = new MyOtherClass(); // upon instantiation, init is called in MyOtherClass and all of my movieclips are added to the sage
        }
    }
}

何を与える?ステージに追加した後、MyMovieClip プロパティ isEnabled にアクセスできないのはなぜですか? 別の方法はありますか?(事前に助けてくれてありがとう)

4

1 に答える 1

0

内部的に のすべての子はDisplayObjectContainerとして参照されるDisplayObjectため、 を使用するgetChildByNameと DisplayObject が返されます。

getChildByNameコンパイル時エラーを発生させずにカスタム プロパティにアクセスするには、カスタム プロパティの Class として結果をキャストする必要があります。以下のコードを参照してください。

ただし、それが唯一の問題ではありません (エラーの理由ですが、修正すると実行時エラーも発生します)。

作成ループではmyMC、表示リストに追加していないため、クリップがステージ上にないため、stage.getChildByName() を呼び出すと null が返されます。

また、投稿されたコードの表示リストに myOtherClass を追加していません。

また、ドキュメント クラスへの参照を格納する必要はありません。addedToStage リスナーを追加しMyOtherClassて、ハンドラーを init にするだけです。

ここにいくつかの更新されたコードがありますMyOtherClass

    public function MyOtherClass():void { 
        if(stage){
            init(); //if stage is ready, call init, if not wait for the added to stage event
        }else{
            addEventListener(Event.ADDED_TO_STAGE,init);
        }
    }

    public function init(e:Event = null):void {
        removeEventListener(Event.ADDED_TO_STAGE,init);

        for(var i:int=0; i<6; i++) {

            var myMC:MyMovieClip = new MyMovieClip;
            myMC.name = "myMC" + i; //setting the name so I can reference this movieclip after it's been added to stage
            myMC.y = myMC.height * i + 20;
            myMC.x = 20;
            myMC.alpha = .7;

            addChild(myMC); //!!!! add to the displayList
        }

        dispatchEvent(new Event(MyOtherClass.MOVIECLIPS_ADDED)); // just to be safe, let's dispatch a custom event when all movieclips have been added
    }

    public function traceEnabled():void {
        var myMC:MyMovieClip = this.getChildByName("myMC1") as MyMovieClip; //!!! cast it as MyMovieClip so you have access to all the properties/methods in that class
        if(myMC){  //myMC will be null if the cast failed
            trace(myMC.isEnabled); 
        }
    }

    /*
       getChildByName is slow and cumbersome. Most people generally only use it for accessing things put on the timeline in the Flash IDE.  Using events is a much better way of accessing your items. If traceEnabled was caused by a mouse event attached to myMC, then this would be a much better implementation:
    */
    public function betterTraceEnabled(e:Event):void {
        var myMC:MyMovieClip = e.currentTarget as MyMovieClip;
        if(myMC){
            trace(myMC.isEnabled);
        }
    }

そしてあなたの文書クラス:

public class DocumentClass extends MovieClip {

    public var myOtherClass:MyOtherClass;

    public function DocumentClass():void {

        if(stage){
            onAddedToStage(null); //most of the time stage is already populated in the constructor of your document class
        }else{
            addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
        }
    }

    public function onAddedToStage(e:Event):void {

        myOtherClass = new MyOtherClass(); // upon instantiation, init is called in MyOtherClass and all of my movieclips are added to the sage
        addChild(myOtherClass); //add it to the displayList
    }
}
于 2012-09-21T21:00:33.253 に答える