View Full Version : Using an event listeners function at will - event dispatching?
waffe
October 19th, 2007, 11:38 PM
Hi,
I have a event listener that turns a camera on and off according to how much motion there is:
camera1 = Camera.getCamera(""+Number(aCameraComboBoxes[selComboBoxNum].selectedIndex-1));
aCameras[selComboBoxNum].addEventListener(ActivityEvent.ACTIVITY, activityHandler, false, 0, true);
private function activityHandler(event:ActivityEvent):void {
if(event.activating){
//camera is turning on
}else if (!event.activating){
//camera is turning off
}
}
All of this works but I need a way to rerun the addlistener's function without the camera actual turning on or off. Is there a way for me to use this function, and send it the variable it is looking for?
Thanks
Krilnon
October 20th, 2007, 12:00 AM
If you are only using the boolean value from the event that is passed, you could just split your function into two functions to end up with something like this:
private function activityHandler(event:ActivityEvent):void {
setCameraState(event.activating);
}
private function setCameraState(on:Boolean):void {
if(on){
// camera is turning on
} else {
// camera is turning off
}
}
This way, you could call setCameraState with or without passing an event to it while still being able to send the boolean value.
waffe
October 20th, 2007, 01:02 AM
Thanks Krilnon,
but I must not be explaining myself correctly. What I want to do is run the function activityHandler manually. In AS2 you could use this:
theFunctionName({})
and this would run the function.
Is there a similar way to do this in AS3?
Krilnon
October 20th, 2007, 01:33 AM
Sure, you can do the same thing in AS3. After all, the event handler is just a regular function. If you don't have an event to pass to the function, then you'll need to make the default value for the event parameter null:
private function activityHandler(event:ActivityEvent = null):void {}
The reason that I posted the two function approach is that you said 'and send it the variable it is looking for?' By 'send', I'm assuming that you mean pass as a parameter. The problem with trying to send a parameter to an event handling function is that the first argument of an event handler should be of the Event class. If you don't have an event to send, then it would be somewhat inconvenient to construct a fake one to send to your function. Of course, you could use an untyped parameter as the first parameter and check the datatype of the parameter, like this:
private function activityHandler(event:*):void {
if(event is ActivityEvent){
// the function was probably called through the event handler
} else if(event is SomeOtherType){
// the function wasn't called through the event handler
} else {
// the function was given an argument that you weren't expecting,
// but you can't check this at compile time
throw new ArgumentError('Incompatible type');
}
}
The problem with that method is that the AS compiler can't check the validity of the type of the argument that you're passing, so errors become harder to spot. It's okay to use this method if you're fine with that risk.
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.