【问题标题】:event flow in action script 3动作脚本 3 中的事件流
【发布时间】:2011-01-31 21:25:25
【问题描述】:

我尝试从舞台上的某个组件分派自定义事件,我注册了另一个组件来监听它,但另一个组件没有收到该事件。

这是我的代码;我错过了什么?

public class Main extends MovieClip //main document class
    {
        var compSource:Game;
        var compMenu:Menu;

        public function Main() 
        {
            compSource = new Game;
            compMenu = new Menu();
            var mc:MovieClip = new MovieClip();
            addChild(mc);
            mc.addChild(compSource); // the source of the event - event dispatch when clicked btn
            mc.addChild(compMenu);  //in init of that Movie clip it add listener to the compSource events
        }

    }


public class Game extends MovieClip 
    {
        public function Game() 
        {
            btn.addEventListener(MouseEvent.CLICK, onFinishGame);
        }

        private function onFinishGame(e:MouseEvent):void 
        {

            var score:Number = Math.random() * 100 + 1;
            dispatchEvent(new ScoreChanged(score));
        }


    }

public class Menu extends MovieClip
    {
        //TextField score
        public function Menu() 
        {
            addEventListener(Event.ADDED_TO_STAGE, init);
        }   

        private function init(e:Event):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);
//on init add listener to event ScoreChanged
            addEventListener(ScoreChanged.SCORE_GAIN, updateScore);
        }

        public function updateScore(e:ScoreChanged):void 
        {
//it never gets here!
            tScore.text = String(e._score);
        }   

    }



public class ScoreChanged extends Event
    {
        public static const SCORE_GAIN:String = "SCORE_GAIN";
        public var _score:Number;

        public function ScoreChanged( score:Number ) 
        {
            trace("new score");
            super( SCORE_GAIN, true);
            _score = score;
        }
}

我不想写在 Main

compSource.addEventListener(ScoreChanged.SCORE_GAIN, compMenu.updateScore);

因为我不想让 compSource 知道 compMenu; compMenu 有责任知道它需要监听哪些事件。

【问题讨论】:

    标签: actionscript-3 flash events


    【解决方案1】:

    游戏和菜单似乎位于事件链中的不同链上。事件向上冒泡,由于 Game 和 Menu 是兄弟,它们将无法访问彼此的事件。

    一种解决方案是让您将游戏的参考从主屏幕发送到菜单。然后在此时从菜单中添加一个事件侦听器。

    【讨论】:

      【解决方案2】:

      Sandro 是正确的,因为事件会冒泡,而不是横盘,你的Menu 永远不会看到事件。

      一个可能的解决方案:Main 已经“知道”compSourcecompMenu 你可以安全地通过你的主类传递事件:

      class Main{
          public function Main() 
          {
              compSource = new Game();
              compSource.addEventListener(ScoreChanged.SCORE_GAIN, scoreGainHandler);
              compMenu = new Menu();
              //... rest of constructor
          }
      
          public function scoreGainHandler(event:ScoreChanged):void
          {
              compMenu.updateScore(event);
          }
          //... rest of class
      

      这样您的GameMenu 将保持独立。

      实际上,如果你这样构建,Menu 根本不需要监听 score 变化事件,你只需将 update 函数改为获取 score 变量即可:

      class Menu{
      
          public function updateScore(score:int):void 
          {
              tScore.text = String(score);
          }
          //... etc
      

      【讨论】:

        猜你喜欢
        • 2012-01-16
        • 2012-12-14
        • 1970-01-01
        • 2010-12-09
        • 1970-01-01
        • 2012-02-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多