PDA

View Full Version : optionally passing a variable?



doublemazaa
December 4th, 2008, 12:40 AM
Minor frustration...

I write a function to be called when I click on an object, but never use reference to the event I pass to the function.



object.addEventListener(MouseEvent.CLICK, blowUp);

function blowUp(thisEventIsNeverReferenced:Event):void{
//Stuff happens here
}


Then later I want to call blowUp from actionscript without passing an event, but now actionscript complains that I'm not passing an event to the function. Is there anyway around this? Can I write my event listeners so they don't pass an event or can I write my functions to optionally accept input?

I know I can do the following, also, but am looking to see if there's an easier way.



//This results in a program full of shell functions
object.addEventListener(MouseEvent.CLICK, activateBlowUp);

function activateBlowUp(thisEventIsNeverReferenced:Event):v oid{
blowUp();
}

function blowUp():void{
//Stuff happens here
}


or



//Send dummy variable through - probably a bad practice to follow an required extra dummy code

object.addEventListener(MouseEvent.CLICK, activateBlowUp);

function blowUp(thisEventIsNeverReferenced:Event):void{
//Stuff happens here
}

var foo:Event = new Event("this is a waste of my time");
blowUp(foo);




Thanks.

Krilnon
December 4th, 2008, 12:52 AM
function f(parameter:Object = null):void {}

ViktorHesselbom
December 4th, 2008, 05:33 AM
function f(parameter:Object = null):void {}

Yep, either that or just the obvious. blowUp(null)

Iamthejuggler
December 4th, 2008, 06:08 AM
or

function blowUp(thisEventIsNeverReferenced:Event = null):void{
//Stuff happens here
}

and

blowUp();

which i guess is what Krilnon was saying.