Actionscript 3 通过setInterval将e:MouseEvent作为参数传递

Actionscript 3 通过setInterval将e:MouseEvent作为参数传递,actionscript-3,actionscript,Actionscript 3,Actionscript,所以我有这个函数 capture_mc.buttonMode = true; capture_mc.addEventListener(MouseEvent.CLICK,captureImage); function captureImage(e:MouseEvent):void { //lalalala } 我想每2秒钟调用一次这个函数(在鼠标点击事件发生后)。 我试过使用setInterval setInterval(captureImage,2000,e:MouseEvent);

所以我有这个函数

capture_mc.buttonMode = true;
capture_mc.addEventListener(MouseEvent.CLICK,captureImage);

function captureImage(e:MouseEvent):void { 
//lalalala

}
我想每2秒钟调用一次这个函数(在鼠标点击事件发生后)。 我试过使用setInterval

setInterval(captureImage,2000,e:MouseEvent);
但它会导致以下错误

1084: Syntax error: expecting rightparen before colon.
怎么了?
是的,我是新来的

首先,因为这是AS3,所以您应该使用and。我将在示例中演示如何进行

现在,您需要分离您的函数:

编辑:根据@(胡安·帕布罗·卡里法诺)的建议,我已经更新了这篇文章,使之更安全。如果时间不会改变,我会永远保持同样的计时器

// first param is milliseconds, second is repeat count (with 0 for infinite)
private var captureTimer:Timer = new Timer(2000, 0);
captureTimer.addEventListener(TimerEvent.TIMER, handleInterval);

function handleClick(event:MouseEvent):void
{
    // call here if you want the first capture to happen immediately
    captureImage();

    // start it
    captureTimer.start();
}

function handleInterval(event:TimerEvent):void
{
    captureImage();
}

function captureImage():void
{
    // lalalala
}

您还可以随时使用captureTimer.stop()停止计时器。

首先,因为这是AS3,您应该使用和。我将在示例中演示如何进行

现在,您需要分离您的函数:

编辑:根据@(胡安·帕布罗·卡里法诺)的建议,我已经更新了这篇文章,使之更安全。如果时间不会改变,我会永远保持同样的计时器

// first param is milliseconds, second is repeat count (with 0 for infinite)
private var captureTimer:Timer = new Timer(2000, 0);
captureTimer.addEventListener(TimerEvent.TIMER, handleInterval);

function handleClick(event:MouseEvent):void
{
    // call here if you want the first capture to happen immediately
    captureImage();

    // start it
    captureTimer.start();
}

function handleInterval(event:TimerEvent):void
{
    captureImage();
}

function captureImage():void
{
    // lalalala
}

您还可以随时使用
captureTimer.stop()
停止计时器。

问题是,只有在声明形式参数时(或在声明
var
s和
const
s时),才应使用
parameterName:ParameterType
语法。也就是说,这仅在定义函数时有效:

function func(paramName:Type){
}
调用函数时,不必输入参数的类型

因此,您的函数调用应该如下所示:

setInterval(captureImage,2000,e);

问题是,只有在声明形式参数时(或在声明
var
s和
const
s时),才应该使用
parameterName:ParameterType
语法。也就是说,这仅在定义函数时有效:

function func(paramName:Type){
}
调用函数时,不必输入参数的类型

因此,您的函数调用应该如下所示:

setInterval(captureImage,2000,e);

+1.检查计时器是否在
handleClick
顶部运行也是一个好主意,如果正在运行,可能会退出(或者在上下文中有意义的情况下重置计时器)。这可以通过一个简单的布尔标志或检查
captureTimer
是否为空来实现。否则,您可能会导致多个计时器运行,并且无法阻止它们(因为当您为
captureTimer
变量分配一个新的
Timer
对象时,您将丢失对旧对象的引用。+1。检查计时器是否在
handleClick
的顶部运行也是一个好主意,如果它正在运行,可能会退出。)(或重置计时器,如果上下文允许的话)。这可以通过一个简单的布尔标志或检查
captureTimer
是否为空来实现。否则,可能会导致多个计时器运行,并且无法停止它们(因为在将新的
Timer
对象分配给
captureTimer
变量时,将丢失对旧对象的引用。