【问题标题】:Listen Custom Events on Main Class AS3在主类 AS3 上监听自定义事件
【发布时间】:2014-01-01 19:58:49
【问题描述】:

这是我在 actionscript 中的小问题。为了简单起见,我将两个小类放在一起来演示我的问题。

所以从 RedState.as 我派发自定义事件时,女巫将字符串传递给侦听器。我想听这个事件并在根类中获取传递的字符串。

如果我在另一个班级听同样的事件,一切似乎都很好,但主班没有反应:(:D

package 
{
    import assets.ButtonController;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;


    public class Main extends Sprite 
    {

        public var nameCollection:Array
        public var sManager:SceneManager
        public var cText:TempClass
        public var bManager:ButtonController;

        public var red:RedState
        public function Main():void 
        {   
            addEventListener(Event.ADDED_TO_STAGE, init);
        }
        public function init (e:Event):void {
            red = new RedState();
            addChild(red);
            addEventListener(TextDispatcher.SEND_TEXT, red_sendText);

        }

        public function red_sendText(e:TextDispatcher):void 
        {   trace ("Something")
            trace (e.url)
        }





    }

}


package  
{
    import flash.display.Sprite;

    public class RedState extends Sprite 
    {
        [Embed(source = "assets/states/red.png")]
        public var Red:Class;

        public var red:Sprite;

        public function RedState() 
        {
            red = new Sprite();
            red.addChild(new Red());
            addChild(red);
            dispatchEvent(new TextDispatcher(TextDispatcher.SEND_TEXT, "I wanna Sing!!"))

        }

    }

}

【问题讨论】:

    标签: actionscript-3 flash


    【解决方案1】:

    如果我对您的理解正确,您正在尝试从 RedState 类中调度 TextDispatcher.SEND_TEXT。假设一切都设置正确,有两个原因导致事件没有被分派。首先是您没有将侦听器添加到RedState 实例。那就是:

    public function init (e:Event):void {
        red = new RedState();
        red.addEventListener(TextDispatcher.SEND_TEXT, red_sendText);
        addChild(red);
    }
    

    添加这将允许 dispatchEvent 从RedState 工作,因为现在RedState 实例将监听它。但是RedState构造函数中还有另一个问题:

    public function RedState() {
        red = new Sprite();
        red.addChild(new Red());
        addChild(red);
        //trying to dispatch here will not work
        dispatchEvent(new TextDispatcher(TextDispatcher.SEND_TEXT, "I wanna Sing!!"))
     }
    

    您正试图在添加侦听器之前调度事件。目前没有任何对象带有此事件的侦听器,因此它永远无法运行red_sendText()

    因此,您应该这样做:

    public function init(e:Event):void {
        red = new RedState();
        red.addEventListener(TextDispatcher.SEND_TEXT, red_sendText);
        red.runText();
        addChild(red);
    }
    

    然后在你的 RedState 中:

    public function RedState() {
        red = new Sprite();
        red.addChild(new Red());
        addChild(red);
    }
    
    public function runText():void {
        dispatchEvent(new TextDispatcher(TextDispatcher.SEND_TEXT));
    }
    

    现在您的 RedState 实例将在您调用 runText() 之前监听事件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-07
      • 2017-02-18
      • 1970-01-01
      • 2017-02-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多