Skip to main content
Known Participant
October 11, 2009
Question

Simple parameter passing on event listener

  • October 11, 2009
  • 1 reply
  • 1133 views

I'm new to ActionScript and have a fairly basic question. I want an eventlistener to call a function and pass it a parameter that will send the movie to a specific frame. My code generates and invalid parameter type error. It's as follows:

TL_AboutUs_btn.addEventListener(MouseEvent.MOUSE_OVER, DropSubMenu(10)); // calls a function to drop a sub menu

function DropSubMenu(sm:number)

{
gotoAndPlay(sm);
}

Error is "1046: Type was not found or was not a compile-time constant: number."

Newbie question ... I know. Can someone correct my syntax error?

Thanks,

Sam

This topic has been closed for replies.

1 reply

Inspiring
October 11, 2009

G'day Sam,

The problem is you are trying to pass a number typed parameter for a function which expects a MouseEvent typed parameter.

If I re-write your code,

TL_AboutUs_btn.addEventListener(MouseEvent.MOUSE_OVER, DropSubMenu);

function DropSubMenu(eventObject:MouseEvent)

{
gotoAndPlay(10);
}

Basically if you ask "TL_AboutUs_btn" to listen to a MouseEvent and call a function to handle it, you should always pass a MouseEvent typed object as a parameter to that function.

If you do following,

function DropSubMenu(eventObject:MouseEvent)

{

trace("eventObject.currentTarget=" + eventObject.currentTarget)
gotoAndPlay(10);
}

This will proves that event target was "TL_AboutUs_btn"

I can gather that you always want to pass number 10 as a parameter.

So it doesn't matter if you hard code it inside the function where its been handled.

Known Participant
October 11, 2009

If I remove the number parameter and hard code it then it works. Each button however will pass a different numbered parameter. Rather than have many different functions with gotandplay statements (one for each button) I thought I would create one function (DropSubMenu) and pass it the frame number for the gotoandplay statement. When I add the parameter to the eventhandler it causes the error.

Ned Murphy
Legend
October 11, 2009

You cannot pass a variable to a function thru an event handler the way you are trying to do it.  I do not know the correct process for doing it, but I believe it involves creating a custom event or something to that level of complication.  Maybe someone who knows what's involved will come along with that solution.

What you could do instead is use movieclips as buttons and assign each one a variable which you acquire in the event handler via event.currentTarget.variableName.  Or another approach you could take would be to name the buttons with the variable value, such as btn_10, and then extract the number from the end of the event.currentTarget.name property.