Actionscript 3 在没有正确参数的情况下调用某些函数

Actionscript 3 在没有正确参数的情况下调用某些函数,actionscript-3,flash,Actionscript 3,Flash,我的AS3程序有两个功能,一个在宽度和高度改变时启动: stage.addEventListener(Event.RESIZE, resizeListener); function resizeListener (e:Event):void { //some commands } 第二个会在几毫秒后触发一个: var myTimer:Timer = new Timer(clockUpdate, 0); myTimer.addEventListener(TimerEvent.TIMER, up

我的AS3程序有两个功能,一个在宽度和高度改变时启动:

stage.addEventListener(Event.RESIZE, resizeListener);
function resizeListener (e:Event):void { 
//some commands
}
第二个会在几毫秒后触发一个:

var myTimer:Timer = new Timer(clockUpdate, 0);
myTimer.addEventListener(TimerEvent.TIMER, updateData);
myTimer.start();

function updateData(e:TimerEvent):void {
    trace("AUTOUPDATE");
    trace(e);
}
我还需要手动启动这些函数,比如说当用户按下按钮时,但我不知道手动调用这些函数时必须发送哪些参数


我尝试了resizeListener()和updateData(),但它当然无法向我询问参数。

您可以通过提供默认值使函数中的参数成为可选参数。这是一个示例,使用上面的两个函数并使事件参数可选:

function resizeListener(e:Event = null):void { 
    //some commands
}


例如,调用
resizeListener()
现在将执行该函数,
e
的值将默认为
null

您可以通过提供默认值使函数中的参数成为可选参数。这是一个示例,使用上面的两个函数并使事件参数可选:

function resizeListener(e:Event = null):void { 
    //some commands
}


例如,调用
resizeListener()
现在将执行该函数,
e
的值将默认为
null
使事件参数成为可选的,
resizeListener(e:Event=null)
,正如walkietokyo的回答一样,是一个非常有效且通常很方便的解决方案。另一种选择是将您希望能够在不触发事件的情况下执行的操作放在一个单独的函数中,该函数可以由事件处理程序调用,也可以从其他任何地方调用

例如,假设在调整大小时要做的是重新排列布局,并且您还希望在初始化时,或在单击按钮时,或在任何时候进行相同的布局设置,您可以执行以下操作:

stage.addEventListener(Event.RESIZE, resizeListener);
function resizeListener(e:Event):void { 
   rearrangeLayout();
}

function rearrangeLayout():void {
   // The actual rearrangement goes here, instead of in resizeListener. This can be called from anywhere. 
}
addEventListener(Event.SOME_EVENT, eventListener);
function eventListener(e:Event):void { 
   // Code that needs the Event parameter goes here (if any).
   // Call other function(s), for the stuff that needs to be done when the event happens.
   otherFunction();
}

function otherFunction():void {
   // Stuff that is not dependent on the Event object goes here, an can be called from anywhere.
}
哪种方法可能是口味的问题,也可能因情况而异,实际上,两者都很好

将事件处理程序和另一个函数中的内容分离的好处是,不会出现必须检查e:event参数是否为null的情况。换句话说,您将在事件处理程序中拥有依赖于事件(如果有的话)的代码,在更一般的函数(而不是事件处理程序)中拥有独立于事件的代码

因此,在更一般和示意性的情况下,结构应该是这样的:

stage.addEventListener(Event.RESIZE, resizeListener);
function resizeListener(e:Event):void { 
   rearrangeLayout();
}

function rearrangeLayout():void {
   // The actual rearrangement goes here, instead of in resizeListener. This can be called from anywhere. 
}
addEventListener(Event.SOME_EVENT, eventListener);
function eventListener(e:Event):void { 
   // Code that needs the Event parameter goes here (if any).
   // Call other function(s), for the stuff that needs to be done when the event happens.
   otherFunction();
}

function otherFunction():void {
   // Stuff that is not dependent on the Event object goes here, an can be called from anywhere.
}

将事件参数设置为可选,
resizeListener(e:Event=null)
,如walkietokyo的回答所示,是一个非常有效且通常非常方便的解决方案。另一种选择是将您希望能够在不触发事件的情况下执行的操作放在一个单独的函数中,该函数可以由事件处理程序调用,也可以从其他任何地方调用

例如,假设在调整大小时要做的是重新排列布局,并且您还希望在初始化时,或在单击按钮时,或在任何时候进行相同的布局设置,您可以执行以下操作:

stage.addEventListener(Event.RESIZE, resizeListener);
function resizeListener(e:Event):void { 
   rearrangeLayout();
}

function rearrangeLayout():void {
   // The actual rearrangement goes here, instead of in resizeListener. This can be called from anywhere. 
}
addEventListener(Event.SOME_EVENT, eventListener);
function eventListener(e:Event):void { 
   // Code that needs the Event parameter goes here (if any).
   // Call other function(s), for the stuff that needs to be done when the event happens.
   otherFunction();
}

function otherFunction():void {
   // Stuff that is not dependent on the Event object goes here, an can be called from anywhere.
}
哪种方法可能是口味的问题,也可能因情况而异,实际上,两者都很好

将事件处理程序和另一个函数中的内容分离的好处是,不会出现必须检查e:event参数是否为null的情况。换句话说,您将在事件处理程序中拥有依赖于事件(如果有的话)的代码,在更一般的函数(而不是事件处理程序)中拥有独立于事件的代码

因此,在更一般和示意性的情况下,结构应该是这样的:

stage.addEventListener(Event.RESIZE, resizeListener);
function resizeListener(e:Event):void { 
   rearrangeLayout();
}

function rearrangeLayout():void {
   // The actual rearrangement goes here, instead of in resizeListener. This can be called from anywhere. 
}
addEventListener(Event.SOME_EVENT, eventListener);
function eventListener(e:Event):void { 
   // Code that needs the Event parameter goes here (if any).
   // Call other function(s), for the stuff that needs to be done when the event happens.
   otherFunction();
}

function otherFunction():void {
   // Stuff that is not dependent on the Event object goes here, an can be called from anywhere.
}