trace("HELP ME WITH CUSTOMS EVENTS PLEASE!")

  Hi im trying to practice Custom Events by making little stupid projects and is not working. I've read every article online and in like 5 books, i understand it all but when i try it on my own little things, it doesnt work.

  I have 2 movieclips on the stage (named red_mc and blue_mc) and im trying to dispatch and event to the blue_mc, when i click the red_mc.

 thanks for the help.

Thanks for the help.

   I've seen people creating another public method like you did but i didn't know about the MovieClip on the stage thing. I've been  trying to learn AS3 for like 5 months now so sorry for my ignorance. Thank you so much for helping me. I really like this Website, i know i'm going to learn a lot here.

Jody Hall's picture

Ross, There is nothing wrong

Ross,

There is nothing wrong with your code. You probably got this error:

ArgumentError: Error #1063: Argument count mismatch on Blue(). Expected 1, got 0.
    at flash.display::Sprite/constructChildren()
    at flash.display::Sprite()
    at flash.display::MovieClip()

There is a problem in Flash with having MovieClip instances that are already on the stage accept parameters. Since these instances are constructed at authoring time (because you are physically placing them on the stage) there is no way to send them arguments. There are two possible solutions to this. One is to delete your instances from the stage and create them dynamically instead from some controlling code (this can either be in code on frame 1 or in a document class):

var red:Red = new Red();

var blue:Blue = new Blue(red);

Then you would have code that set the x's and y's for positioning of course. And you would use addChild to add them both to the screen. And now your event code will work.

The other way is to go ahead and leave your stage instances in place. But edit the Blue class and take away the required parameter in the constuctor. Instead, add a public method that does the same thing:

package
{
    import flash.display.MovieClip;
    import flash.events.*;
  
    import Red;
  
    public class Blue extends MovieClip
    {
      
        public function Blue()
        {
           
        }
        public function setRed(red:Red)
        {
            red.addEventListener(Red.CLICKED, redClicked);
        }
      
        public function redClicked(event:Event)
        {
            trace("from blue- red was clicked");
        }
    }
}

Then in your controlling code (once again, either on frame 1 of an actions layer or inside a document class), add this one line of code:

blue.setRed(red);

Once again, your custom event will be dispatched and handled as expected.

 

these are the 2 classes.Sorry.

package
{
    import flash.display.MovieClip;
    import flash.events.*;
   
    public class Red extends MovieClip
    {
        public static const CLICKED:String = "clicked";
       
        public function Red()
        {
            this.addEventListener(MouseEvent.MOUSE_UP, clicked);
        }
       
        private function clicked(event:MouseEvent):void
        {
            trace("red was clicked");
            dispatchEvent(new Event(CLICKED));
        }
    }
}

--------------------------------------------------------------------------

package
{
    import flash.display.MovieClip;
    import flash.events.*;
   
    import Red;
   
    public class Blue extends MovieClip
    {
       
        public function Blue(red:Red)
        {
            red.addEventListener(Red.CLICKED, redClicked);
        }
       
        public function redClicked(event:Event)
        {
            trace("from blue- red was clicked");
        }
    }
}