Loosely related to the Dispatching Custom Events post, here's a quick example of how to deal with the data type checking of mandatory arguments, such as those used with event listeners.
Below is all the source code required for this example, including the errant segments. Lines 1 through 7 setup a simple timer that calls a function every 3 seconds, until the timer is triggered three times. If you test just that portion of the code, you'll see an initial delay of three seconds, followed by three traces to the Output panel.
Suppose, however, that you also want to trigger the same event once, immediately. If you just call the function, as seen in line 10, you will get an error. This is because you can't call a listener with required arguments, without sending the required values. That is, the listener function requires an event object for the evt parameter, and an error will be thrown if it is not supplied.
-
var timr:Timer = new Timer(3000, 3);
-
timr.addEventListener(TimerEvent.TIMER, onTimer, false, 0, true);
-
timr.start();
-
-
function onTimer(evt:TimerEvent):void {
-
trace("timer function triggered");
-
}
-
-
//scenario 1
-
//onTimer();
-
//error: 1136: Incorrect number of arguments. Expected 1.
-
onTimer(null);
-
-
//scenario 2
-
onTimer(new TimerEvent(TimerEvent.TIMER));
-
-
//scenario 3
-
//stage.addEventListener(MouseEvent.CLICK, onTimer, false, 0, true);
-
//TypeError: Error #1034: Type Coercion failed:
-
// cannot convert flash.events.MouseEvent to flash.events.TimerEvent.
-
stage.addEventListener(MouseEvent.CLICK, onClick, false, 0, true);
-
-
function onClick(evt:MouseEvent):void {
-
onTimer(new TimerEvent(TimerEvent.TIMER));
-
}
One easy way to accomplish this goal, is to send null with the function call, as seen in line 12. This will prevent an error from being thrown. If the event itself can be of use to you, you can create a new Timer event and send that to the listener argument. This can be seen in line 15, and is also covered in the previous post. All you have to do is follow the new keyword, with the desired event class, and the specific event you want to be created.
The third scenario is an example of wanting to augment the timer with mouse clicks. If you simply add a mouse listener to the stage, as seen in line 21, you will also get an error. The onTimer function will be called, but the event passed to the function will be a mouse event, rather than the required timer event. One way to get around this is to create an interim function, that correctly receives the mouse event and sends the desired timer event to the ultimate function, as seen in lines 23 through 25.
Print This Post


(6 votes, average: 4.83 out of 5) 






























THX for this one... Been banging my head against a wall for the last few days trying to figure out how to get a function within a function without this stupid error.
Coming from ASM and Basic on MSX starting to program again after so many years is more troublesome that I thought.
Arjan, hang in there. It's an uphill climb, but you'll soon find that the benefits of AS3 vastly outweigh the extra work required to get going again.
Is there any code for call this kind of functionality:
function display(n:String) { if (n==null) { trace("Welcoe Guest"); } else { trace("welcome: " n); } } display();//It's showing error //display("Mike"); It's working fineThanks,
Flash Duniya
@FD, yes. Just move the null from the function call (cited in the post) to the default value of the parameter:
function display(n:String=null) { if (n==null) { trace("Welcome, Guest"); } else { trace("Welcome, ", n); } } display() //display("Mike");hi.
great stuff.
so tell me, when doing this:
stage.addEventListener(MouseEvent.CLICK, onClick, false, 0, true);
instead of this:
stage.addEventListener(MouseEvent.CLICK, onClick);
will this be the proper way of removing an eventListener then:
stage.removeEventListener(MouseEvent.CLICK, onClick);
or do I need to add more parameters?
thanks
felisan