# Dispatching Custom Events

**URL:** <https://forum.kirupa.com/t/dispatching-custom-events/271642>\
**Category:** flash\
**Created:** [September 25, 2008, 6:41pm UTC](https://forum.kirupa.com/t/dispatching-custom-events/271642 "2008-09-25T18:41:11Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![adnan794](https://avatars.discourse-cdn.com/v4/letter/a/8edcca/32.png) [@adnan794](https://forum.kirupa.com/u/adnan794)\
**Post date:** [September 25, 2008, 6:41pm UTC](https://forum.kirupa.com/t/dispatching-custom-events/271642/1 "2008-09-25T18:41:11Z")

</div>

Hello

I want to dispatch a custom event and also a string value which can be used by the listener function.

I dont know for some reason I have been getting this error.

“1138: Required parameters are not permitted after optional parameters.”

Here is the code for CustomEvent Class

```auto

package {
import flash.events.*;

public class CustomEvent extends Event {

public static const GET_NAME:String = "getname";

public var buttonName:String;
// Constructor
public function CustomEvent (type:String,
bubbles:Boolean = false,
cancelable:Boolean = false,
buttonName:String ) {

super(type, bubbles, cancelable);
this.buttonName = buttonName;
}

public override function clone( ):Event {
return new CustomEvent(type, bubbles, cancelable, buttonName);
}

public override function toString( ):String {
return formatToString("CustomEvent", "type", "bubbles",
"cancelable", "eventPhase", "buttonName");
}
}
}

```

Here is the code of Btn class where this event is dispatched

```auto

package 
{
 import flash.display.*;
 import flash.text.*;
 import flash.events.*;
 import flash.geom.ColorTransform;
 
 public class Btn extends Sprite
 {
     
  private var nam:String;
  public function Btn(lbl:String)
  {
   
   this.nam = lbl;
   
   buttonMode = true;
   mouseEnabled = true;
   _label.mouseEnabled = false;
   
   _label.text = lbl;
   
   
   addEventListener(MouseEvent.CLICK, onClick);
   addEventListener(MouseEvent.MOUSE_OVER, onOver);
   addEventListener(MouseEvent.MOUSE_OUT, onOut);
   
  }// end constructor
  
  public function onClick(e:MouseEvent):void
  {
   trace("Clicked");
   dispatchEvent(new CustomEvent(CustomEvent.GET_NAME, true, 
            false, nam));
   
  }// end onClick
    
  
  public function onOver(e:MouseEvent):void
  {
   trace("over");
  }// end onOver
  public function onOut(e:MouseEvent):void
  {
     trace("OUT");
  }// end onOut
 }// end of launch class
}// end package

```

and the code on the timeline is given as under

```auto

var btn:Btn = new Btn("Soundtrack");
btn.addEventListener(CustomEvent.GET_NAME,
getName);
addChild(btn);
function getName(e:CustomEvent):void
{
 trace(e.buttonName);
}

```

Thanks i nadvance
