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