event flow in action script 3

Posted by Shay on Stack Overflow See other posts from Stack Overflow or by Shay
Published on 2010-03-25T19:09:46Z Indexed on 2010/03/25 19:13 UTC
Read the original article Hit count: 389

Filed under:
|
|
|

i try to dispatch a custom event from some component on the stage and i register other component to listen to it but the other component doesn't get the event here is my code what do i miss

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

}

I don't want to write in Main compSource.addEventListener(ScoreChanged.SCORE_GAIN, compMenu.updateScore); cause i dont want the the compSource will need to know about compMenu its compMenu responsibility to know to what events it needs to listen....

any suggestions? Thanks!

© Stack Overflow or respective owner

Related posts about actionscript-3

Related posts about events