Ryan
Ryan

Reputation: 10049

AS3: Clicking a button via actionscript

This is my very simple code:

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

It works great when I click the button, but is it possible to fire this function from another function using Actionscript?

Upvotes: 0

Views: 82

Answers (2)

mfa
mfa

Reputation: 5087

Use a default parameter of null to allow the function call from elsewhere in your code. You won't be able to get any mouseevent data, but that's probably not an issue. You could pass null into your function as you have it, but I find this to be cleaner.

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

call it like any other function, the param is now optional.

buttG2();

Upvotes: 1

altschuler
altschuler

Reputation: 3922

In some other function:

function otherFunction() {
    buttG2(null);
}

You pass in null since it is never used. You could also give the parameter a default value like this:

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

And then call the function without any parameters as event will then be null as per default:

function otherFunction() {
    buttG2();
}

Upvotes: 2

Related Questions