English 中文(简体)
行动说明3
原标题:event flow in action script 3

我试图从这个阶段的某些部分发出一次习俗活动,我登记了另一个部分来听取它的意见,但其他部分却没有这样做。

这里是我的法典;我错了什么?

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;
        }
}

我不想在主文中写字。

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

因为我不想自欺欺欺欺人,而我有责任了解它需要倾听哪些事件。

最佳回答

狩猎和Mendu似乎在事件链中处于不同的链条。 事件增加,由于狩猎和Mendu是兄弟姐妹,他们无法相互接触。

一种解决办法是你从主屏幕上向菜单发送游戏参考资料。 然后,从那时起,从菜单上增加一次活动。

问题回答

亚历杭德罗是正确的,因为事件固然如故,而不是旁边,你的话题永远不会见到。

可能的解决办法:Mainalready <代码>compSource和compMenu上的“knows> 您可以安全地通过你的主要班子通过活动:

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

这样,<代码>Game和-Menu就保持独立。

事实上,如果你以这种方式建造,<代码> Menu并不需要听从一次核心变化活动,你只能改变更新功能,以掌握一个核心变量:

class Menu{

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




相关问题
Mysql trigger/events vs Cronjob

I have an auction website which let my users place an unlimited amount of autobiddings. To monitor these autobiddings something has to check the database every second. My question is if it is ...

Can an event be used as an event listener?

I m trying to expose some events from a private object which is contained inside the object I am creating and it looks like the compiler is happy with this: private WindowUpdateServer ...

鸡奸

由于将javascript DOM方法放在第html页底部(在<有人>之后)远比利用“j Query”准备活动要快得多,因此我们不得不这样做:

Attaching a property to an event in Flex/AS3

I have a parameter that needs to be passed along with an event. After unsuccessful attempts to place it on the type by extending the class, I ve been advised in another SO question to write a custom ...

jQuery bind on ajax load() event

I have a page which displays multiple blocks with results details. Inside each block I have some <a> tags with thickbox jQuery plugin attached: class="thickbox" Here is an example of one kind ...

HTML text input event

I have a form, and want to disable/enable its submit button depending on whether the form s input text fields are empty or have had text entered into them. I think this means having an event handler ...

IE doesn t run Javascript when you click back button

I ve built a shop with tons of JS running. One of the pieces of the cart, is a zip code field which is required to calculate shipping cost. This works fine going through the cart. Now clicking ...

热门标签