I have more than one Event Listener that calls a specific function, and I want to add if statements to check what event is being passed. How can I do that?
I tried a simple comparison like:
if(evt == MouseEvent.MOUSE_OVER)
But it will give an error because I comparing a MouseEvent object and a String.
You should use the type property of the event:
if(evt.type == MouseEvent.MOUSE_OVER)
Related
The problem is, I cannot find listeners having been added to the table.
It seems the method getListeners doesn't work properly. It returns an empty collection.
My code:
table.addValueChangeListener(new MyListener());
System.out.println("ListenerCount="+table.getListeners(ValueChangeListener.class).size());
Console output:
ListenerCount=0
What is wrong? I expected it would return 1.
AbstractComponent.getListeners takes the event type class as an argument.
Pass it a Property.ValueChangeEvent or one of it's implementations, a Field.ValueChangeEvent or Label.ValueChangeEvent, depending on your use case.
If you read the API very carefully you'll see that you need to provide the event type of your listener:
java.util.Collection<?> getListeners(java.lang.Class<?> eventType)
Returns all listeners that are registered for the given event type
or one of its subclasses.
In your case:
table.getListeners(ValueChangeEvent.class);
I encountered an error in Flash CS5.5 ( ActionScript 3 ) :
ArgumentError: Error #1063: Argument count mismatch on
MethodInfo-185(). Expected 1, got 0. at MethodInfo-186()
But I have no MethodInfo-185() and MethodInfo-186() . What's wrong with the Flash ?
Somehow Flash CS5.5 / AS3 compiler cannot identify nested functions. The compiler will refer nested functions ( myInnerFunction as example below ) as MethodInfo-123() ( or something similar ).
function myFunction() {
function myInnerFunction() {
}
}
This means yes, you have there an unnamed function. Make sure you have all event listeners enumerated, and check if you have a listener added like this:
addEventListener(Event.ENTER_FRAME,function():void {...});
Any event can be in place of an enter frame event I wrote. If so, this is the line with an error. An event listener function should always accept 1 parameter of corresponding Event type. In this case, the correct line should be:
addEventListener(Event.ENTER_FRAME,function(e:Event):void {...});
Note the parameter type. If you, for example, listen for a "click" mouse event, it should be of MouseEvent type instead.
EventDispatcher.addEventListener() expects first parameter for event type (parameter of String type).
But the current object can generate multiple types of events.
Is it possible to handle all of them in one handler? May be I can pass null for type parameter or something?
You should try to make way around and extend dispatchEvent function :
public override function dispatchEvent(evt:Event):Boolean {
trace(evt.type);
return super.dispatchEvent(evt);
}
You can put Your code here to handle all events dispatched in this object .
Yes, this is possible.
If you use getQualifiedClassName of the Event class, you could get the types using describeType. Then you know all types that could be added, assuming you are using a custom event with public static types as strings in same event class. Then you could loop through all types, and add listeners with all those types to the dispatcher.
This idea is included in the templelibrary (EventUtils.addAll), which I suggest to use.
See documentation: http://templelibrary.googlecode.com/svn/trunk/doc/temple/utils/types/EventUtils.html
Ive been using the 'event' parameter for my KeyboardEvents and MouseEvents in a recent project ive been working on for my course (VERY BASIC).
Im not entirely sure on what the 'e' part of e:KeyboardEvent actually does, and ive been asked to find out what information the parameter 'e' can actually access when using it.
Im sorry if the questions badly written, its been a long night!
EDIT: If A method takes the parameter (e:KeyboardEvent). what information could we access through the use of the parameter e?
e represents an instance of KeyboardEvent (the instance being passed to your listening function).
The most important property of KeyboardEvent (referenced by e in your example) is keyCode.
This determines which key is being pressed/released.
eg:
stage.addEventListener(KeyboardEvent.KEY_DOWN, _keyDown);
function _keyDown(e:KeyboardEvent):void
{
trace(e.keyCode); // Will be 65 if you press 'a'.
}
I'm assuming you have some function like this
function someFunction(e:KeyboardEvent):void
{
// code
}
You can access any information from the KeyboardEvent class, just the same as if the parameter were called "event". The name of the parameter doesn't affect what you can access through it; the type does.
Edit: "e" is just the name of the variable - it could be called fred, banana, or tyrannosaurusRex, and it would make no difference. The thing that determines what sort of information you can access through a variable is its type - in this case, KeyboardEvent. If you follow the KeyboardEvent link above, you will see documentation for the KeyboardEvent class, which will tell you all the things you can do with it. For example, one of the properties of KeyboardEvent is keyCode, which tells you which key was pressed:
if (e.keyCode == 32)
{
// 32 is the keyCode for spacebar, so spacebar was pressed
}
import flash.events.KeyboardEvent;
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyboardHandler);
function keyboardHandler(Jack:KeyboardEvent):void{
trace(Jack.keyCode);///----------see output pannel
}
/////////////////--------or
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyboardHandler2);
function keyboardHandler2(Banana:KeyboardEvent):void{
trace(Banana.keyCode);////////----see output pannel
}
You can type anything inside()including KeyboardEvent
You're naming the event that triggers the function, just like any other var it can be named anything. Then, depending on the type of event, you'll have access to a number of vars and function relating to whatever caused the event to trigger.
Edit: Here's what's available to you with a MouseEvent (Public Properties)
When you click on the button something happens. However it seems redundant to me that in the declaration of myListenerFunction, the event object e of class MouseEvent, actually has to have its data type MouseEvent mentioned.
mybutton.addEventListener(MouseEvent.CLICK, myListenerFunction);
function myListenerFunction(e:MouseEvent):void
{
// function body
}
Couldn't I get away with this (the .swf works just the same so far as I know...)?
function myListenerFunction(e):void
Since the data type of e should always match the class of the event MouseEvent.CLICK (which is MouseEvent)?
EDIT:
So let's say we go from a mouse event to a keyboard event. By not declaring the data type of e, we can not be prone to errors in not changing the data type of e. e by default is going to be of type KeyboardEvent
mybutton.addEventListener(KeyboardEvent.KEY_DOWN, myListenerFunction);
function myListenerFunction(e):void
{
// function body
}
You can keep the event type to the base class Event if you like. But you will not have access to any of the MouseEvent / KeyboardEvent-specific members when you do it like that.
Using it without a type will make it Object, which is dynamic, meaning you can try to access any member by name (even if it does not exist) - this is slower (a lot) and fairly error prone. You will not get compile time checking for example.