Search code examples
actionscript-3eventscustom-events

Custom events not working in AS3


I have an event called SelectEvent. Whenever I click the buyer, the select event should be launched. But that is not what is happening.

My code of base:

package 
{

    import flash.display.MovieClip;
    import flash.utils.Timer;
    import flash.events.TimerEvent;
    import flash.events.MouseEvent;

    public class FashionFrenzy extends MovieClip
    {

        public var Buyer_mc:Buyer;
        public var Buyers:Array;
        private var BuyerNumber:Number;
        public var xpositions:Array;
        public var ypositions:Array;
        public var SelectedBuyer:Number;

        public function FashionFrenzy()
        {

            GameTimeController();

            xpositions=new Array();
            xpositions.push(523,563,603);

            ypositions=new Array();
            ypositions.push(377,377,377);

            Buyers = new Array  ;
            BuyerNumber=0;


            Time_mc.gameTimer.addEventListener(TimerEvent.TIMER,GenerateBuyers);
            addEventListener(SelectEvent.BUYERSELECT,showselectbuyer);


        }


        public function GameTimeController()
        {
            Time_mc.StartGame();

        }
        public function showselectbuyer(event:SelectEvent):void
        {
            trace("Bamba");         
        }
        public function GenerateBuyers(event:TimerEvent):void
        {
            if(BuyerNumber<6)
            {
                if (Math.random() < 0.01)
                {
                    var position:Number = Math.floor(Math.random()*3);
                    var newBuyer_mc = new Buyer(xpositions[position],ypositions[position],BuyerNumber);
                    ypositions[position]-=40;
                    Buyers.push(newBuyer_mc);
                    addChild(newBuyer_mc);

                    BuyerNumber++;

                }
            }

                for each (var buyer_mc:Buyer in Buyers)
                {
                    if(buyer_mc.walking==true)
                    {

                        buyer_mc.Enter();


                    }


                }


        }








    }

}

My code of the buyer:

package 
{

    import flash.display.MovieClip;
    import flash.utils.Timer;
    import flash.events.TimerEvent;
    import flash.events.MouseEvent;

    public class Buyer extends MovieClip
    {
        public var buyerTimer:Timer;
        public var walking:Boolean;
        public var stopposition:Number;
        public var buyerCode:Number;



        public function Buyer(startx:Number, stopy:Number, code:Number)
        {
            x=startx;
            stopposition=stopy;
            walking=true;
            buyerCode=code;

            BuyerProperties();

        }
        public function Enter():void
        {

            if(y>stopposition)
            {

                walking=false;
                StartFunction();

            }
            else
            {
                y= y+3;
            }
        }
        public function BuyerProperties():void
        {
            buyerTimer = new Timer( 25 );
            trace(" I am generated");

        }
        public function StartFunction():void
        {

            buyerTimer.start();
            addEventListener(MouseEvent.CLICK,Select);

            trace("My timer starts now within Buyer.as");
        }
        public function Select(event:MouseEvent):void
        {
            trace(buyerCode);
            dispatchEvent( new SelectEvent(SelectEvent.BUYERSELECT));
        }




    }

}

Now that when I'm clicking the buyer, the MouseEvent.CLICK is activated and then in the buyer.as, buyercode is traced on the screen. But the event is not dispatched or else it is dispatched but the eventlistener is not executing the code. I'm not getting any runtime errors. the function "showselectbuyer" is never even launched.

How should I solve it?


Solution

  • The accepted answer is incorrect. The provided solution works but for the wrong reasons. It creates a direct dependency to the object supposed to listen to the custom event and then makes that object be the dispatcher. All together this makes the whole idea of using event/custom event unnecessary at best since in that case a simple callback would work just as well. Instead using the useCapture flag would make the whole system work as expected:

    addEventListener(SelectEvent.BUYERSELECT,showselectbuyer, true);
    

    The accepted solution is also the inverse way of dealing with non DisplayObject event propagation. The dispatcher should be the listener (no dependencies) and not the listener should be the dispatcher (dependency necessary).