2017-06-07 10 views
1

私はactionScriptに問題があります。私はドキュメントクラス(メイン)内のメソッドにアクセスするために単純な1行のコードを使用しようとしていますが、エラーが発生するたびに発生します。ステージ上で同じコードをmovieClipで試してみましたが、うまく動作します。AS3 Mainクラス内のメソッドに別のクラスからアクセスします。エラー?

メイン・クラスはFLAにリンク:

package { 

import flash.display.*; 
import flash.events.*; 


public class Main extends MovieClip { 


    public function Main() { 

     if (stage) { 
      init(); 

     } 
     else addEventListener(Event.ADDED_TO_STAGE, init); 


    } 
    private function init(e:Event = null):void { 
     removeEventListener(Event.ADDED_TO_STAGE, init); 
     button.addEventListener(MouseEvent.CLICK,_click); 

    } 
    private function _click(e:MouseEvent):void { 
     var l:Leecher = new Leecher(); 
     l.leech(); 
    } 


    public function callMe():void { 
     trace("hey nice work"); 
    } 

} 

} 

リーチャクラス:

package { 

    import flash.display.*; 

    public class Leecher extends MovieClip { 

     public function leech():void 
     { 
      trace(" leech function "); 

      Main(parent).callMe();  // output null object 
      Main(root).callMe();  // output null object 
      Main(Main).callMe();  // output null object 


     } 

    } 

} 

同じコードが、上のボタンにリンクされているクラスステージ

package 
{ 

    import flash.display.*; 
    import flash.events.*; 


    public class Button extends MovieClip { 


     public function Button() { 
      this.addEventListener(MouseEvent.CLICK,r_click); 
     } 
     private function r_click(e:MouseEvent):void { 
      var l:Leecher = new Leecher(); 
      l.leech(); 
      Main(parent).callMe(); // hey nice work 
      Main(root).callMe(); // hey nice work 
      Main(Main).callMe(); // output null object 

     } 
    } 

} 
+0

** DisplayObject.parent **と** DisplayObject.root **は定義されています**問題の** DisplayObject **がステージの表示リストにアタッチされている場合のみ**** Button **インスタンスが添付されて動作します。** Leecher **インスタンスは何にも関連付けられていません。**親**と** root **は両方とも** null **です。次に、** Main(anything)**は型キャストであり、クラス自体はインスタンスではないので、** Main(Main)**は常に** null **を与えます。 – Organis

答えて

1

エラーは、コードの実行時に、Leecherインスタンスがまだ表示リストに追加されていないためであり、そのようにparent又はrootまたはstage(SO parentnullである)を有していません。ここ

は、(コードのコメントで説明する)何が起こっているかの内訳である:

private function _click(e:MouseEvent):void { 
    //you instantiate a new Leecher object 
    var l:Leecher = new Leecher(); 

    //you call leech, but this new object does not have a parent because you haven't added it to the display list (via `addChild(l)`) 
    l.leech(); 
} 

//your saying parent should be of type Main, then call the callMe method. However, parent is null because this object is not on the display list 
Main(parent).callMe(); 

//same as above, except using root 
Main(root).callMe(); 

//Here you are saying the Main class is of type Main (which since Main is a class and not an instance of Main will error or be null) 
Main(Main).callMe(); 

表示オブジェクトが表示リストにに追加されたときの表示オブジェクトのrootparent & stage VARSのみ移入さ。 root & stageの場合、親(および祖父母)は、最上位の親/祖父母がstageになるように追加する必要があります。

イベントをリッスンして、parentに安全にアクセスできるようになるまで待つ必要があります。

private function _click(e:MouseEvent):void { 
    var l:Leecher = new Leecher(); 

    //call the leech method once the child has been added to the stage and has a parent value 
    l.addEventListener(Event.ADDED_TO_STAGE, l.leech, false, 0, true); 
    addChild(l); 
} 

あなたが上記の操作を行う場合は、リーチメソッドにオプションのイベントパラメータを追加したり、エラーが発生します必要があります

public function leech(e:Event = null):void 
{ 

自分のメインクラスを作成するには簡単にアクセスできるので、静的な参照を使用できます。 静的変数は、オブジェクトのインスタンスには関連付けられず、クラス自体に関連付けられます。

Main.main.callMe(); 

私は前に、より多くのvarsの静的について読んでお勧めします:あなたはそれを行う場合

public class Main extends MovieClip { 
    //create a static var that holds a reference to the root/main instance 
    public static var main:Main; 

    public function Main() { 
     //assign the static var to this (the instance of Main) 
     main = this; 

     //...rest of code 

することは、あなたがして行うことができ、あなたの例ではそうMain.mainを行うことにより、どこでも、あなたのコード内であなたのルートを評価することができますそれらを使って狂ってしまう。あなたのドキュメントクラス/ルートへの簡単な参照のために私が示したことは安全ですが、他の状況では、それは気づいていることが最も良いメモリである&のメモリがあります。

+0

ありがとうございました: 'addChild(l);'私のためにエラーを修正してください、 –

関連する問題