I have a button that triggers some code and I want to trigger that same code for initialization purposes w/o the user having to click the button....how would I programmatically trigger that buttons event?
thx for the help in adv.!
Printable View
I have a button that triggers some code and I want to trigger that same code for initialization purposes w/o the user having to click the button....how would I programmatically trigger that buttons event?
thx for the help in adv.!
Is your code triggered in the buttons call function or is it a separate function ?
Here's a fast "example".
yourButtonsInstanceName.addEventListener(MouseEven t.CLICK, onClick);
function onClick(event:MouseEvent):void
{
// do something
}
and for your second button you should only do a:
secondButton.addEventListener(MouseEvent.CLICK, onClick);
If you are calling a certain function inside onClick at your first button then you could do something like this for your second button:
secondButton.addEventListener(MouseEvent.CLICK, onSecondClick);
function onSecondClick(event:MouseEvent):void
{
yourFunction(); // call the specific function
}
Hope that helped, if not, try uploading a file or something so that I ( we ) can understand much better what your problem is.
Good luck.
Your question was vague, but I can guess that you mean one of two things. First: "How can I make the function not depend on the event?". That's fairly easy. You can make the event argument to the function have a default value.
Now handleClick can be used as a MouseEvent listener AND it can be called with no arguments.Code:public function handleClick(event:MouseEvent = null){
//stuff that never uses event
}
Second: "How can I simulate mouse actions without actually relying on the user?"
This one is a little more complex, but also do-able. You can use VirtualMouse, which is a handy little class from Senocular which simulates the mouse and can dispatch actual mouse events. Check the com.senocular.ui package under ActionScript 3 here: http://senocular.com/flash/actionscript.php
yes, that was a good suggestion and one that I will probably use...the other way I was thinking of involved using dispatchEvent, but I'm not familiar with using it and I'm sure that's what senocular utilizes in those classes probably.Quote:
Originally Posted by 5TonsOfFlax
thanks guys!
You could make your button dispatch a click event this way:
This triggers the event listener as if the user had clicked the button.Code:yourButtonsInstanceName.dispatchEvent(new MouseEvent(MouseEvent.CLICK));