Skip to main content
grendizerus
Known Participant
July 24, 2008
Question

How to send arguments to MouseEvent called functions?

  • July 24, 2008
  • 2 replies
  • 1752 views
Hello,

I can't figure out how to send arguments to a MouseEvent called function.

For example, in my code, the function is called by this way:
mysymbol.addEventListener(MouseEvent.ROLL_OVER, fPopupMenu);

Then, for the function to work, I must write it like this:
function fPopupMenu(event:MouseEvent) {
trace("blah");
}

Now, if I want my function to run this kind of code:
trace(some_argument);

What kind of code should I type ?

Thanks in advance,

Gz
This topic has been closed for replies.

2 replies

kglad
Community Expert
Community Expert
July 27, 2008
yes, if they each have ExtendButton as a base class.

he picked a different class name and didn't need any properties. viva la difference. flash is flexible.
kglad
Community Expert
Community Expert
July 24, 2008
if mysymbol is a member of a dynamic class, add properties to it and you can retrieve those properties in your listener function.
grendizerus
Known Participant
July 25, 2008
Hi kglad , thanks for the help.

However, this is a bit confusing to me, as I'm still a beginer with Flash CS3 and AS3.

What I'd like to do on my Flash stage, is a set of 5 buttons that would trigger the apparition of menus. Each button would trigger the apparition of their respective menu.

So, on each buttons, I put : button_x.addEventListener(MouseEvent.ROLL_OVER, fPopupMenu);

I want to use the same function (fPopupMenu) for all 5 buttons, but I need to specify WHICH menu it would trigger.
That's why I need to send an argument to the function.

Do I need to create a class for this? (I've never done this before)

Thanks for any insight.
Gz
July 25, 2008
Hello,

You can implement this with a class definition. I'm not an expert but I've played a bit with this type of thing and I think I can help.

You can create a class that will extend the Button class and possess the features you need. I did some experimenting with the following example:

package {
import fl.controls.Button;

public class ExtendButton extends Button {
private var clickCount:int;
private var buttonMessage:String;

public function ExtendButton() {
this.clicks = 0;
this.Message = "";
}

public function get clicks():int {
return clickCount;
}

public function set clicks(iCount:int):void {
clickCount = iCount;
}

public function get Message():String {
return buttonMessage;
}

public function set Message(strMessage:String):void {
buttonMessage = strMessage;
}

}

}

Sorry about the spacing issue...

The above defines a couple of properties on my ExtendButton. I can access them from an instance of the button in my movie. The above can be pasted into an AS3 file and saved with the name "ExtendButton.as". It should be saved in the same directory as your FLA that will use it.

To use it, you'll need to create a new FLA and then drag a button from the Components panel to the Stage. Delete the button, then in the Library right-click the button instance there and select the "Linkage..." option. In the dialog in the Class field, change "fl.control.Button" to "ExtendButton", and click the green checkmark to ensure the file can be found.

To use the button you can then create a document class to call ExtendButton, something like this:

package {
import flash.net.*;
import flash.system.*;
import fl.data.*;
import flash.events.*;
import flash.external.ExternalInterface;
import flash.utils.Timer;
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.text.TextFieldAutoSize;
import fl.controls.*;
import fl.containers.*;
import flash.display.*;
import flash.text.*;

public class Main extends MovieClip {
private var oBtn:ExtendButton = new ExtendButton();


public function Main() {
oBtn.width = 100;
oBtn.label = "Click to show message";
oBtn.Message = "This is the current button message.";
oBtn.addEventListener(MouseEvent.CLICK, showMessage);
oBtn.move(100,100);
addChild(oBtn);
}

private function showMessage(event:MouseEvent) {
trace(event.target.Message);
}

}
}

You don't need all those imports, but it doesn't hurt to include them for this example; I just snagged the list from another project...

Anyway, I saved this as Main.as and in the Properties tab for my FLA I set the DocumentClass property to Main. Then I ran the thing and it showed my button and traced my message as I expected.

I know this is primitive, but hopefully it'll help out with your efforts. I believe you could use this structure and include a "MenuName" property or some such that your event handler could access. Good luck, please post any questions here and I'll try to answer them

Regards,
Dave Spaar