0

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?

2 Answers 2

2

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();
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks guys! I got the same answer from both of you, picking the one that was first, upvoted both :)
1

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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.