AS3:通过actionscript单击按钮

时间:2013-07-07 16:33:27

标签: actionscript-3 flash

这是我非常简单的代码:

g2.addEventListener(MouseEvent.CLICK, buttG2);
function buttG2(event:MouseEvent):void
{
    buttonNote="G2";
    addWholeNote();
}

单击按钮时效果很好,但可以使用Actionscript从其他函数触发此函数吗?

2 个答案:

答案 0 :(得分:2)

在其他一些功能中:

function otherFunction() {
    buttG2(null);
}

你传递null,因为它从未使用过。您还可以为参数指定一个默认值,如下所示:

function buttG2(event:MouseEvent = null):void
{
    buttonNote="G2";
    addWholeNote();
}

然后调用没有任何参数的函数,因为默认情况下事件将是null

function otherFunction() {
    buttG2();
}

答案 1 :(得分:1)

使用null参数null允许从代码中的其他位置调用函数。您将无法获得任何mouseevent数据,但这可能不是问题。您可以将null传递给您的函数,但我发现它更清晰。

g2.addEventListener(MouseEvent.CLICK, buttG2);
function buttG2(event:MouseEvent = null):void
{
    buttonNote="G2";
    addWholeNote();
}

像任何其他函数一样调用它,param现在是可选的。

buttG2();