pass variable

I am sure i asked this before and couldnt find it the answer. I will check again.

to pass a variable in a custom event I need to create a custom class and override a clone function?

i want the event picked up by parent class
 dispatchEvent(new Event("introClicked")); //from

 dispatchEvent(new Event("introClicked",a)); //to
jagguyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

moagriusCommented:
anytime you extend event, you need to override the clone method.

you can give a custom event custom variables when defining the extended event class.

the example you gave isn't correct.  the second argument in the Event constructor is a boolean (true or false).

new Event("whatever", true);

means it'll bubble up the display list - e.g, pass it to it's parent.

if the bubble argument - the second argument - is false, it will not bubble.

CarnouCommented:
What you want is this:
class myEvent extends Event
{
  public var A:Object
  function myEvent( str:String, a:Object, bubbles:Boolean=false, cancelable:Boolean = false )
  {
    super( str, bubbles, cancelable );
    this.A = a;
  }
}
What this does is extend event so that you pass a string, as usual, and you can pass this object a.  bubbles and cancelable default the same way they do with the regular Event, so the user can tweak them if they want - they just have to pass the a object first.
Then you dispatch it as follows:
dispatchEvent( new myEvent( "introClicked", a ) );
To get the "a" object:
function myListener( e:myEvent ):void
{
  trace( "Object = " + e.A );
}
jagguyAuthor Commented:
isnt the second parameter you cant override as it has to be a boolen?
dispatchEvent( new myEvent( "introClicked", a,b ) );

I want to pass an integer variable with the event which here it would be 'b'
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

CarnouCommented:
What moagrius told you was for the Event class.  The second parameter of the Event class has to be Boolean.  The class I showed you is a derived class, and the second parameter in my example is an object.  You can change it to an integer if you need.
You'll notice that in the line beginning super, it passes a string followed by two Booleans.  The "super" is constructing the Event class, and passes in all the necessary parameters.
Take the class for a spin, you'll see it works fine.  *:-]
moagriusCommented:
just a quick note - you should make sure to define the overridden clone method if you want to bubble.

just a quick addition like this should be fine:

override public function clone():Event {
      return new myEvent(type, A, bubbles, cancelable);
}
jagguyAuthor Commented:
ok let me do an example on my own and I will post back
jagguyAuthor Commented:
Ok I get an error when getting variable from e event

 Access of possibly undefined property 'A' through a reference with static type flash.events:Event.
 this si what I have
//main

  myplayer.addEventListener("eventClicked",eventClickedHandler);

  private function eventClickedHandler(e:Event):void {
 trace( "Object = " + e.A ); //error

       }
      

//player
 private var  myevent:ClassEvent;
 private var  a:int ;

  public function goEvent():void{
a=4;
dispatchEvent( new ClassEvent( "eventClicked", a ) );
            
            }

jagguyAuthor Commented:
Ok I get an error when getting variable from e event

 Access of possibly undefined property 'A' through a reference with static type flash.events:Event.
 this si what I have
//main

  myplayer.addEventListener("eventClicked",eventClickedHandler);

  private function eventClickedHandler(e:Event):void {
 trace( "Object = " + e.A ); //error

       }
     

//player
 private var  myevent:ClassEvent;
 private var  a:int ;

  public function goEvent():void{
a=4;
dispatchEvent( new ClassEvent( "eventClicked", a ) );
           
            }



//event
package  {
      import flash.events.Event;

      public class ClassEvent extends Event
{
             public var A:int ;

            public function ClassEvent( str:String, a:int, bubbles:Boolean=false, cancelable:Boolean = false )
             {
                     super( str, bubbles, cancelable );
                    this.A = a;
             }
            
            override public function clone():Event
            {
            return new ClassEvent(type, A, bubbles, cancelable);
            }

      }
      
}
CarnouCommented:
That's because within the eventClickedHandler, you set argument e to be of type Event, when it should be of type ClassEvent.
Change
private function eventClickedHandler(e:Event):void {

to
private function eventClickedHandler(e:ClassEvent):void {
 
When you handle an event from the mouse, you type the variable to be of type MouseEvent; similarly, when you handle an event of your class event, you've got to take the ClassEvent as an argument.
(You'll notice in my example, for myListener, e is of type myEvent.)
Also, your line private var myevent:ClassEvent isn't necessary, since myevent is never used.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Adobe Flash

From novice to tech pro — start learning today.