2009-07-28 7 views
0

子クラスから送出されたイベントのリスニングに問題がありますが、その理由はわかりません。eventListenerでのFlash AS3の問題

私はクラスに持っている: クラス1、魔女は、四角形を描画し、カスタムイベント

パッケージ { 輸入は、flash.displayを送出します。 ; import flash.events。; import flash.text。*;

public class clase1 extends Sprite 
{ 
    private var labelField:TextField; 
    public function clase1(label:String = "buttono") { 
     // draw the background for the button. 
     graphics.beginFill(0x3366CC); 
     graphics.drawRect(0, 0, 100, 30); 
     // store the label as the button’s name. 
     name=label; 
     // create a TextField to display the button label. 
     labelField = new TextField(); 
     // ensure clicks are sent from labelField rather than the button. 
     labelField.mouseEnabled=false; 
     labelField.selectable=false; 
     labelField.text=label; 
     labelField.x=10; 
     labelField.y=10; 
     labelField.width=80; 
     labelField.height=20; 
     addChild(labelField); 
     dispatchEvent(new Event("Hello",true)); 
    } 


} 

}

クラス2は、魔女は、別の矩形を描画し、イベント

パッケージ{ インポートは、flash.displayを聴取します。 ; import flash.events。; import flash.text。*;

public class clase2 extends Sprite { 
    private var labelField:TextField; 
    public function clase2(label:String = "buttono") { 
     // draw the background for the button. 
     graphics.beginFill(0xFFFCCC); 
     graphics.drawRect(200, 0, 100, 30); 
     // store the label as the button’s name. 
     name=label; 
     // create a TextField to display the button label. 
     labelField = new TextField(); 
     // ensure clicks are sent from labelField rather than the button. 
     labelField.mouseEnabled=false; 
     labelField.selectable=false; 
     labelField.text=label; 
     labelField.x=210; 
     labelField.y=10; 
     labelField.width=80; 
     labelField.height=20; 
     addChild(labelField); 
     addEventListener("Hello",eventHandler,true); 

    } 
    function eventHandler(event: Event) 
    { 
     trace("event received "); 
    } 
} 

}

及びIは

インポートclase1を有するFLAに、

var c1:clase1 = new clase1();

import clase2;

var c2:clase2 = new clase2();

addChild(c2);

c2.addChild(c1);

c1のc2の親を作成しますが、メッセージは表示されません。なぜですか? C1はC2が今まで作成し、イベントリスナーを追加する前に、それのコンストラクタからイベントを発生しようとしているよう

ありがとう

答えて

0

だけで簡単に一見、それが見えます。 c1の別のメソッドからイベントを発生させてみてください。そして、c2.addChild(c1)を呼び出した後でそのメソッドを呼び出すことができます。私はあなたのコードを変更した場合

少しだけ、私は、イベントが

public function clase1(label:String = "buttono") { 
     name=label; 
     labelField = new TextField(); 
     labelField.text=label; 
    } 

    public function drawRect():void { 
     // draw the background for the button. 
     graphics.beginFill(0x3366CC); 
     graphics.drawRect(0, 0, 100, 30); 
     labelField.mouseEnabled=false; 
     labelField.selectable=false; 
     labelField.x=10; 
     labelField.y=10; 
     labelField.width=80; 
     labelField.height=20; 
     addChild(labelField); 
    dispatchEvent(new Event("Hello",true)); 
    } 

を発射すると、メインアプリケーションで

public function test():void { 
    var c1:clase1 = new clase1(); 
    var c2:clase2 = new clase2(); 

    c2.addChild(c1); 
    c1.drawRect(); 
    } 
+0

ありがとうございます!!!!!!本当に、私は終日このことに苦労しています –

0

私は別の問題が生じています見ることができていますが、私このスレッドと同じ行に沿って考える 私のイベントリスナーが発砲していない理由は何ですか?私は出力に「リスナーが追加されました」と表示されるため、オブジェクトが作成されているという事実を知っています。ここに私のコードです。

package com.wsh.savage.world { 

import com.wsh.savage.world.WorldManager; 

import flash.events.Event; 
import flash.events.EventDispatcher; 

public class WorldState extends EventDispatcher{ 

    //constants defining worlds state 
    private const STATE_STARTING:uint = 0; 
    private const STATE_RUNNING:uint = 1; 
    private const STATE_PAUSED:uint = 2; 
    private const STATE_DESTROYING:uint = 3; 

    //an instance of the manager of all world states 
    private var m_worldManager:WorldManager;  

    //keeps track of what state the WorldState is in 
    private var m_stateStatus:uint; 


    /** 
    * Constructor starts the state up 
    */ 
    public function WorldState(manager:WorldManager) { 
     //set flag that we are creating a world state 
     m_stateStatus = STATE_STARTING; 

     m_worldManager = manager; 
     this.addEventListener(Event.ENTER_FRAME,enterFrameListener,false,1,true); 
     trace('listener added'); 

     //set the flag that the state is now running 
     m_stateStatus = STATE_RUNNING; 
    } 

    protected function enterFrameListener(event:Event){ 
     trace('event fired'); 
     if(m_stateStatus == STATE_RUNNING){ 
      trace('running'); 
      m_worldManager.getKeyboardManager().getKeyStatus(40); 
     } 
    } 
} 

ありがとうございます!